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 Rust, Python, TypeScript, Clojure, PHP, Go, and C. 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, or with Lean's runtime library for C; Lean is needed to check and generate code, not to run the result.
Install the compiler leanfran and Lean 4.35.0, and start a project:
$ nix profile install 'github:conao3/lean-leanfran#leanfran'
$ 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 --lang python 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 --lang python sync --package src/myapp
leanfran: /tmp/myapp/src/myapp/arith.py:8: 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.14.0 (PyPI and the Nix compiler) in /tmp/myapp.
leanfran compiles Lean's compiler IR into source code for the selected language. For C, it uses Lean's C emitter and adds headers, conversion code and link settings. 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 | conao3/leanfran on Packagist | #[Lean], #[Model], leanfran --lang php sync |
| Go | Go quick start | leanfran-go on pkg.go.dev | go get github.com/conao3/leanfran-go, embedded Lean, Go module tool |
| C | C quick start | Git tags, Nix flake output #leanfran | leanfran:lean comments, C headers, pkg-config, Make and CMake |
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.
Proofs are theorems about Lean definitions. The code that runs is generated from those definitions, and tests cover the step from proof to running code: each backend's emitter and runtime (for C, Lean's own C emitter and runtime and leanfran's conversion code) is tested against Lean's native C backend. A proof about a host-language function is a theorem about its translation to Lean, so the translator is outside the proof. The C proof scope and the Go proof scope list what their proofs trust.
Choose a language above and install its host package and the compiler. Lean 4.35.0 is required for generation and proof checking. One executable, leanfran, compiles to every language; --lang selects the language. With Nix, install it from this repository:
nix profile install 'github:conao3/lean-leanfran#leanfran'
leanfran --lang python module generate --module-name MyModule --search-path .lake/build/lib/lean --output-file my_module.py
--lang takes rust, python, typescript, clojure, php, go or c. The package also holds the Lean library of the Go backend and the C support files. The #rust package adds the Cargo CLI and the Aeneas verification tools to the compiler. Without Nix, build the compiler from a checkout of a release tag with lake build leanfran.
To compile the modules of a Lake package to several languages, list them in leanfran.toml and run leanfran generate. It binds the types the generated modules share. See the generate guide.
Follow the Go quick start to add the Go module and tool to your application, and the C quick start to generate and link a C library. Follow the other language quick starts for their dependencies, project setup, and generation commands.
In Python, TypeScript, PHP, Go, and C, Lean blocks live in your source files. The host package reads them, and the compiler builds and generates them with three commands:
| Command | Effect |
|---|---|
leanfran --lang <language> sync | Builds every block with Lake, generates the host-language files, and records them in leanfran.manifest.json. |
leanfran --lang <language> check | Runs sync, translates the functions bound to Lean specifications, checks their proofs, and records the results in leanfran.lock. |
leanfran --lang <language> status | Compares the sources with leanfran.manifest.json, the generated files, and leanfran.lock without Lean or Lake. |
Python and TypeScript take the package directory with --package <dir>. PHP takes --package <dir>, and Go and C take --dir <dir>; these default to the working directory. Commit leanfran.manifest.json and leanfran.lock, which are at the project root, and the generated files; C keeps its output directory out of version control because its link settings hold absolute paths. sync keeps each language's Lake workspace in .leanfran/<language>/ and writes .leanfran/.gitignore, which excludes that workspace and the lock file .leanfran/lock; commit .leanfran/.gitignore too.
| Exit code | Meaning |
|---|---|
| 0 | Success. |
| 1 | status printed a stale, missing, or removed line, check found an unextracted or failed obligation, or an obligation is not proved under --require proved. |
| 252 | An argument is invalid. |
| 254 | The project has an error: a Lean error (reported at the line in your source file), a block the host package cannot read, or a file in the way that leanfran did not generate. |
| 255 | The environment has an error: the host package, lake, lean, clang, or sha256sum (shasum on macOS) is missing, or the compiler and the host package come from different releases. |
Use the compiler and the host package from the same release. When they come from different releases, sync, check and status exit with 255 and one line that names the release to install: the compiler reads the version of the installed host package before it runs it, and the host package stops when an older compiler passes it sync, check or status. sync, check and status run sha256sum on Linux and shasum on macOS and exit with 255 when that command is not on PATH, so CI jobs that run them need it.
Upgrade the compiler and the host package to the same release (a 0.15.0 compiler with a 0.14.0 host package, or the reverse, exits with 255 and names the release to install), then run the commands below once in each project and commit the result. status prints the files that sync will delete as <path>: removed lines. Delete .leanfran/workspace/ and .leanfran/sync.lock if they are in the project.
| Language | Run once | Commit |
|---|---|---|
| Python | leanfran --lang python sync --package <dir> for each package, then check for a package with @model functions | leanfran.manifest.json, .leanfran/.gitignore, leanfran.lock |
| TypeScript | leanfran --lang typescript sync --package <dir>, then check for a package with model(...) functions | the deletion of .leanfran/blocks.json, leanfran.manifest.json, .leanfran/.gitignore, leanfran.lock |
| PHP | leanfran --lang php sync, then check for a project with #[Model] functions | the deletion of each output directory's leanfran.json, leanfran.manifest.json, .leanfran/.gitignore, leanfran.lock |
| Go | leanfran --lang go sync, then check for a module with //leanfran:model functions | the deletion of each package's leanfran.json, leanfran.manifest.json, .leanfran/.gitignore, leanfran.lock |
| C | leanfran --lang c sync, then check for a project with LF_MODEL functions | leanfran.manifest.json, .leanfran/.gitignore, leanfran.lock |
sync deletes the files named in the deletions above. Commit after check: until then, status reports every function as stale, and in TypeScript the generated modules lack the calls into proved TypeScript functions. In C, sync also deletes <out-dir>/leanfran.manifest.json; if .gitignore lists leanfran.lock, remove that line so that the lock can be committed.
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/. Use .#php and .#php85 for PHP 8.4 and 8.5, and .#go and .#go126 for Go 1.27 and 1.26.
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 |