h5i

MiniLean4

Implementing an Interactive Theorem Prover from Scratch

a Lean 4-style proof kernel, built from nothing. five kinds of terms and eight typing rules to start; inductive types, a tactic language, and the universe hierarchy that keeps it all consistent by the end. every piece is tied to a small Rust implementation you can read in an afternoon.

proofs are programs. the kernel checks them. nothing else is trusted.

# the polymorphic identity on propositions: for every P, P -> P.
goal forall (P : Sort 0), P -> P
intro P
intro p
exact p
qed

this is the real kernel, compiled to webassembly, checking the first theorem of the book. edit the script and run it again.

What this book is

Interactive theorem provers such as Lean, Coq, and Agda rest on a small core: a kernel that decides whether a term has a given type. Everything else, including tactics, notation, and automation, is scaffolding that produces terms for the kernel to check. If the kernel is right, a bug anywhere else can make the system fail to prove a theorem, but never make it accept a false one.

MiniLean4 is a proof assistant kernel at teaching scale: the same kind of dependent type theory that Lean 4, Coq, and Agda use for formal verification, cut down to what fits in a few files of Rust. If you want to understand how Lean 4 checks a proof, what the Curry-Howard correspondence buys you in practice, or where formal verification tools get their trust, this is the machinery with nothing hidden.

The goal here is to make that kernel concrete. Each chapter introduces one idea on paper, states it precisely, and then shows the Rust code that implements it. The whole implementation, kernel plus tactics and parser, is under three thousand lines with no dependencies, and this book covers all of it.

The book is self-contained. You need to be comfortable reading a recursive function and a little set notation. No prior exposure to type theory or to Lean is assumed, although readers who know Lean will recognize much of what they see.

How to read it

A Kernel for Propositional Logic

  1. 1Propositions as TypesWhy checking a proof is the same job as type checking a program.
  2. 2The Language of TermsFive kinds of terms, and the Rust enum that represents them.
  3. 3Typing RulesContexts, judgments, and the eight rules that define well-typed terms.
  4. 4The Typing Rules as a LogicNatural deduction, the translation of formulas to types, and what consistency means.
  5. 5A Type Checking AlgorithmTurning the rules into Infer and Check, line by line against typecheck.rs.
  6. 6Worked ExamplesStep-by-step traces of the checker on right proofs, wrong proofs, and conversions.

Growing the Kernel

  1. 7Inductive Types I: Natural NumbersConstants, definitions, and the recursor that gives both recursion and induction.
  2. 8Inductive Types II: The General RecipeTrees, lists, vectors, and the procedure that generates any recursor.
  3. 9Equality Is an Inductive Type TooEq, transport, definitional versus propositional equality, and the proof of 0 + n = n.
  4. 10Tactics: Building Proof Terms InteractivelyGoals, holes, and how intro, apply, induction, and rw construct terms.

Under the Hood

  1. 11Substitution, Capture, and Alpha-EquivalenceThe operations where kernel bugs hide, and how substitution.rs avoids them.
  2. 12Reduction and ConversionBeta, delta, iota; computation rules as data; weak head normal form.

Foundations

  1. 13Universes: Why Sort u?Type-in-Type is inconsistent; a hierarchy with an impredicative bottom is not.
  2. 14Properties of the SystemUniqueness, subject reduction, strong normalization, consistency.

Appendices

  1. AProof that Proofs Are TermsThe full proof of the translation theorem.
  2. BCorrectness of Alpha-EquivalenceBoth definitions compute de Bruijn forms.
  3. CHurkens' ParadoxA closed term of type False under Type : Type.
  4. DProof of ConsistencyNo closed term inhabits forall P, P.
  5. EThe leanmini LanguageSyntax, commands, and tactics of the script language.
  6. FNotation and GlossaryEvery symbol and term used in the book.
  7. Bibliography

Part I builds a type checker for a dependent type theory and shows that it is a proof checker for a logic. Part II adds inductive types (natural numbers, trees, lists, vectors, and equality) and a tactic language that writes proof terms for you. Part III opens the black boxes that Parts I and II relied on: substitution, alpha-equivalence, and reduction, where kernel bugs hide. Part IV explains why universes are structured as they are, and states the metatheory. The appendices contain the full proofs, the paradox that justifies the universe design, and a reference for the script language.

Throughout the book you will meet four kinds of interactive element.

  • Playgrounds run the kernel. Edit the script, press run, and read the transcript.
  • Explorers show the kernel's own view of a term: its syntax tree, its typing derivation rule by rule, its reduction one step at a time, which binder each variable belongs to, and the proof state after every tactic. Every explorer is live: change the term and the kernel recomputes.
  • Steppers walk through an algorithm one step at a time, showing the state after each step.
  • Trace trees show the exact sequence of recursive calls the type checker makes on a term, with the typing rule each call corresponds to.

Callouts marked in the code point to the Rust function that implements what was just defined. Exercises come with solutions, folded away until you want them.

Running the code yourself

The repository contains the kernel, a command-line runner, and the example scripts used in this book.

cargo run -- examples/identity.leanmini
cargo run -- examples/nat-reflexivity.leanmini
cargo run -- examples/zero-add.leanmini
cargo run -- examples/generic-list.leanmini
cargo run -- examples/generic-vec.leanmini
cargo test

Running cargo run with no arguments starts an interactive session that accepts the same commands one line at a time. Appendix E describes the language.

The source is organized to follow the book.

File Contents Chapters
src/term.rs The term representation 2
src/typecheck.rs Type inference and checking 5, 6
src/substitution.rs Free variables, substitution, alpha-equivalence 11
src/reduction.rs Weak head normal form, normalization, definitional equality 12
src/declaration.rs The signature: constants, definitions, inductive metadata 7, 8
src/inductive/ Checking inductive declarations and generating recursors 8
src/proof.rs Goals, proof states, and tactics 10
src/syntax.rs The parser for the script language Appendix E
src/main.rs The command-line runner Appendix E