Skip to content

About

Write and prove functions in Lean 4, run them as Rust, Python, TypeScript and Clojure

Topics

Resources

Contributing

Stars

44 stars

Watchers

0 watching

Forks

Latest commit

 

History

311 Commits

Folders and files

Repository files navigation

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

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

leanfran

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.

Quick look

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.

Lean at the core

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 conao3/leanfran on Packagist #[Lean], #[Model], Composer CLI

What you can build

  • Proved functions in your application. Keep an algorithm and its correctness proof together, then call the generated function from your application.
  • 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, or PHP 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.

Get started

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.

Documentation

Language Quick start Full guide Examples
Rust Setup and first block Types, verification and CLI Rust samples
Python Setup and first block Models, verification and CLI Python samples
TypeScript Setup and first block Schemas, verification and CLI TypeScript samples
Clojure Setup and first block Types, verification and CLI Clojure samples
PHP Setup and first block Embedding, types, verification and CLI PHP 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/. 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/.

License

Apache-2.0.

About

Write and prove functions in Lean 4, run them as Rust, Python, TypeScript and Clojure

Topics

Resources

Contributing

Stars

44 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages