Write it in Lean. Prove it in Lean. Run it in your language.
Quick look · Get started · Languages · Features · Documentation & examples · Contribution
leanfran brings Lean 4 to your programming language. Write functions and proofs together, compile them to your application's language, and call them through native types. The generated code runs with a small host-language runtime; Lean is needed to check and generate code, not to run the result.
Install the Python compiler and Lean 4.35.0, and start a project:
$ nix profile install 'github:conao3/lean-leanfran#python'
$ elan default leanprover/lean4:v4.35.0
$ uv init --lib myapp && cd myapp
$ uv add leanfran
Put a Lean function and its proof in a Python file:
# src/myapp/arith.py
from leanfran import lean
arith = lean("arith", r"""
def double (n : Nat) : Nat := n + n
theorem double_eq (n : Nat) : double n = 2 * n := by
unfold double; omega
""")
Generate the module and call it as Python:
$ uv run leanfran sync --package src/myapp
leanfran: 1 blocks of myapp checked and generated
$ uv run python -c 'from myapp.arith import arith; print(arith.double(21))'
42
Change the theorem to double n = 3 * n and generation stops at the proof:
$ uv run leanfran sync --package src/myapp
leanfran: /tmp/myapp/src/myapp/arith.py:7: error: (lean block arith, Lean 5:17) omega could not prove the goal:
a possible counterexample may satisfy the constraints
a ≥ 1
where
a := ↑n
leanfran: lake build failed
Every supported language embeds Lean the same way (see the table below). The output above is from leanfran 0.6.0 (PyPI and the Nix compiler) in /tmp/myapp.
leanfran compiles Lean's compiler IR into source code for the selected language. You can generate code from a Lean project or embed Lean blocks directly in host-language source files. Proofs are checked by Lean during generation and erased from the executable code.
| Language | Start here | Package | Integration |
|---|---|---|---|
| Rust | Rust quick start | leanfran on crates.io | lean!, Rust types, Cargo build integration |
| Python | Python quick start | leanfran on PyPI | lean(...), pydantic models, Python modules |
| TypeScript | TypeScript quick start | leanfran on npm | lean(...), TypeScript types, Standard Schema |
| Clojure | Clojure quick start | io.github.conao3/leanfran on Clojars | deflean, Clojure values, JVM namespaces |
| PHP | PHP quick start | Composer path repository (Packagist pending) | #[Lean], #[Model], Composer CLI |
The backends share Lean's semantics and are tested against Lean's native C backend. Their supported types, externs and host-language integrations are documented in the language guides.
Choose a language above and install its host package and compiler. Lean 4.35.0 is required for generation and proof checking. With Nix, install a compiler from this repository:
nix profile install 'github:conao3/lean-leanfran#python'
The compiler targets also include #rust, #typescript, #clojure, and #php. The Rust package also includes the Cargo CLI and Aeneas verification tools. For PHP, build the compiler from a checkout with nix build .#php and install the runtime through a Composer path repository. Follow the language quick start for dependencies, project setup, and generation commands.
New language support is welcome! Use the latest Claude Opus, OpenAI Sol, or a model with a comparable score on the Artificial Analysis Intelligence Index, and ask your coding agent to follow this guide:
Read docs/add-language.md and add support for <LANGUAGE> to leanfran.
The repository has a shared Nix environment and Lean toolchain, with language-specific source and packages under langs/. The PHP development shells are .#php for PHP 8.4 and .#php85 for PHP 8.5.
nix develop
make check
make check BACKENDS=Python
See CONTRIBUTING.md for build targets, formatting, packaging and the coordinated release process. Implementation notes live in docs/; maintenance tools live in maint/.
Can you improve this documentation? These fine people already did:
conao3 & github-actions[bot]Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |