Liking cljdoc? Tell your friends :D

leanfran — prove in Lean, run in your language

Write it in Lean. Prove it in Lean. Run it in your language.

Latest release CI License: Apache-2.0 Lean v4.35.0 GitHub stars

Rust on crates.io Python on PyPI TypeScript on npm Clojure on Clojars PHP on Packagist Go on pkg.go.dev C from Git tags

Quick look | Get started | Languages | Features | Documentation & examples | Contribution

leanfran

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.

Quick look

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.

Lean at the core

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.

LanguageStart herePackageIntegration
RustRust quick startleanfran on crates.iolean!, Rust types, Cargo build integration
PythonPython quick startleanfran on PyPIlean(...), pydantic models, Python modules
TypeScriptTypeScript quick startleanfran on npmlean(...), TypeScript types, Standard Schema
ClojureClojure quick startio.github.conao3/leanfran on Clojarsdeflean, Clojure values, JVM namespaces
PHPPHP quick startconao3/leanfran on Packagist#[Lean], #[Model], leanfran --lang php sync
GoGo quick startleanfran-go on pkg.go.devgo get github.com/conao3/leanfran-go, embedded Lean, Go module tool
CC quick startGit tags, Nix flake output #leanfranleanfran:lean comments, C headers, pkg-config, Make and CMake

What you can build

  • 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.

Get started

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.

Embedded Lean blocks

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:

CommandEffect
leanfran --lang <language> syncBuilds every block with Lake, generates the host-language files, and records them in leanfran.manifest.json.
leanfran --lang <language> checkRuns sync, translates the functions bound to Lean specifications, checks their proofs, and records the results in leanfran.lock.
leanfran --lang <language> statusCompares 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 codeMeaning
0Success.
1status printed a stale, missing, or removed line, check found an unextracted or failed obligation, or an obligation is not proved under --require proved.
252An argument is invalid.
254The 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.
255The 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 from 0.14.0

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.

LanguageRun onceCommit
Pythonleanfran --lang python sync --package <dir> for each package, then check for a package with @model functionsleanfran.manifest.json, .leanfran/.gitignore, leanfran.lock
TypeScriptleanfran --lang typescript sync --package <dir>, then check for a package with model(...) functionsthe deletion of .leanfran/blocks.json, leanfran.manifest.json, .leanfran/.gitignore, leanfran.lock
PHPleanfran --lang php sync, then check for a project with #[Model] functionsthe deletion of each output directory's leanfran.json, leanfran.manifest.json, .leanfran/.gitignore, leanfran.lock
Goleanfran --lang go sync, then check for a module with //leanfran:model functionsthe deletion of each package's leanfran.json, leanfran.manifest.json, .leanfran/.gitignore, leanfran.lock
Cleanfran --lang c sync, then check for a project with LF_MODEL functionsleanfran.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.

Documentation

LanguageQuick startFull guideExamples
RustSetup and first blockTypes, verification and CLIRust samples
PythonSetup and first blockModels, verification and CLIPython samples
TypeScriptSetup and first blockSchemas, verification and CLITypeScript samples
ClojureSetup and first blockTypes, verification and CLIClojure samples
PHPSetup and first blockEmbedding, types, verification and CLIPHP samples
GoSetup and first blockEmbedding, types, verification and CLIGo samples
CSetup and generationEmbedding, types, verification and linkingC samples

Contribution

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.

Development

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/.

License

Apache-2.0.

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

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close