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. The two install lines take the compiler and the Python package from master:
$ nix profile install 'github:conao3/lean-leanfran#leanfran'
$ elan default leanprover/lean4:v4.35.0
$ uv init --lib myapp && cd myapp
$ uv add 'leanfran @ git+https://github.com/conao3/lean-leanfran#subdirectory=langs/Python'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
info: leanfran-python-workspace: no previous manifest, creating one from scratch
✔ [2/6] Built LeanPythonEmbed.myapp.arith.arith (402ms)
✔ [3/6] Built LeanPythonModels (471ms)
✔ [5/6] Built LeanPythonEmbed (359ms)
Build completed successfully (6 jobs).
leanfran: 1 blocks checked and generated
$ uv run python -c 'from myapp.arith import arith; print(arith.double(21))'
42Change the theorem to double n = 3 * n and generation stops at the proof:
$ uv run leanfran --lang python sync
✖ [4/6] Building LeanPythonEmbed.myapp.arith.arith (296ms)
src/myapp/arith.py:8:18: error: (lean block myapp.arith:arith, Lean 5:17) omega could not prove the goal:
a possible counterexample may satisfy the constraints
a ≥ 1
where
a := ↑n
Some required targets logged failures:
- LeanPythonEmbed.myapp.arith.arith
error: build failed
leanfran: lake build exited with 1Every supported language embeds Lean the same way (see the table below). The output above is from the compiler and the Python package built from this repository with Lean 4.35.0.
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 |
- Proved Lean functions in your application. Keep an algorithm and its correctness proof together in Lean, then call the function generated from it.
- Validated data at the boundary. Lean structures and inductive types map to host-language values. Supported proof fields become runtime checks for the invariants your proofs assume.
- Specifications for host-language code. Bind supported Rust, Python, TypeScript, Clojure, PHP, Go, or C functions to Lean specifications, translate them to Lean, and check proof obligations. Each language guide describes its supported subset and verification tools.
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 the compiler of the release of your host package:
nix profile install 'github:conao3/lean-leanfran/v<version>#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. Take the compiler and the host package from the same release, or both from the same commit of master. The pages on master describe the commands of master: for a release, read the pages of its tag, such as v0.15.0, and put the tag in the URL. Without /v<version>, the URL installs the compiler of master. 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.
The Rust crates require Rust 1.88, and the Cargo CLI cargo-leanfran requires Rust 1.89.
To compile the modules of a Lake package to several languages, list them in the project file 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. |
leanfran sync, leanfran check and leanfran status without --lang run the command for every [embed.<language>] table of leanfran.toml in their order, and exit with the largest code. [embed.rust] runs cargo leanfran check and cargo leanfran status. The settings of sync, check and status describe the tables and projects with several languages.
--dir <dir> selects the project directory and defaults to the working directory. The project root is the nearest directory from there upwards with leanfran.toml, or else with the package file of the language (pyproject.toml, package.json, composer.json, or go.mod). leanfran --lang <language> <command> --help lists the options of a command. 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 or status 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 to 0.15.0 first, with the commands below, and then follow Upgrade from 0.15.0: later compilers do not recognize the files that 0.14.0 generated. 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 of 0.15.0 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.
The options, commands, environment variables and exit codes of every language follow one form. The old names are gone: an old option or command exits with 252. Change the scripts and CI jobs that use them.
In each project:
- Upgrade the compiler and the host package to the same release.
- Move the settings to
leanfran.tomlas the settings table says. - Rename the labels, options and theorems of the source table.
- Run
leanfran --lang <language> sync, thencheckfor a project with verified functions, and commit the result. Until then,statusreports every block and function asstale. A Rust crate follows the Rust table, and Clojure regenerates its blocks when they expand.
| Language | 0.15.0 | Now |
|---|---|---|
| All | The options of each language | One leanfran.toml at the project root with [check], [[require]], [embed.<language>] and [[generate]]. An unknown key or a value of another type exits with 254 and its position |
| All | The directory of --dir or --package, or the nearest package file |
The project root is the nearest directory upwards with leanfran.toml, or else with the package file of the language. A directory with neither exits with 254 |
| Python | --package <dir> for each package |
paths in [embed.python], or the package src/<name> or <name> of the project name in pyproject.toml; a package removed from them loses its generated files |
| TypeScript | "leanfran": { "tagKey", "option" } of package.json |
tag-key and option in [embed.typescript]. Only status detects a change of them; lean(...) at run time does not |
| PHP | extra.leanfran.paths and extra.leanfran.require of composer.json |
paths in [embed.php], and [[require]] |
| Go | paths and require of leanfran.config.json |
paths in [embed.go], and [[require]] |
| C | sources, lean-files, cflags and out-dir at the top level of leanfran.toml |
sources, lean-files, cflags and output-dir in [embed.c] |
| Rust | [package.metadata.leanfran] require of Cargo.toml, LEANFRAN_REQUIRE |
[check] require = "proved". LEANFRAN_REQUIRE has no effect |
| Clojure | The workspace in .leanfran/, with cache/ and out/ |
.leanfran/clojure/ with workspace/ and cache/. Delete cache/, out/ and the Lake files of the old workspace directly in .leanfran/ |
| Clojure | No settings of deflean |
[check] require = "proved" stops the expansion of a block with an unproved theorem, [[require]] adds Lake packages to the workspace, and a mistake in leanfran.toml stops the expansion |
[[generate]] |
out-dir |
output-dir |
Settings left in an old place of TypeScript, PHP, Go, C or Rust exit with 254 and a message that names the new table.
| Language | 0.15.0 | Now |
|---|---|---|
| Clojure | :use of deflean |
:uses. Another unknown key is an error |
| C | The line imports: <name>, … of a block |
uses: <name>, … |
| TypeScript | The label returnsNat and the theorems <function>.proof.returnsNat |
returns_nat |
Every language with ensures |
Also labels with other characters, such as inRange |
A label matches [a-z][a-z0-9_]*. refines is reserved, and so is returns_nat in Python, TypeScript and Rust |
| Every generated file | A first line in the form of each language, none in Clojure and C | Code generated by leanfran --lang <language> (generator <N>) from <Lean module>. DO NOT EDIT. in a comment |
Files that sync overwrites or deletes |
Files with the header of leanfran | The files of the manifest, and those whose first line names the language and a Lean library of its workspace |
Go and PHP module generate --output-dir |
Deletes other generated files of the directory | Go deletes nothing. PHP deletes the files of an earlier run of the same module |
| 0.15.0 | Now | |
|---|---|---|
| Number of inputs | 256 in Rust, 100 in Clojure, 200 in the others | 200, or LEANFRAN_RUNS (1 to 2147483647), which counts the inputs that satisfy requires |
| Seed | The default of the library in Rust, Python, TypeScript and Clojure, 1 in the others | 1, or LEANFRAN_SEED (0 to 9223372036854775807) |
| Value out of range | Different in each language | leanfran: <name> must be a decimal number between <least> and <most> (got <value>) |
| Environment variable of proptest | PROPTEST_CASES |
No effect |
| Failure | Different in each language | <id> [refines]: mismatch (seed <seed>, run <n>) or <id> [<label>]: <spec> does not hold (seed <seed>, run <n>), then input: and expected: …, got: … or result: |
| Name of the specification | Different in each language | The name in the block, such as atLeast |
| A function or a specification that throws or panics | Different in each language | failed: <exception> as the value of the function, or the spec failed and error: <exception> |
Few inputs satisfy requires |
Different in each language | Fails with <id>: only <n> of <m> inputs satisfied requires (seed <seed>) after 100 discarded inputs per run. Hypothesis stops earlier in Python |
The random test of Clojure has no label. With PHP, Verify::checkModel takes ?int $runs = null, ?int $seed = null.
| 0.15.0 | Now | |
|---|---|---|
leanfran.lock |
TOML in the crate | The section rust of the JSON leanfran.lock in the directory of leanfran.toml, or else in the crate. A file that is not JSON exits with 254: delete it and run cargo leanfran extract and cargo leanfran check |
Generated code of lean! |
.leanfran/cache/ |
.leanfran/rust/cache/. Change include of Cargo.toml |
Row of cargo leanfran status |
<function> and <label> <state> lines |
<function> [<label>]: <state>, and <function>: removed for a function only in the lock |
| State of an unjudged obligation | unchecked |
stale |
| Function without obligations | model only |
No row |
A Lean file in lean/ changed since check |
Not detected | stale. With [check] require = "proved", cargo build fails until cargo leanfran check runs again |
| Rust version | 1.88 | 1.88, and 1.89 for cargo-leanfran |
| Commands and options | 0.15.0 | Now |
|---|---|---|
Project directory of sync, check and status in Python, TypeScript and PHP |
--package <dir>, required in Python and TypeScript |
--dir <dir>, the working directory by default |
| Build a Lake package and generate its modules in Rust, TypeScript and Clojure | project generate, TypeScript generate |
A [[generate]] table of leanfran.toml and leanfran generate |
| Rust proof check | crate check --lake-dir <dir> |
obligations check --lake-dir <dir> |
| Clojure generation | module generate-namespace --namespace <ns> --strip-namespace <Name> |
module generate --clojure-namespace <ns> --namespace <Name> |
Clojure ns form |
--forms-only, --no-forms-only |
--forms-only |
| C generation | generate --roots a,b --out-dir <dir>, which builds the module with Lake |
lake build <Module>, then lake env leanfran --lang c module generate --roots a b --output-dir <dir> |
| C library | build --out-dir <dir> |
build --output-dir <dir> |
Go package name of module generate |
--package <name> |
--go-package <name> |
| Types defined outside the module in Rust | --type-path <Name>=<path> |
--type-module <Name>=<path> |
| Lean version of the compiler | --lang go --lean-version |
--version, which prints leanfran <version> (Lean <version>) with and without --lang |
| Usage | help, module help |
--help or -h after leanfran, after --lang <language> and after each command |
cargo leanfran --version |
cargo-leanfran <version> |
leanfran <version> (Lean <version>) |
| Arguments of a command | Rust, Python, TypeScript and Clojure accept the options of another command, and the last value of a repeated option counts | An option that the command does not list, or a repeated one with a single value, exits with 252 |
JSON of module generate |
0.15.0 | Now |
|---|---|---|
| Name of the generated module in a job | rust_path, python_path, go_path, php_namespace, ts_root |
path |
| Types defined outside the job | type_paths in Rust; type_modules with module, php_namespace, or strings |
type_modules, an array of {"namespace", "target"} |
| Python models in a job | "adopt": true in type_modules |
model_modules, an array of {"namespace", "target"} |
| A job with an unknown key | Ignored in Rust, Python, TypeScript, PHP and Go | Exits with 252 and names the job and the key |
| Summary | Counts or lists, different in each language | module, files, functions, externs, skipped, data, and error for a failed job |
| Environment variable | 0.15.0 | Now |
|---|---|---|
Automatic sync of the Python and TypeScript runtimes |
LEANFRAN_PYTHON_AUTO_SYNC, LEANFRAN_TS_AUTO_SYNC |
LEANFRAN_AUTO_SYNC |
| Clojure workspace | LEANFRAN_CLJ_WORKSPACE, the workspace |
LEANFRAN_CLOJURE_WORKSPACE, the directory of workspace/ and cache/ |
lake of Clojure deflean and Rust lean! |
lake on PATH |
LAKE, or lake on PATH |
| Exit code | 0.15.0 | Now |
|---|---|---|
--lang <language> without a command |
0, and the usage on standard output (252 in C) | 252, and the usage on standard error |
status with a failed or unextracted function |
0 without --require proved |
1 |
--modules or --module-name names a module that is not in the search path |
252 | 254 |
| A missing directory | 252 for generate --dir and the Rust --lake-dir, 255 for the C build |
254 |
leanfran generate with a failed lake build |
255 | 254 |
leanfran generate with an invalid leanfran.toml |
252 | 254 |
Several failed --batch jobs |
The first code in Python, TypeScript, PHP and Go | The largest code |
| An error such as an output file that cannot be written, in Clojure | 254 | 255 |
cargo leanfran with invalid arguments |
2 | 252 |
cargo leanfran with an error in the project or the environment |
1 | 254 or 255 |
cargo leanfran status with a stale, failed, unextracted or removed function |
0 | 1 |
Messages start with leanfran: in every language, where 0.15.0 writes leanfran --lang <language>: , leanfran-go: , cargo leanfran: and others. Errors with a position start with <file>:<line>:<column>: error: , also for a function that cannot be translated (error: unextracted <id>: <message>).
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=PythonSee CONTRIBUTING.md for build targets, formatting, packaging and the coordinated release process. Implementation notes live in docs/; maintenance tools live in maint/.