Skip to content

Lean 4: Proofs a Machine Can Check, Chapter 1, 8 min read, updated 2026-12-06

Your first machine-checked proof

On this page

At a glance

  • You'll write three proofs, watch Lean reject a false one, and learn what "checked" means.
  • Why it matters: this is the same kind of check that stands behind the "AI proved a theorem" headlines.
  • Time: about fifteen minutes, all of it in a browser.

1.1 What Lean is: a proof checker you can talk to

Lean is a programming language and a proof assistant. You write a mathematical statement and a proof in a precise language. Lean checks every step. If Lean accepts the proof, the statement follows from the definitions and axioms in use, with no gaps. (An axiom is a basic statement that is accepted without proof. Section 1.6 shows which ones Lean uses.)

The part of Lean that does the final checking is called the kernel. It is small on purpose. Everything else (the tactics, the automation, the AI tools in Part IV) produces a proof term, and the kernel checks that term. A proof term is the finished proof, written out in full as one precise expression. A tactic can be clever or buggy, and it does not matter: if it produces something wrong, the kernel rejects it.

In plain words: The kernel is a strict referee. Helpers, including clever tactics and AI tools, can suggest moves as fast as they like. Only the referee decides whether the proof counts, and it checks every step.

The kernel at the centreDiagram: tactics, automation and AI tools surround a small kernel that checks every proof term.TacticsAutomationAI toolsproof termKernelchecks every termAcceptedRejectedHelpers: fast, clever, can be buggy
Tactics, automation and AI tools only propose proof terms; the small kernel decides.

This design is why people trust Lean proofs that no human has read line by line. Chapter 14 shows the limits of that trust.

Mathlib is Lean's community library of mathematics: algebra, number theory, analysis, topology and much more. Most of this book uses it.

1.2 Start in your browser, with nothing to install

Open https://live.lean-lang.org in a browser. It is a web editor that runs Lean on a server, with Mathlib installed. Its maintainers describe it as a way to run "(smallish) Lean snippets" and to try Lean "before installing it as a newcomer", which is what we need now.

The screen has two halves:

  • The left side is the editor, where you type.
  • The right side is the infoview. It shows Lean's messages and, inside a proof, the current goal: what you still have to prove.

Pitfall: the online editor may be newer than this book. The online editor tracks recent Mathlib, which may be newer than the version this book was tested on (see "Tested on" in the front matter). Small differences are possible. For long work, install Lean locally (section 1.7).

Clear the editor and type the first line of every example in this book:

import Mathlib

The first time, loading Mathlib takes a little while.

1.3 A proof by computation: Lean works out 2 + 2

Your first theorem is one Lean can settle just by calculating. Below the import line, type this:

theorem two_plus_two : 2 + 2 = 4 := rfl

#check two_plus_two

Read it as: "theorem two_plus_two states 2 + 2 = 4, and here is the proof: rfl". rfl (short for reflexivity) says "both sides compute to the same thing". Lean evaluates 2 + 2, gets 4, and accepts.

Put your cursor on the #check line. The infoview shows:

two_plus_two : 2 + 2 = 4

That is Lean telling you the theorem exists and what it states.

Now try a false statement. This does not compile:

theorem two_plus_two_is_five : 2 + 2 = 5 := by
  decide

decide is a tactic that settles a statement by computing whether it is true. (A tactic is a command that changes the goal one step at a time. The word by starts a list of them.) Here decide finds that the statement is false, and Lean reports an error (shortened):

error: Tactic `decide` proved that the proposition
  2 + 2 = 5
is false

Nothing is added to the library. A false statement cannot be "proved" by asking politely.

1.4 A proof with tactics: change the goal until nothing is left

Most proofs are written in tactic mode. You start with a goal and give commands that transform it until nothing is left. Here is a short example that uses one library fact. Type:

theorem add_swap (a b : ℕ) : a + b = b + a := by
  rw [Nat.add_comm]

Put your cursor at the end of the by line. The infoview shows the goal:

a b : ℕ
⊢ a + b = b + a

This display is called the goal state. Read it in two parts:

  • Above the line ⊢ are the things you have: two natural numbers a and b. Facts and objects listed here are your hypotheses.
  • After ⊢ is what you must prove: the goal.

The tactic rw [Nat.add_comm] rewrites the goal with the library fact Nat.add_comm (addition of natural numbers is commutative). After the rewrite the two sides are the same, rw closes the goal by reflexivity, and the infoview says:

No goals

In plain words: A tactic proof is a conversation. Lean shows you what is left to prove. You answer with a tactic. Lean shows you what is left now. When it says "No goals", you are done.

ℕ is Lean's natural numbers 0, 1, 2, ….

Tip: In the editor, type \N and a space to get the symbol ℕ. Lean also accepts the plain name Nat.

1.5 A proof you would write on paper, line for line

Here is a statement from a first proofs course: the sum of two even numbers is even. On paper: if $m = k + k$ and $n = l + l$, then $m + n = (k + l) + (k + l)$.

In Mathlib, Even m means "there is an r with m = r + r". The Lean proof below follows the paper proof step for step:

theorem even_add_even (m n : ℕ) (hm : Even m) (hn : Even n) : Even (m + n) := by
  obtain ⟨k, hk⟩ := hm
  obtain ⟨l, hl⟩ := hn
  exact ⟨k + l, by omega⟩

Step by step:

  1. obtain ⟨k, hk⟩ := hm unpacks the hypothesis "m is even" into a number k and the fact hk : m = k + k.
  2. The second line does the same for n.
  3. exact ⟨k + l, by omega⟩ says: the witness is k + l, and the equation m + n = k + l + (k + l) follows by omega. omega is a tactic that solves linear arithmetic over the integers and natural numbers using the hypotheses in context.

Here is the goal state just before the last line:

m n k : ℕ
hk : m = k + k
l : ℕ
hl : n = l + l
⊢ Even (m + n)

You did not need to write even_add_even yourself. Mathlib already has it, and Chapter 5 shows how to find it. Here the whole proof is one library fact:

example (m n : ℕ) (hm : Even m) (hn : Even n) : Even (m + n) :=
  hm.add hn

Writing it yourself once is still the best way to learn what the library is doing.

1.6 What "checked" means, and how sorry marks a gap

Sometimes you want to state something now and prove it later. Lean lets you write sorry in place of a proof:

theorem later (n : ℕ) : n ≤ n ^ 2 := by
  sorry

Lean accepts the file but warns:

warning: declaration uses `sorry`

sorry is an honest placeholder: Lean records that the theorem depends on an unproved step.

You can ask any theorem which axioms it depends on, with #print axioms:

theorem later' (n : ℕ) : n ≤ n ^ 2 := by
  sorry

#print axioms later'

The output is:

'later'' depends on axioms: [propext, sorryAx]

sorryAx is the marker for "proof missing". A finished proof never mentions it.

Compare a real theorem from Mathlib, Euclid's theorem that there are infinitely many primes:

#check Nat.exists_infinite_primes
#print axioms Nat.exists_infinite_primes
Nat.exists_infinite_primes (n : ℕ) : ∃ p, n ≤ p ∧ Nat.Prime p
'Nat.exists_infinite_primes' depends on axioms: [propext, Classical.choice, Quot.sound]

Those three axioms are Lean's standard ones: propositional extensionality, the axiom of choice, and quotient soundness. Most of Mathlib uses them. When people say a Lean proof "uses only the standard axioms", this is the list they mean. Remember it: it comes back in Chapters 14 and 15.

In plain words: #print axioms is the ingredients label on a proof. Three standard ingredients are normal. sorryAx on the label means a step is missing, however finished the proof looks.

Try it. Replace the sorry in later with a real proof. Hint: Nat.le_self_pow is in Mathlib, and nlinarith (Chapter 6) also works. The solution is in labs/QedFolio/Solutions/Ch01.lean.

1.7 Installing Lean on your own computer

When you are ready for a real project, install Lean on your own machine. The supported route is elan, Lean's version manager, from github.com/leanprover/elan, together with the Lean 4 extension for VS Code. Lean's own install guide is at lean-lang.org/install.

The fastest way to get a project with every example from this book is the book's labs/ folder. It pins the exact versions we tested. Run these three commands:

cd labs
lake exe cache get   # download prebuilt Mathlib (several GB)
lake build

lake exe cache get downloads Mathlib's prebuilt files instead of compiling them.

Pitfall: don't skip the cache. Do not skip lake exe cache get: compiling Mathlib from source takes hours.

Chapter 10 explains the files in labs/ and how to make your own project.

Ran On 2026-12-06 we installed elan 4.5.0 on Windows 11 in user space, ran lake exe cache get (it unpacked 9,117 files) and lake build in labs/. The .lake folder was 7.1 GB afterwards.

Key takeaways

  • A theorem is a statement plus a proof. Lean's kernel checks the proof.
  • rfl and decide prove things by computation. rw, obtain, exact and omega are tactics that change the goal. The infoview shows hypotheses above ⊢ and the goal below.
  • sorry marks a missing proof, and #print axioms exposes it.
  • "Standard axioms" means propext, Classical.choice and Quot.sound.

Chapter 2 is about the shape of goals: what to do when the goal is "for all", "there exists", "and", "or" or "not".

Sources (read 2026-12-06)