Lean: Programs and Proofs
A program can calculate the balance after a series of transactions. A proof can establish that the calculation preserves an account invariant under stated conditions. In Lean, both use the same language: you write a function, state its contract as a type, and construct a proof of that contract.
You will begin with expressions and small functions, learn to state and prove properties, and finish with a command-line application whose central operation has a checked specification. By the end, you should be able to start a project, read library types, implement a feature, prove ordinary properties, diagnose failures, and build an executable.
Audience and prerequisites
You should already be comfortable writing programs in at least one language. Familiarity with functions, lists, branching, and command-line tools is sufficient. The book introduces functional programming, formal logic, and dependent types as needed.
The explanations are written for engineers reading English at CEFR C1 and define technical terms when they first appear.
You do not need university mathematics to follow the main route. The Mathlib chapter introduces the conventions needed to start using an existing mathematical library. Research mathematics, compiler internals, and advanced tactic implementation are separate areas of study.
What you will build
The running project is a small account model. A withdrawal either returns a new balance or an explicit failure. The final application accepts a balance and an amount, validates the input, performs the operation, and prints the result.
The central theorem shows that a successful withdrawal accounts for the entire original balance. Another theorem states when withdrawal succeeds.
The application is small enough to inspect each layer, from the input boundary and pure operation to the specification, proof, executable, and tests. The same separation applies to larger tools.
How to use the book
Read Parts I and II in order. In Part III, implement the examples before reading their proofs. Part IV combines the techniques into a project workflow.
Attempt each exercise in a separate file, then compare your solution with the answer chapter. A compiling proof with a different structure is a valid answer.
Use the Lean InfoView throughout. When a tactic fails, read the goal and local hypotheses before editing the script. When an expression fails, compare its expected and inferred types.
The checked examples target Lean 4.35.0, provided by this repository’s pinned Nix environment. All lean fences in the main chapters are checked together within each chapter, with import Std and a fresh chapter namespace. Shell transcripts and intentionally incomplete exercises use text or sh fences.
The downloadable workbook project contains the capstone as ordinary source files. Mathlib has its own toolchain; its separate verification instructions appear in the Mathlib chapter.
Learning milestones
The program computes a result, and the proof shows that the result satisfies its contract.
| After | You should be able to do this without copying a solution |
|---|---|
| Part I | Model an input with inductive types, write total recursive functions, and inspect their types and results |
| Part II | Translate a simple contract into a proposition and prove it with terms, rewriting, cases, or induction |
| Part III | Use type classes and effects, design validated data, and connect an implementation to a specification |
| Part IV | Maintain a Lake project, navigate dependencies, build a CLI, review trust assumptions, and diagnose failures |
The tutorial draws on the source books and provides its own sequence, explanations, examples, and exercises. Sources and coverage identifies the upstream material and explains how the programming and proof material are integrated.