inlean
Lean 4 inside your Rust source: proofs are checked by cargo build, and functions compile to
plain Rust.
lean!
lean! is a proc macro. During cargo build the real lean binary checks the block, proofs
included, and the macro translates Lean's compiled form of midpoint into Rust: main calls an
ordinary fn midpoint(lo: u32, hi: u32) -> u32. @[export midpoint] gives the function its
Rust name. Each theorem is a claim that Lean must prove for every pair of u32 values with
lo <= hi, not for the pairs a test happens to try. The proof is what follows by: grind is
one of Lean's automatic tactics, told here to unfold midpoint.
Write the midpoint the usual way, (lo + hi) / 2, and the first theorem is false: the sum wraps
around past u32::MAX, so lo = hi = 2_147_483_648 gives 0. The build stops at that proof, in
the Rust file:
error: `grind` failed
case grind.1
lo hi : UInt32
h : lo ≤ hi
h_1 : ¬lo ≤ midpoint lo hi
h_2 : lo + hi = (lo + hi) / 2
⊢ False
...
--> src/main.rs:6:7
|
6 | grind [midpoint]
| ^^^^^^^^^^^^^^^^
The ... stands for about a hundred lines in which grind lists what it derived before it gave
up.
Why
A proof with the overhead of a unit test. The theorem sits next to the function, in the
Rust file, and cargo build has Lean's own kernel check it; nothing reimplements Lean. You add
one dependency: no build script, no second project, no command to run, no configuration.
A proof that does not hold is a compile error. So is a sorry (Lean's placeholder for a
missing proof), an axiom beyond Lean's standard three, and native_decide, which trusts
compiled code instead of the kernel. Each is reported with its line and column in the Rust
file.
What comes out is ordinary Rust. Nothing of Lean is linked or shipped: no C toolchain, no
runtime to start. A block over fixed-width scalars expands to safe Rust; any other block calls
a small runtime written in Rust. inlean and its runtime have no third-party dependencies. A
call that takes a u64 and returns it costs 0.4 ns on an Apple M1 Pro
(measurements).
Rust types in, Rust types out. Values cross as u32, String, Vec<T>, Option,
Result, tuples and HashMap. Lean structures and inductive types become Rust structs and
enums, with their doc comments, derives and generic parameters. An argument is passed by
value, which moves a string or byte array in without a copy, or by reference.
Your Rust takes part in the Lean code. A crate's own type can stand for a Lean type, such
as ruint's U256 for BitVec 256. An opaque type carries any Rust value through Lean
unconverted, and adds nothing to what is trusted. A Lean declaration can run a Rust function
of the crate, with its Lean body as the model that proofs use. Rust's own Vec, VecDeque,
HashMap and HashSet are usable inside Lean, so a theorem can be about code that changes a
Rust HashMap in place.
Only the author needs Lean. Commit the results to .inlean/, and the crate builds with no
Lean downstream, on docs.rs and offline. CI checks the committed results against Lean.
What is trusted is written down. Your definitions are verified. Lean's compiler and inlean's
translator and runtime are trusted, and tested against real Lean on roughly 35,000 results.
Anything more a crate trusts is one named line in its Cargo.toml, and appears in the
documentation of every exported function that can reach it.
A tour
Rust types in, Rust types out
lean!;
Line is a pub struct with fields item: String, quantity: u32 and unit_price: u64.
OrderError is a pub enum with variants Empty, NoQuantity { item } and
TooLarge { total }, where total is an inlean::Nat, Lean's unbounded natural number.
Except is Result. The first call passes a slice, which is copied; the second gives up a
Vec, and its strings move into Lean without a copy.
Proofs about Rust's own collections
use HashMap;
lean!;
With import Inlean, a HashMap in the block is the standard library's HashMap, held by
Lean as it is and changed by Rust's own code, with nothing converted or copied at the boundary.
Proofs see a model of the map in Lean, whose simp lemmas make restock_get a one-line proof.
Each instantiate line names the element types a collection is used at.
Collections has the
operations, the models and what can be proved.
Rust functions inside Lean
/// `a * b / c` in 128 bits; `None` when `c` is zero or the quotient needs more than 64 bits.
lean!;
[]
= "crate::mul_div"
@[extern] marks a declaration whose code comes from elsewhere. The line in Cargo.toml says
it is mul_div, and the Rust generated for quote calls it. The body of mulDiv is its model:
quote_zero is proved about the model, and holds for the program if mul_div returns what the
model returns. Nothing can check that, so the crate says it by name, and so do the docs of
quote:
Trusted, not checked: `crate::mul_div` runs for `mulDiv`.
An @[extern] declaration that Cargo.toml does not name fails the build. Rust functions in a
block has the rules
and how to test such a function against its model. Such a declaration can also take a Lean
function, which the Rust function receives as a closure, and be generic in a type (functions
as arguments).
The collections build fold, any, all, filter, map and find? on it (operations that
take a function).
Your own types
A Rust type of the crate takes part in a block at one of three levels, each named in a table of
Cargo.toml:
| Level | What Lean code does with the value | What the crate trusts |
|---|---|---|
| primitive | computes with it as a Lean type, such as ruint's U256 as BitVec 256, converted at each crossing |
that its conversions are total, and a round trip returns the same value |
| opaque | carries it unconverted, and cannot look inside or make one | nothing about the type |
| foreign | carries it unconverted; Rust functions operate on it, and proofs use a Lean type of the block as its model | that each of those functions matches its model |
Choosing a level
compares them, and compares the collections of import Inlean with Lean's own.
How it works
- The macro recovers the exact text of the block, so that Lean's positions map back to lines and columns of the Rust file.
- It runs Lean 4.36.0 on the block, which checks every proof and compiles every definition. A small driver reports Lean's messages, the exports and their signatures, the compiler's intermediate representation of everything they reach, and what breaks the trust policy.
- inlean fails the build on anything the policy rejects, and translates the intermediate representation into Rust that takes the place of the block.
- The result is cached by the block's text.
A block that Lean has not seen costs about 1.2 s of build time; an unchanged block costs
nothing on rebuild. Under rust-analyzer the macro never runs Lean and answers from the cache:
a block resolves in the editor once cargo check has compiled it and the file that holds it
is next edited. Until then its functions are unresolved, and the macro reports no error.
DESIGN.md has the details.
Getting started
inlean needs Rust 1.88 or later and a macOS or Linux build host, x86-64 or ARM64. It needs Lean
4.36.0, exactly that release, only when a block has no committed or cached result. The macro
looks for it in the directory named by INLEAN_LEAN_TOOLCHAIN, then in elan (Lean's toolchain
manager), then in its own cache. If none has it, the macro says so on stderr and downloads the
official release once per machine: 638 MB, 2.3 GB on disk, about a minute, checked against its
published SHA-256. To avoid the download, run elan toolchain install leanprover/lean4:v4.36.0
first. To forbid it, set INLEAN_OFFLINE=1; a block that needs Lean then fails with an error
that says how to install it.
A block takes one of three forms:
| Form | Use it for |
|---|---|
lean! { ... } |
short definitions in plain ASCII; the block must lex as Rust tokens |
lean!(r#" ... "#) |
any Lean text, with Unicode (∀, ≤, ⟨⟩) or primes (x') |
lean_file!("src/proofs.lean") |
real proofs, with Lean's own editor support: goals, hovers, errors as you type |
The editor support needs a lean-toolchain file containing leanprover/lean4:v4.36.0 in or
above the file's directory. The path is relative to Cargo.toml, and a change to the file
rebuilds the crate.
To build without Lean downstream, commit what Lean said about each block:
INLEAN_VENDOR=1 INLEAN_VENDOR=check
Building covers the toolchain, committed results for a published crate, and the environment variables.
What is proved, and what is trusted
A theorem in a block is a theorem about your definitions, checked by Lean's kernel. What turns
them into the Rust that runs is trusted: Lean's compiler, inlean's translator and runtime, the
conversions at the boundary, and the collections of import Inlean, which are trusted to match
their models in Lean. Lean's own native code has the same shape of trust, through Lean's
compiler, a C emitter and a C runtime. Tests compare the generated code with real Lean on
roughly 35,000 results and check that calls leave no live objects behind; the runtime's tests
also run under Miri, which checks its unsafe code.
A crate can trust more, for itself only: each entry under [package.metadata.inlean.trusted]
has a Rust function run in place of a Lean declaration. Without such an entry, a crate trusts
nothing beyond the above. What is proved, and what is
trusted lists what fails the
build,
and principle 3
says why.
Status
inlean is an early prototype. It is for Lean written as part of a Rust program; running existing Lean programs is not the goal. Lean code in a block is pure: there is no IO yet, no thunks, and no type indexed by a value crosses the boundary. Each block is a Lean file of its own, and blocks do not see each other's definitions. A missing runtime primitive or an unsupported boundary type fails the build and names what is missing.
CI runs the full test suite on macOS and Linux, each on ARM64 and x86-64, with stable Rust, and also with Rust 1.88 on macOS ARM64 and Linux x86-64. Windows is not supported. Limits and known gaps say what else is missing.
Documentation
- The guide: writing a block, types at the boundary, your own types, opaque types, Rust functions in a block, foreign types, collections, choosing a level, what is proved, and what is trusted, building.
- The principles: what a user of inlean gets, in five outcomes, and how trade-offs between them are decided.
- DESIGN.md: how the pieces work, the measurements, and the known gaps.
- The changelog: what changed in each release, and how to upgrade.
License
MIT OR Apache-2.0. inlean-runtime contains functions ported from the runtime of Lean 4
(lean.h), which is under the Apache License 2.0; see its
NOTICE.