* rust-ritsuryo: Japanese date and tax calculations proved in Lean 4, as a Rust crate and a web page :properties: :export_file_name: generated/contents/blog/2026/c37a82 :export_date: 2026-11-27 18:40 :export_updated: 2026-11-27 18:40 :export_lang: en :export_original: /blog/2026/c4f11a :export_tags: lean rust formal-verification tax release rust-ritsuryo lean-ritsuryo lean-rs :end: [[https://s.conao3.com/blog/2026/232531][lean-ritsuryo]] is a library that computes the periods and tax amounts set by Japanese law in Lean 4, with proofs that the calculations match a specification transcribed from the statutes. I compiled it to Rust with [[https://s.conao3.com/blog/2026/1ff994][lean-rs]] and turned it into a Rust crate and a web page that does the calculations in the browser alone. The repository is [[https://github.com/conao3/rust-ritsuryo][conao3/rust-ritsuryo]], under Apache-2.0, and the crate is on [[https://crates.io/crates/ritsuryo][crates.io]] as =ritsuryo=. [[github:conao3/rust-ritsuryo::Rust][Japanese statutory periods and taxes for Rust and the browser, compiled from proved Lean 4 code with lean-rs]] ** Calculate in the browser The web page is at this URL. https://conao3.github.io/rust-ritsuryo/ [[./resources/public/img/c4f11a-page.png]] The form offers the same calculations as the lean-ritsuryo CLI, such as the end date of a period, public holidays, income tax, inheritance tax, and consumption tax. The image above shows the income tax for 2025 on a total taxable income of 5,000,000 yen, and the amount to pay comes to 584,500 yen. That amount is the income tax of 572,500 yen plus 12,022 yen of [[https://laws.e-gov.go.jp/law/423AC0000000117#Mp-Ch_4-Se_2-At_13][special income tax for reconstruction]] (2.1% of the income tax), which makes 584,522 yen, [[https://laws.e-gov.go.jp/law/423AC0000000117#Mp-Ch_4-Se_2-At_24-Pr_2][with the part under 100 yen cut off]]. The calculation runs inside the browser, so the page sends your input nowhere. ** Call it from Rust Add =ritsuryo= to =Cargo.toml=, and you can call the same calculations from Rust. #+begin_src toml [dependencies] ritsuryo = "0.1.0" serde_json = "1" #+end_src #+begin_src rust use serde_json::json; fn main() { let out = ritsuryo::invoke( "income-tax", "compute-tax", &json!({"TaxYear": 2025, "TaxableIncome": 5_000_000}), ) .unwrap(); println!("{}", out["Result"]); for h in ritsuryo::holiday::holidays(2027).iter().take(3) { println!("{}/{} {}", h.month, h.day, h.name); } } #+end_src #+begin_src console $ cargo run -q {"DefenseTax":0,"IncomeTax":572500,"Payable":584500,"ReconstructionTax":12022,"Total":584522} 1/1 元日 1/11 成人の日 2/11 建国記念の日 #+end_src =invoke= takes a CLI subcommand and its parameters as they are, and returns the same JSON that the CLI prints. For common calculations such as holidays and income tax, I also added functions that take and return Rust types. The crate builds with cargo alone, without Lean installed. ** How it works [[./resources/public/img/c4f11a-pipeline.svg]] The input to the translation is a single Lean module that holds only the functions to expose to Rust, and lean-rs turns this module into Rust code together with the lean-ritsuryo functions it calls. The lean-ritsuryo CLI keeps the parts that handle IO, such as files and standard output, apart from the calculations, and the calculations are functions without IO, so Rust and the browser can call them as they are. The web page compiles this crate to WebAssembly and calls it from a static page on GitHub Pages. The lean-rs runtime also runs on WebAssembly, where operations that use IO, such as file access, return an error, but the lean-ritsuryo calculations use no IO and run unchanged. Generating the Rust code builds the Lean package, including the lean-ritsuryo proofs, so only calculations whose proofs pass become Rust code. The proofs cover the Lean definitions only, though, and whether the Rust code computes the same thing depends on the lean-rs translation being correct.