
How Lean started
In 2013, Leonardo de Moura started the Lean project at Microsoft Research. The idea was to combine two things that are difficult to get together. You want powerful automation to help you find proofs, and you want a small, carefully designed program to check that those proofs are valid. Lean calls that small program the kernel. The first public release, Lean 0.1, arrived in June 2014.
Lean 3 arrived in 2017 and gained traction among mathematicians. One reason was that users could write their own tactics, commands that automate proof steps. They could also share definitions and theorems through Mathlib, an open-source library of formalized mathematics. If you needed a theorem someone had already proved, you could import it and use it as a building block.
Development of Lean 4 began in 2018 with de Moura and Sebastian Ullrich. The first prerelease came in 2021, and Lean 4.0 was officially released in September 2023. Much of Lean 4, including its compiler, proof tooling, and build system, is written in Lean itself. That matters because the language you use to model a system is also the language you can use to extend the tools that help you reason about it.

What Lean 4 actually does
Lean 4 is both a functional programming language and an interactive theorem prover. You can write a function that computes a result, then write a theorem that states a property of that function. Lean checks the proof you supply.
Imagine you’re reviewing a vault. The developer says, “A withdrawal always reduces the user’s balance by exactly the amount paid.” That sounds precise until you ask what happens when the user requests more than their balance. Does the operation reject the request? Does it pay what’s available? Does it return success with a zero payout? The sentence leaves those cases open.
Writing the rule in Lean forces you to decide what balance, amount paid, and successful withdrawal mean. Once you’ve made those decisions, you can state a claim that applies to every input covered by your model.
A worked example from vault accounting
We’ll model one account and one withdrawal. Amounts are whole units, such as a token’s smallest denomination. A valid withdrawal returns the remaining balance. An oversized request returns none, which represents rejection. For this example, withdrawing zero is allowed.
| Starting balance | Requested amount | Result |
|---|---|---|
| 100 | 30 | Accepted, 70 remains |
| 100 | 100 | Accepted, 0 remains |
| 100 | 101 | Rejected |
| 100 | 0 | Accepted, 100 remains |
Here’s the Lean definition and the first proof.
import Lean
def withdraw (balance amount : Nat) : Option Nat :=
if amount ≤ balance then
some (balance - amount)
else
none
#eval withdraw 100 30 -- some 70
#eval withdraw 100 100 -- some 0
#eval withdraw 100 101 -- none
theorem withdraw_preserves_balance
(balance amount remaining : Nat)
(h : withdraw balance amount = some remaining) :
remaining + amount = balance := by
by_cases enough : amount ≤ balance
· have hRemaining : balance - amount = remaining := by
simpa [withdraw, enough] using h
calc
remaining + amount = (balance - amount) + amount := by
rw [hRemaining]
_ = balance := Nat.sub_add_cancel enough
· simp [withdraw, enough] at h
theorem withdraw_rejects_excess
(balance amount : Nat)
(tooMuch : balance < amount) :
withdraw balance amount = none := by
simp [withdraw, Nat.not_le_of_lt tooMuch]
The snippets were reviewed against Lean’s syntax and documentation. I didn’t run them through a Lean compiler here.
Read the code like an auditor
Nat means natural numbers: 0, 1, 2, and so on. One detail matters immediately. In Lean, (30 : Nat) - 50 evaluates to 0. Natural-number subtraction stops at zero. If you remove the if and always return some (balance - amount), a withdrawal of 50 from a balance of 30 would return some 0 and be treated as successful in this model. The balance check is where we encode the rejection rule.
Option Nat gives us two distinct outcomes. some 0 means the withdrawal succeeded and the account is now empty. none means the request was rejected. Using plain Nat and returning 0 for both outcomes would lose that distinction.
#eval runs a specific example. Evaluating withdraw 100 30 shows what the function does for those two numbers. The theorem is written with arbitrary balance, amount, and remaining values. Its claim applies whenever the success hypothesis h holds.

Walk through the proof
The first theorem says that if the withdrawal succeeds, the remaining balance plus the withdrawn amount equals the starting balance. The line beginning with h : supplies the condition for applying the theorem. The line before := by is the conclusion we have to establish.
The proof splits on amount ≤ balance.
- When the account has enough funds, Lean unfolds
withdrawand simplifies the success hypothesis. Fromsome (balance - amount) = some remaining, it getsbalance - amount = remaining. Thecalcblock then replacesremainingwithbalance - amountand appliesNat.sub_add_cancel. That arithmetic theorem needs the very fact we have in this branch:amount ≤ balance. - When the account has insufficient funds,
withdrawevaluates tonone. The hypothesis still says it returnedsome remaining. Those are different constructors ofOption, so the hypothesis is contradictory and the branch closes.
The second theorem states the rejection rule directly. If balance < amount, the condition amount ≤ balance is false, so the function returns none.
There’s a specification lesson hidden here. Imagine replacing the function with one that always returns none. Both theorem statements would still hold. The first starts with an impossible success hypothesis, and the second only demands rejection for oversized withdrawals. We need another property to guarantee that valid requests succeed.
theorem withdraw_accepts_valid
(balance amount : Nat)
(enough : amount ≤ balance) :
withdraw balance amount = some (balance - amount) := by
simp [withdraw, enough]
That’s one of the most useful questions to ask when reviewing formal verification work: What incorrect implementation could still satisfy these theorems? Here, that question exposed a missing requirement even though the two original claims were mathematically sound.
What happens when Lean checks a proof?
Take the hypothesis h : withdraw balance amount = some remaining. In Lean, the equality to the right of : is a proposition, and h is evidence of that proposition. A theorem is a declaration whose value is a proof. This is the idea behind propositions as types. A proof is a term whose type is the proposition it proves.
The commands inside by help construct that term. by_cases creates two goals, simp simplifies expressions using known rules, and rw rewrites an expression using an equality. The tactics can do substantial work, but the kernel checks the resulting proof term. If a tactic fails, you still have an open goal or an error to resolve. If it produces an invalid proof term, the kernel rejects it.
Lean also supports dependent types, where a value can appear inside a type. For instance, you can define a withdrawal request whose type depends on the balance and requires evidence that the requested amount is within it.
structure ValidWithdrawal (balance : Nat) where
amount : Nat
withinBalance : amount ≤ balance
A function that accepts ValidWithdrawal 100 receives both an amount and evidence that the amount is at most 100. The earlier withdraw function is a good fit for raw input because it can reject a request. A type like ValidWithdrawal is useful inside the program after validation, where later functions can reuse the evidence.
How this carries over to a real protocol
Our theorem is about a one-account model. amount represents the intended payout, and the function computes the remaining balance. A deployed vault has more moving parts. It updates storage, transfers a token, may charge fees, and can call another contract. For a fee-on-transfer token, the amount debited from the vault and the amount received by the user can differ. A useful theorem has to say which quantity it tracks and how that quantity is measured.
Suppose the actual question is whether the vault can cover what it owes depositors. For a simple vault with one asset and no outstanding loans, you might start with this invariant:\[ \text{Total user credits} \leq \text{Assets held by the vault} \]
Now the work has four parts.
- Define the state. Say how user credits, vault assets, and any pending withdrawals are represented. Decide which balances count toward each side of the inequality.
- Define the state transitions. Model deposits, withdrawals, fees, and any privileged operation that can change the balances. Include the external call behavior the guarantee depends on.
- Prove preservation. Show that the invariant holds in the initial state and that every allowed transition preserves it. Induction then extends the guarantee to every finite sequence of those modeled transitions.
- Connect it to the deployed implementation. Establish how Solidity storage, token calls, and execution correspond to the model. If you prove the theorem over an abstract accounting model, the correspondence is a separate obligation. You can also reason directly over a formal model of execution, with scope tied to the modeled semantics and bytecode.
A real project tackling that execution layer is Nethermind’s EVMYulLean. It defines executable models of the EVM and Yul in Lean 4. The project gives concrete meaning to questions like “what state does this instruction produce?” and runs the model against Ethereum conformance tests. Nethermind described cases where behavior expected by the tests differed from the Yellow Paper, which shows why validating the execution model itself matters before using it to reason about contracts.
Where Lean fits alongside Foundry
| Method | Question it answers |
|---|---|
| Unit test | If this account withdraws 30 from 100, does the implementation leave 70? |
| Fuzz or invariant test | Across the inputs and transaction sequences the test runner explores, can it find a violation? |
| Lean proof | Does the exact property follow for every case covered by the formal model and its assumptions? |
These methods can reinforce each other. A Foundry test can exercise actual Solidity and surface a token behavior the model missed. A Lean proof can cover arbitrarily long sequences of modeled transitions once you’ve proved the invariant is preserved. Reviewing the specification ties those two kinds of evidence together.