A test tells you that a program worked on the inputs you tried. A proof tells you that it works on every input. For most code, tests are enough. For a compiler, a cryptographic library, a distributed protocol or a theorem in a research paper, “it worked on the cases I tried” leaves a gap, and bugs live in that gap. Edsger Dijkstra put it in one line in 1970: testing can show the presence of bugs, but never their absence (EWD 249).
This course is about closing the gap with a machine: writing the claim and its proof in a language precise enough that a small program can check the proof, step by step, with nothing left to the reader’s goodwill. The tool is Lean 4. This lecture sets the scene. It explains what formal verification buys and what it costs, where Lean sits among the alternatives, why you can trust its verdict, how to install it, and how the lectures, notebooks and cheatsheet fit together.
Notebook and cheatsheet
Notebook 0: First proofs shows how the notebooks talk to Lean, then runs your first
#eval,#checkand theorem, and reads your first error. Cheatsheet: Installing Lean and starting a project, Commands, Axioms and the trusted base, unknown identifier.
Why verify at all
Two stories show the stakes, one from software and one from mathematics.
Compilers are among the most heavily tested programs in existence. In 2011, Yang, Chen, Eide and Regehr built Csmith, a generator of random valid C programs, and fed its output to every C compiler they could get. Over three years they reported more than 325 previously unknown bugs, and every compiler they tested could be made to crash and to silently produce wrong code (PLDI 2011). The exception was CompCert, a C compiler whose compilation passes are proved correct in the Rocq prover (compcert.org). Csmith found bugs in CompCert’s unproved front end, but none of the middle-end wrong-code bugs it found everywhere else. The proof did not make CompCert perfect. It made the proved part free of the kind of bug that testing kept finding.
Mathematics has the same problem in a different form. In 1998 Samuel Ferguson and Thomas Hales proved the Kepler conjecture on the densest packing of spheres. The proof relied on long computer calculations, and it was eventually published without complete certification from the referees. In 2003 Hales started the Flyspeck project to formalize the whole proof; its last checks ran in August 2014, and its account appeared in 2017 (Forum of Mathematics, Pi). Since then the scale has grown fast. On 2026-09-04 Anthropic announced a complete Lean proof of Fermat’s Last Theorem, produced in 11 days by Claude agents with occasional high-level human instructions. Kevin Buzzard compiled it and ran an independent check himself (VERIFIED; announcement, Xena blog).
A machine check settles one question exactly. Here is the vocabulary for that question.
Definition 0.1 (Formal verification).A specification is a precise mathematical statement of what should be true: “this function returns a sorted permutation of its input”, or “there are no positive integers with
for ”. Formal verification is producing a proof that a system or a mathematical object meets its specification, in a form a machine can check.
The definition of formal verification (Definition 0.1) has two halves, and the machine checks only the second. Nothing checks that the specification says what you meant. That weakness is not hypothetical. On 2026-09-08 OpenAI claimed a Lean-checked result on blow-up for the Navier–Stokes equations with a smooth external force. On 2026-10-06 Bastounis, Circelli and Hansen showed that the Lean statements are weaker than the English claims in at least two places (arXiv 2610.08144), so the result is DISPUTED. The Lean code may well be correct; the question is what it proves. Lecture 5 turns reading a statement into a routine, and Lecture 13 sets out both sides of this case.
The spectrum of assurance
Formal verification is one end of a spectrum. Each step along it checks more cases and costs more human effort per property, and each leaves something different to trust.
Testing runs the program on inputs someone chose. It is cheap, it finds most bugs, and it says nothing about the inputs nobody chose.
Property-based testing states a property (“reversing a list twice gives the list back”) and checks it on hundreds of random inputs. QuickCheck introduced the style for Haskell (Claessen and Hughes, 2000); Hypothesis brings it to Python (docs). The property is a specification, which is progress, but the check is still a sample.
Model checking describes a system as a finite state machine and explores every reachable state. Engineers at Amazon Web Services used TLA+ and its model checker TLC on the designs of DynamoDB and S3, and found bugs that had survived design reviews, code reviews and testing; the shortest trace for one DynamoDB bug was 35 steps long (Newcombe et al., 2015). The check is exhaustive, but only for the model, and only for the instance size you can afford to explore. The same paper is frank that it does not tell you whether the code matches the model.
Auto-active verifiers let you annotate real code with pre- and postconditions and loop invariants, then hand the resulting proof obligations to an SMT solver (an automatic prover for logic plus arithmetic, arrays and similar theories, such as Z3). Dafny is a language built around this, and Verus does it for Rust. A verified function holds for all inputs. The cost is writing the annotations; the risk is that when the solver gives up you get little help, and you trust the solver and the translation of your program into its logic.
Interactive provers ask you to write the proof itself, with as much automation as you can get, as a term in a rich logic, and then re-check that term with a small kernel. Lean, the Rocq prover (formerly Coq) and Isabelle are the main ones. They can state anything mathematics can state, including the correctness of a whole operating-system kernel (seL4, in Isabelle/HOL). The cost is effort: you prove what an SMT solver would try to find for you, and much more.
Remark 0.1 (The cost of assurance).No row of the figure dominates. A verified sorting function with a wrong specification is worse than a well-tested one with no specification, because it carries false confidence. Industrial projects mix the rows: the team behind AWS’s Cedar authorization language reports keeping a Lean model with proofs next to differential and property-based tests of the Rust implementation (SELF-REPORTED; arXiv 2407.01688; Lecture 13).
The cost of assurance (Remark 0.1) is why this course spends as much time on stating things correctly as on proving them.
What Lean is, and why you can trust it
Lean 4 is two things at once. It is a functional programming language: you can define functions, run them, and compile them to fast native code. And it is a proof assistant: the same language states theorems and writes proofs (de Moura and Ullrich, 2021). This is not a coincidence of packaging. Lecture 1 explains that a proof is a program and a statement is its type, so a type checker for programs is already a proof checker.
Here is Lean as a programming language. #eval runs an expression and #check asks for its
type; the comments show what Lean prints.
#eval 2 + 3 * 4 -- 14
#eval "Hello, " ++ "Lean" -- "Hello, Lean"
#check 2 + 3 * 4 -- 2 + 3 * 4 : Nat
def square (n : Nat) : Nat := n * n
#eval square 12 -- 144
#check square -- square (n : Nat) : NatAnd here is the difference between testing and proving, in two lines. The first line checks a
property on a thousand inputs. The second proves it for every natural number, using omega, a
decision procedure for linear arithmetic that ships with Lean.
#eval (List.range 1000).all (fun n => 2 * n == n + n) -- true
theorem two_mul_eq (n : Nat) : 2 * n = n + n := by omegaA false claim is rejected, not merely left unproved:
theorem square_three : square 3 = 10 := by decideLean reports Tactic decide proved that the proposition square 3 = 10 is false: the
tactic computed square 3 to 9 and compared.
Why believe Lean when it says yes? Because of how the work is split. Everything clever (tactics
such as omega, the automation that fills in details, and today AI models) runs outside the
part you trust. What these produce is a single, fully explicit proof term, and Lean’s kernel
re-checks that term from scratch with a short list of typing rules. This design is the
de Bruijn criterion, described in Lecture 1.
Definition 0.2 (Trusted computing base).The trusted computing base of a check is everything that must be correct for its verdict to be right. For a Lean proof it is the kernel, the axioms the proof uses, the computer that runs the kernel, and the statement itself, as Lean read it from your file.
The trusted computing base (Definition 0.2) is small but not empty. #print axioms lists the
axioms a theorem depends on, and independent re-implementations of the kernel can re-check a
whole project. Lecture 2 opens the kernel and shows both.
On top of the core language sits Mathlib, a single library of formalized mathematics maintained by a large community, from basic algebra and analysis to research-level number theory (the Mathlib paper, 2020). Most of Part II uses it. The code on this page needs only core Lean.
Setting up
You need three things: Lean itself, an editor that talks to it, and a project.
-
Lean and elan. The recommended route is to install VS Code and the Lean 4 extension, then follow the extension’s setup guide, as the official install page describes. The guide installs elan, the Lean version manager. Each project names its Lean version in a file
lean-toolchain, and elan downloads that version on first use, so different projects can use different versions side by side. -
A project with Mathlib. Lean code lives in a project managed by Lake, Lean’s build tool. From a terminal (or from the ∀ menu in VS Code, “New Project”):
lake new my_project math # a new project that depends on Mathlib cd my_project lake exe cache get # download Mathlib already compiledlake new … mathwrites the project files and checks out Mathlib’s source.lake exe cache getthen downloads Mathlib’s compiled files, a few gigabytes; without it, your machine would spend hours compiling Mathlib itself. The Mathlib community’s project guide covers the details and the usual problems. -
The infoview. Open a
.leanfile in VS Code and Lean checks it as you type. The infoview panel on the right shows, at the cursor, the current goal: the hypotheses you have, a turnstile⊢, and what remains to prove. Reading the infoview is most of the skill of writing proofs; Lecture 3 practises it.
Your first error will probably look like this one, from a typo:
#eval squre 12Lean reports Unknown identifier squre. The course’s Lean project turns off a feature called
autoImplicit, which would otherwise treat some unknown names in a statement as new variables
and hide the typo; every unknown name is an error here. When a name that should exist is
unknown, the usual causes are a typo, a missing import, or a missing open of a namespace.
How this course works
Each lecture comes with up to two companions.
- A notebook runs the lecture’s examples, and more, against a real Lean process. The
notebooks are Jupyter notebooks with a Python kernel. Their first cell starts one long-lived
Lean process (the Lean REPL, a program that
takes Lean commands as JSON on its input) and loads the imports once. Each later cell marked
%%leanis sent to that process and builds on the cells before it, and the reply is drawn as a coloured box: accepted, accepted with asorryleft open, or rejected. The notebook pages on this site were executed when the site was built, so every box shows what Lean actually printed. To run them yourself, see “Running the notebooks” on the home page. - The cheatsheet has one card per concept, tactic or common error. Each card opens into short notes with compiled examples, and each lecture links the cards it uses at the top.
Every Lean snippet on the lecture pages is compiled when the site is built, and every message quoted in the prose was copied from a real run. If a snippet here fails on your machine, the likely cause is a different Lean or Mathlib version; this course uses Lean 4.35.0 and Mathlib v4.35.0.
The sixteen lectures fall into five parts:
| part | lectures | the question it answers |
|---|---|---|
| I. How Lean works | 0 to 4 | Why does Lean’s “yes” mean that a proof exists? |
| II. How to formalize anything | 5 to 7 | How do you turn a subject into definitions and theorems you can trust? |
| III. Verifying programs and systems | 8 to 10 | How do you state and prove that code does what it should? |
| IV. Automation, AI and the frontier | 11 to 14 | What can automation do, and what has really been verified? |
| V. Lean for theoretical computer science | 15 | How do you formalize models of computation, and what do the theorems then mean? |
Part I builds the mechanism: propositions as types, the kernel and its type theory, tactics and induction. Part II is the method: a recipe for formalizing, how Mathlib is organized, and a case study in quantum information. Part III turns to programs, from verified functional programs through semantics and compilers to systems and hardware models. Part IV covers metaprogramming, AI provers, a sourced survey of recent applications in which every claim is tagged VERIFIED, SELF-REPORTED or DISPUTED, and a capstone in which you plan a formalization of your own. Part V applies everything to theoretical computer science: query complexity, error-correcting codes, the cost of algorithms, RSA and NP reductions, adapted from a Berkeley course.
Try this
- Install Lean with the VS Code extension, create a project with
lake new my_project math, and runlake exe cache get. Open the fileMyProject/Basic.lean, type#eval 2 ^ 100, and read the answer in the infoview. - Paste the
two_mul_eqtheorem into your file and change2 * nto3 * n. Read whatomegareports, then fix the statement so that it is true again. - In Notebook 0, prove
square 4 = 16twice: once withrfland once withby decide. Then state something false aboutsquareand read the error. - For one program or theorem you care about, place it on the spectrum of assurance: which row would you choose, and what would you still have to trust?
Readings
- The official install page and the Mathlib community’s project guide.
- Avigad, de Moura, Kong and Ullrich, Theorem Proving in Lean 4. The standard introduction; chapters 1 to 3 go with Lectures 0 to 2.
- Christiansen, Functional Programming in Lean. Lean as a programming language.
- Avigad, Buzzard, Lewis and Massot, Mathematics in Lean. Lean with Mathlib, for mathematicians; Part II follows it.
- Newcombe et al., How Amazon Web Services uses formal methods, Communications of the ACM 58(4), 2015. Model checking in industry, honestly assessed.
- Yang, Chen, Eide and Regehr, Finding and understanding bugs in C compilers, PLDI 2011. What testing finds, and what a proof prevented.