Chapter 1 · Foundations
When Lean says no
Step 1 of 22 · Concept
A message has a place and a text
In this lesson you learn to read every message Lean gives, and which one to fix first.
A message starts with a header such as Main.lean:2:31: error:. It names the file, the line, the column (counted
from 0) and the kind: error or warning. Then comes the text, and often the goal.
Some errors also give their name in brackets: error(lean.unknownIdentifier):.
Step 2 of 22 · Demo
Run a file with two mistakes
Press Run. Lean prints an error, a value, another error and the axioms again rests on.
Step 3 of 22 · Quiz
Which line does it name?
The first message of this file starts like this. Which line does it name?
Main.lean:2:31: error: Tactic `rfl` failed: The left-hand side
theorem four : 2 + 2 = 4 := by rfl
theorem nine : 4 + 4 = 9 := by rfl
#eval 10
theorem again : 4 + 4 = 9 := nine
theorem twelve : 6 + 6 = 12 := twelf
#print axioms againStep 4 of 22 · Concept
A refused declaration is kept, proved by sorry
Each declaration is checked on its own. So after the error on line 2, the #eval on line 3 still printed 10.
A refused declaration is kept as if sorry proved it. So again, proved by nine, gets no message. But
#print axioms again shows what it rests on:
'again' depends on axioms: [sorryAx]
So fix the first error first. Later declarations may rest on it without a message of their own.
Step 5 of 22 · Quiz
Why is again not refused?
theorem again : 4 + 4 = 9 := nine got no message. Why?
theorem four : 2 + 2 = 4 := by rfl
theorem nine : 4 + 4 = 9 := by rfl
#eval 10
theorem again : 4 + 4 = 9 := nine
theorem twelve : 6 + 6 = 12 := twelf
#print axioms againStep 6 of 22 · Concept
Unknown identifier: a name Lean does not know
Unknown identifier means Lean knows no declaration of that name at that point. Line 5 of the file with two mistakes
gave:
Main.lean:5:31: error(lean.unknownIdentifier): Unknown identifier `twelf`
Two causes are common:
- a typo, such as
twelffortwelve; - a name used before its declaration (lesson 2).
Step 7 of 22 · Try it
Try it: fix the name
Press Run and read the message. #print again shows sorry, the refused proof's stand-in.
Fix the name on line 2, so that again is proved by four. Then Run shows:
theorem again : 2 + 2 = 4 :=
four
Lean names the line and the name it does not know. Compare it with the theorem on line 1.
Step 8 of 22 · Concept
rfl fails when the sides do not compute alike
The first message of the file with two mistakes:
Main.lean:2:31: error: Tactic `rfl` failed: The left-hand side
4 + 4
is not definitionally equal to the right-hand side
9
⊢ 4 + 4 = 9
"Is not definitionally equal" means the two sides do not compute to the same value. Then Lean prints the goal it could not close. There are two reasons:
- the claim is false, as here:
4 + 4is8; - the claim is true, but its sides do not compute alike.
Step 9 of 22 · Concept
A theorem can take parameters
A theorem can take parameters before its colon, as a function does (lesson 3):
example (n : Nat) : n + 0 = n := by
rfl
It claims n + 0 = n for every n. Before rfl, the goal panel lists the parameter, then the goal:
n : Nat
⊢ n + 0 = n
A proof must work for every n, so it cannot pick one. Lean accepts this one.
Step 10 of 22 · Quiz
Which one does rfl prove?
Both claims are true for every n. Which one does rfl prove?
example (n : Nat) : n + 0 = n := by rfl
example (n : Nat) : 0 + n = n := by rfl
Step 11 of 22 · Demo
See it run
Press Run. Line 1 is accepted. Line 2 is refused, and the goal Lean prints is 0 + n = n, with n : Nat.
Step 12 of 22 · Concept
decide says when a fact is false
rfl refused 0 + n = n, a true claim: 0 + n does not compute while n is unknown. When decide refuses, the
claim is false:
theorem big : 12 < 7 := by decide
Main.lean:1:27: error: Tactic `decide` proved that the proposition
12 < 7
is false
Proposition is Lean's word for a statement (lesson 5). Fix the claim, not the tactic.
Step 13 of 22 · Concept
A type mismatch: a value of the wrong type
Lesson 3 gave double a String. Lean says:
Main.lean:2:13: error: Application type mismatch: The argument
"two"
has type
String
but is expected to have type
Nat
in the application
double "two"
After "has type" comes what you wrote; after "but is expected to have type", what fits there.
Type mismatch, without "Application", says the same of a whole value or proof. After
theorem five : 2 + 3 = 5 := by rfl, this line is refused:
theorem again : 2 + 2 = 4 := five
Main.lean:2:29: error: Type mismatch
five
has type
2 + 3 = 5
but is expected to have type
2 + 2 = 4
Lesson 5's tactics report it in the same form.
Step 14 of 22 · Quiz
What did Lean expect?
Lean refuses line 2 of this file with this message. What type did Lean expect for the argument?
def area (w h : Nat) : Nat := w * h
#eval area 3 "four"
Main.lean:2:13: error: Application type mismatch: The argument
"four"
has type
String
but is expected to have type
Nat
in the application
area 3 "four"
def area (w h : Nat) : Nat := w * h
#eval area 3 "four"Main.lean:2:13: error: Application type mismatch: The argument
"four"
has type
String
but is expected to have type
Nat
in the application
area 3 "four"Step 15 of 22 · Exercise
Your turn: fix four declarations
Lean refuses all four theorems. Make Lean accept them: each must claim exactly what its comment says.
Fix them from the top, one message at a time. Each message names its line.
Read what each message says: a false claim for rfl, an unknown name, a String where a Nat is expected, a false claim for decide. Then compare the line with its comment.
theorem nine : 4 + 5 = 9 := by rfl
Step 16 of 22 · Concept
unexpected token: the grammar is broken
This theorem has no := before its proof:
theorem t : 2 + 2 = 4 by rfl
Main.lean:1:21: error: unexpected token 'by'; expected ':=', 'where' or '|'
unexpected token comes from the parser: the words are not in an order the grammar allows. The parser stops at
the first word that does not fit. That is often right after the real mistake: here by is fine, and the := before
it is missing.
Step 17 of 22 · Concept
A tactic must be indented
The layout of lesson 1 is grammar too. Here rfl starts at column 0:
theorem t : 2 + 2 = 4 := by
rfl
Main.lean:1:25: error: unsolved goals
⊢ 2 + 2 = 4
Main.lean:2:0: error: unexpected identifier; expected command
A line at column 0 ends the proof. So by got no tactic (unsolved goals), and rfl was read as a new command.
One space too many joins a line to the tactic before it. From lesson 5 on, proofs have several tactics:
example (p q : Prop) : p → q → p := by
intro hp
intro hq
exact hp
Main.lean:3:9: error: Tactic `introN` failed: There are no additional binders or `let` bindings in the goal to introduce
p q : Prop
hp : p
intro : q
⊢ p
Line 3 became part of intro hp, so the message names introN and a hypothesis intro, which you never meant.
Indent every tactic by the same two spaces.
Step 18 of 22 · Concept
A warning is not an error
Two warnings you will meet:
| warning | the check |
|---|---|
declaration uses `sorry` | fails: the proof is not finished |
| a variable that is never used | passes |
def f (x : Nat) : Nat := 0 never uses x. Lean warns, then adds a hint and a note:
Main.lean:1:7: warning: Variable name `x` is not explicitly referenced.
The file is still accepted. Only the sorry warning fails the check.
Step 19 of 22 · Quiz
Which file passes the check?
Each option is a whole file. Which one passes the check?
Step 20 of 22 · Check yourself
Check yourself: make the file pass
Lean refuses this file. Make it pass: each theorem must claim exactly what its comment says.
Fix the messages from the top, and run again after each fix: a later message may change.
Step 21 of 22 · Challenge · optional
Challenge: what #eval cannot show
Optional.
Lean refuses #eval result. Read the message, then change result so that #eval result prints 5. Keep add
as it is.
The message names the type of result, Nat → Nat, as its def line says: result is a function. #eval can show a value, not a function.
add takes two numbers. add 2 has only one, so it is a function still waiting for the second: a Nat → Nat.
Give add its second argument, after the 2, and give result the type of a number.
Step 22 of 22 · Recap
Recap
Main.lean:2:31: error:names the file, the line and the column, then the text.- Fix the first error first: a refused declaration is kept, resting on
sorryAx. rfl's "not definitionally equal": false, or not computing;decide's "is false": false.- A mismatch: "has type ... but is expected to have type".
- A warning passes, except
declaration uses `sorry`.
Next: lesson 5 proves statements that do not compute, with the goal panel as your guide.
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.
theorem four : 2 + 2 = 4 := by rfl
theorem nine : 4 + 4 = 9 := by rfl
#eval 10
theorem again : 4 + 4 = 9 := nine
theorem twelve : 6 + 6 = 12 := twelf
#print axioms again
theorem four : 2 + 2 = 4 := by rfl
theorem again : 2 + 2 = 4 := fuor
#print again
example (n : Nat) : n + 0 = n := by rfl
example (n : Nat) : 0 + n = n := by rfl
def double (n : Nat) : Nat := n + n
-- nine : 4 + 5 = 9
theorem nine : 4 + 5 = 10 := by rfl
-- again : 4 + 5 = 9
theorem again : 4 + 5 = 9 := nien
-- ten : double 5 = 10
theorem ten : double "5" = 10 := by rfl
-- small : 9 < 10
theorem small : 10 < 9 := by decide
def double (n : Nat) : Nat := n + n
-- twelve : 6 + 6 = 12
theorem twelve : 6 + 6 = 12 by rfl
-- twelve_again : 6 + 6 = 12
theorem twelve_again : 6 + 6 = 12 := twelf
-- ten_double : double 5 = 10
theorem ten_double : double "5" = 10 := by
sorry
def add (a b : Nat) : Nat := a + b
def result : Nat → Nat := add 2
#eval result