Chapter 1 · Foundations
Inside the checker
Step 1 of 19 · Concept
Lean reads your file from the top
This lesson shows what reads your file, which part you must trust, and what a proof is.
Lean reads a file one declaration at a time, from the top: a theorem, an #eval, a #check. The last two are
really commands; this lesson calls them declarations for short. A name exists only after its declaration.
Line 2 proves again with := four, the theorem of line 1. The step A proof is a term of its statement's type
says why.
Step 2 of 19 · Quiz
Which line is refused?
This file has the same two theorems in the other order. Which line does Lean refuse?
theorem again : 2 + 2 = 4 := four
theorem four : 2 + 2 = 4 := by rfl
Step 3 of 19 · Concept
Three stages
Three parts of Lean handle each declaration in turn:
| stage | what it does |
|---|---|
| the parser | reads the grammar: which words and symbols, in which order |
| the elaborator | fills in what you left out and runs your tactics |
| the kernel | checks the finished proof |
What the elaborator fills in: for one, that 2 is a Nat. A theorem is accepted only when the kernel agrees.
Step 4 of 19 · Quiz
Who runs rfl?
Which stage runs the tactic rfl?
Step 5 of 19 · Concept
Only the kernel must be trusted
The kernel is small, and it checks every finished proof. Tactics are many, and a tactic can have a bug.
A tactic with a bug can fail, or build a wrong proof. The kernel refuses a wrong proof, so a false theorem cannot pass. That is why the kernel is the only part you must trust.
Step 6 of 19 · Quiz
A tactic with a bug
A tactic has a bug. It builds a proof that does not fit the statement. What happens to the theorem?
Step 7 of 19 · Concept
Every term has a type
A term is anything you can write after #check: a number, a sum, a statement. Every term has a
type, and #check asks Lean for it:
| line | Lean prints |
|---|---|
#check 2 + 2 | 2 + 2 : Nat |
#check 2 + 2 = 4 | 2 + 2 = 4 : Prop |
Nat is the type of the numbers 0, 1, 2 and up. Prop is the type of statements: a statement is a term too.
Step 8 of 19 · Quiz
What does #check say?
What does Lean print for this line?
#check 2 + 2 = 5
Step 9 of 19 · Demo
See it run
Press Run and compare with your answer.
Step 10 of 19 · Concept
A proof is a term of its statement's type
A statement is a type, and a proof is a term of that type. After theorem four : 2 + 2 = 4 := by rfl, the name
four is a term whose type is 2 + 2 = 4.
So a theorem's name is a proof you can use again. Here four proves again, with no tactic:
theorem again : 2 + 2 = 4 := four
Step 11 of 19 · Demo
Reuse a proof
Press Run. #check again prints the type of again, the statement it proves. #print four prints the term
that proves four.
Step 12 of 19 · Quiz
What is the type of four?
What is the type of four?
theorem four : 2 + 2 = 4 := by rfl
theorem again : 2 + 2 = 4 := four
#check again
#print fourStep 13 of 19 · Concept
A goal is a hole with a type
After by, the proof of four is a hole whose type is the statement. The goal panel shows it as the goal,
⊢ 2 + 2 = 4. A tactic fills the hole with a term.
#print four, run in the demo Reuse a proof, shows the term that rfl built:
theorem four : 2 + 2 = 4 :=
Eq.refl (2 + 2)
Step 14 of 19 · Trace
Watch a theorem being checked
One line, checked in four moments. Press Next and watch which stage is at work.
theorem four : 2 + 2 = 4 := by rfl- The parser reads line 1: a theorem named
four, the statement2 + 2 = 4, and a proof afterby. - The elaborator opens a hole of type
2 + 2 = 4, which the goal panel shows as⊢ 2 + 2 = 4. - The elaborator runs
rfl, which fills the hole with the termEq.refl (2 + 2). No goals are left. - The kernel checks the finished term:
2 + 2computes to4, so it fits the statement. Lean acceptsfour.
Step 15 of 19 · Concept
sorry rests on an axiom
An axiom is a fact Lean accepts without a proof. sorry fills a hole with the axiom sorryAx, so Lean warns.
#print axioms lists the axioms a theorem rests on:
| theorem | #print axioms says |
|---|---|
four, by rfl | 'four' does not depend on any axioms |
later, by sorry | 'later' depends on axioms: [sorryAx] |
sorryAx proves any statement, true or false: a promise, not a proof. A theorem resting on it is not proved.
#eval computes a value, and the kernel takes no part in it. A theorem is never run: it is checked.
Step 16 of 19 · Demo
Ask what a theorem rests on
Press Run. Lean warns about the sorry on line 2, then prints what each theorem rests on.
Step 17 of 19 · Quiz
Which theorem is proved?
Run printed these two lines. Which theorem is proved?
'four' does not depend on any axioms
'later' depends on axioms: [sorryAx]
theorem four : 2 + 2 = 4 := by rfl
theorem later : 3 + 3 = 6 := by sorry
#print axioms four
#print axioms laterStep 18 of 19 · Check yourself
Check yourself: a file Lean accepts
Lean refuses this file. Put the two theorems in an order Lean accepts, keep again proved by six, and finish
six with a proof. Then the output must be:
theorem again : 3 + 3 = 6 :=
six
'again' does not depend on any axioms
A name exists only after its declaration. The theorem that another one uses goes first.
Step 19 of 19 · Recap
Recap
- Lean reads one declaration at a time, from the top; a name exists after its declaration.
- The parser reads the grammar, the elaborator runs tactics, the kernel checks the proof.
- Every term has a type: a statement is a type, a proof is a term of it.
- A goal is a hole with a type; a tactic fills it.
sorryfills a hole withsorryAx, a promise:#print axiomsshows it.
Next: lesson 3 gives values and functions their types.
Review
5 questions from this lesson, in a new order each time. Answer from memory: 3 of 5 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
#check 2 + 2 = 5
theorem four : 2 + 2 = 4 := by rfl
theorem again : 2 + 2 = 4 := four
#check again
#print four
theorem four : 2 + 2 = 4 := by rfl
theorem later : 3 + 3 = 6 := by sorry
#print axioms four
#print axioms later
theorem again : 3 + 3 = 6 := six
theorem six : 3 + 3 = 6 := by
sorry
#print again
#print axioms again