Conao3 Notes

21ebbd.org

「leanfran: call Lean 4 functions from Rust, Python, TypeScript and Clojure, and prove their functions in Lean」の 21ebbd.org です。「そのまま開く」はファイルをそのまま返します。

* leanfran: call Lean 4 functions from Rust, Python, TypeScript and Clojure, and prove their functions in Lean
:properties:
:export_file_name: generated/contents/blog/2026/21ebbd
:export_date: 2026-11-27 19:13
:export_updated: 2026-11-27 19:13
:export_lang: en
:export_original: /blog/2026/23d62c
:export_tags: lean rust python typescript clojure formal-verification code-generation release lean-leanfran
:end:
I built leanfran, which lets you call functions written and proved in Lean 4 from Rust, Python, TypeScript, and Clojure.
It also works the other way: it translates functions written in each of those languages into Lean so you can prove their properties, so you can both build implementations proved in Lean into your application and verify in Lean the implementations you write in your usual language.

To generate code from Lean, leanfran reads the compiler's intermediate representation and builds a library for each language, and the generated code runs on that language's runtime with leanfran's runtime library.

The repository is [[https://github.com/conao3/lean-leanfran][conao3/lean-leanfran]], under the Apache-2.0 license.
This post covers the features of v0.6.0.

[[github:conao3/lean-leanfran::Lean][Write and prove functions in Lean 4, run them as Rust, Python, TypeScript and Clojure]]

[[./resources/public/img/23d62c-backends.svg]]

** Using proved functions in an application
Lean is a language in which you write functions and prove their properties; for example, you can write a function that doubles its argument and a theorem that the result is always twice the argument like this.

#+begin_src lean
def double (n : Nat) : Nat := n + n

theorem double_eq (n : Nat) : double n = 2 * n := by
  unfold double
  omega
#+end_src

=Nat= is the type of integers 0 and above.
=double_eq= states that the equation holds for every =n=, and Lean checks the proof to confirm that the equation holds for every natural number.

When I use this function in an application, I want to use the proved implementation as it is.
If I rewrite the function in another language, I have to check again that the rewrite and the Lean function compute the same thing, so leanfran generates source in the application's language from the Lean function.

I have in mind writing functions such as date and money calculations, sorting data, and state transitions in Lean, and writing the UI and the HTTP handling in the language you usually use.

** Supported languages
leanfran supports Rust, Python, TypeScript, and Clojure, and for each language it can generate code from Lean and translate that language's functions into Lean to verify them.
Each package is on that language's registry.

| Language | Package | Source and usage |
|------+------------+----------------|
| Rust | [[https://crates.io/crates/leanfran][leanfran]] | [[https://github.com/conao3/lean-leanfran/tree/master/langs/Rust][langs/Rust/]] |
| Python | [[https://pypi.org/project/leanfran/][leanfran]] | [[https://github.com/conao3/lean-leanfran/tree/master/langs/Python][langs/Python/]] |
| TypeScript | [[https://www.npmjs.com/package/leanfran][leanfran]] | [[https://github.com/conao3/lean-leanfran/tree/master/langs/TypeScript][langs/TypeScript/]] |
| Clojure | [[https://clojars.org/io.github.conao3/leanfran][io.github.conao3/leanfran]] | [[https://github.com/conao3/lean-leanfran/tree/master/langs/Clojure][langs/Clojure/]] |

Every backend uses Lean 4.35.0, and I release all of them with the same version number.
Each backend can load Lean code from a separate file or embed it in the source of the target language.

In TypeScript, for example, saving the following code as =src/arith.ts= embeds the function and theorem from above.

#+begin_src typescript
import { lean } from "leanfran"
import * as arith from "./_lean/arith.ts"

export const arithBlock = lean(
  "arith",
  String.raw`
def double (n : Nat) : Nat := n + n

theorem double_eq (n : Nat) : double n = 2 * n := by
  unfold double
  omega
`,
)

console.log(arith.double(21n))
#+end_src

When you run =leanfran sync=, Lean checks the theorem and leanfran generates the function in =_lean/arith.ts=; after that you can call the generated function from TypeScript, and this example prints =42n=.
The setup steps are in the [[https://github.com/conao3/lean-leanfran/blob/master/langs/TypeScript/README.md#install][TypeScript README]], and an example with structures is in the [[https://github.com/conao3/lean-leanfran/blob/master/docs/typescript-guide.md#structures-inductive-types-and-standard-schema][guide to types and schemas]].

** Types and input validation
leanfran converts not only the computation but also the arguments and return values into forms the target language can handle: from Lean structures and inductive types, each backend generates the types it exposes and the conversion code.

In TypeScript, it generates TypeScript types and a Standard Schema compatible schema from the Lean types, so you can pass that schema to Hono's validator to check HTTP requests, and the other way around, you can turn a schema you already have, such as a zod schema, into a Lean structure and use its constraints as premises of a proof.
In Python, you can handle the data as pydantic models.

There are concrete examples in [[https://github.com/conao3/lean-leanfran/tree/master/langs/TypeScript/samples/pricing-api][the TypeScript pricing-api]] and [[https://github.com/conao3/lean-leanfran/tree/master/langs/Python/samples/pricing-api][the Python pricing-api]].

Input validation here means checking the values the application receives to confirm that they meet the conditions the Lean proofs assume; for example, if you use a proof that assumes the lower bound is at most the upper bound, values from outside must meet that condition too.

** Verifying functions written in your usual language
In the other direction, you can compare functions written in Rust and the other languages with a Lean specification: leanfran translates functions within the supported subset into Lean, and you prove theorems about the translation.

In Rust, leanfran translates functions marked with =#[leanfran::model]= into Lean models with [[https://github.com/AeneasVerif/charon][Charon]] and [[https://github.com/AeneasVerif/aeneas][Aeneas]].
Charon extracts the Rust compiler's intermediate representation, and Aeneas translates it into Lean functions.
In the example below, =Std.U32=, =step*=, and =scalar_tac= are a type and tactics that Aeneas provides.
The next example is a function that doubles its input and returns =u32::MAX= when the result would exceed the limit, taken from the [[https://github.com/conao3/lean-leanfran/tree/master/langs/Rust/samples/steps/10-ensures][10-ensures sample]].

#+begin_src rust
#[leanfran::model(ensures(ge = double::ge))]
pub fn saturating_double(x: u32) -> u32 {
    if x > u32::MAX / 2 {
        u32::MAX
    } else {
        x * 2
    }
}
#+end_src

=double::ge= is the specification "the return value is at least the input", defined in Lean like this.

#+begin_src lean
def ge (x r : Std.U32) : Bool := decide (x.val ≤ r.val)
#+end_src

Running =cargo leanfran extract= generates a Lean model translated from the Rust function and a proposition stating that it meets the specification, and the sample writes the proof of that proposition in =lean/Proofs/SaturatingDouble.lean=.

#+begin_src lean
theorem saturating_double.proof.ge : saturating_double.obligation.ge := by
  intro x
  unfold saturating_double _root_.double.ge
  step* <;> simp_all [core.num.U32.MAX, U32.rMax] <;> scalar_tac
#+end_src

=cargo leanfran check= checks this proof, which shows that for every input the return value is at least the input, and also that the multiplication that runs does not overflow.
The application runs the function written in Rust as it is, and you verify its properties in Lean.

TypeScript supports verification in the same direction: you mark a function with =model= and give =refines= for agreement with a specification or =ensures= for a condition on the return value.

Here, though, you prove properties of the function translated into Lean, so to carry the result over to running in the original language, the translation has to preserve meaning and you have to keep the assumptions the translation makes.
For example, when it verifies a TypeScript =number= as an integer, it assumes that the arguments and intermediate values stay within the safe integer range, and the [[https://github.com/conao3/lean-leanfran/blob/master/docs/typescript-guide.md#verifying-typescript-functions][guide to verifying functions]] describes the supported syntax and the assumptions.

** How the conversion works and what the proofs cover
leanfran reads the intermediate representation that the Lean compiler produces, where the handling of the functions and data needed at run time has already become concrete instructions.
Each backend turns those instructions into code in the target language, and the runtime library handles operations on integers, strings, arrays, and so on, so the generated code runs within that language.

Lean's theorems directly guarantee the properties of the functions defined in Lean.
For those properties to hold in the generated code, the backends and runtime libraries must reproduce Lean's computation correctly, so I check this part with tests, including tests that compare the results of the generated code with the results from Lean's C backend.

I have not proved in Lean that leanfran's conversion itself is correct.

** Lean as a lingua franca
I made the name leanfran from lingua franca, a common language.
I want Lean's types and specifications to be a common language shared by several programming languages.

For now, what you can use is generating code from Lean for each language and verifying each language's functions against Lean specifications, and next I am thinking about a way to generate argument and return value conversions from Lean types when, for example, Rust calls a Python function.
These cross-language calls are still only an idea.

If you want to try it, start from the README for your language and run one short function and its proof.
The [[https://github.com/conao3/lean-leanfran/tree/master/langs/TypeScript/samples/steps][step-by-step leanfran samples]] walk through calling functions, types, input validation, and verifying functions, in that order.