Chapter 3 · Numbers and induction
Chains of steps
Step 1 of 19 · Concept
calc writes a proof as a chain
In this lesson you prove a statement as a chain of small steps. You also prove ≤ and <.
calc writes the chain, one step per line, each with its own proof after :=:
theorem chain (a b : Nat) (h : a = b) : a + 0 = b := by
calc a + 0 = a := by rw [Nat.add_zero]
_ = b := h
Each _ stands for the right side of the step before. The first left side and the last right side must be the
goal's.
Step 2 of 19 · Quiz
What does _ stand for?
In this chain, what does the _ on line 3 stand for?
theorem chain (a b : Nat) (h : a = b) : a + 0 = b := by
calc a + 0 = a := by rw [Nat.add_zero]
_ = b := h
Step 3 of 19 · Demo
See the chain checked
Lean checks each step with its proof, and that the chain's ends are the goal's two sides.
Press Run: Lean accepts it. Put the cursor on rw and read the goal of that one step.
Step 4 of 19 · Exercise
Your turn: three steps
Start each _ under calc, under the first term (here a), or between:
calc a = b := h1
_ = c + 0 := h2
One column past a, Lean reads the _ as part of the line before. Its first message, then two more that go away
with the fix:
calc a = b := h1
_ = c + 0 := h2 -- refused
error: Function expected at
h1
but this term has type
a = b
Prove calc_two with a chain of three steps: from a to b, to c + 0, to c. Fix the first error first. Any proof Lean
accepts passes.
Each step's proof must prove that step exactly. h1 for the step _ = c + 0 gives:
error: Type mismatch
h1
has type
a = b
but is expected to have type
b = c + 0
Function expected at h1 means a _ stands too far right: put it no further right than a.
_ = c := by rw [Nat.add_zero]
Step 5 of 19 · Concept
≤ and < on Nat
a ≤ b reads a is at most b. Type ≤ as \le or <=.
On Nat, a < b means a + 1 ≤ b: Lean defines it so. A proof of a + 1 ≤ b is therefore a proof of a < b.
On given numbers, decide checks both, as in lesson 1: 3 < 5 and 3 ≤ 5.
Step 6 of 19 · Quiz
Which says the same as 3 < 5?
On Nat, Lean reads 3 < 5 as which statement?
Step 7 of 19 · Concept
Four facts about ≤
The library proves these. #check shows each statement:
Nat.le_refl (n : Nat) : n ≤ n
Nat.le_succ (n : Nat) : n ≤ n.succ
Nat.le_add_right (n k : Nat) : n ≤ n + k
Nat.le_trans {n m k : Nat} : n ≤ m → m ≤ k → n ≤ k
n.succ is how a fact's statement shows n + 1: Nat.le_succ c proves c ≤ c + 1.
Nat.le_trans joins two steps into one. Its numbers are in braces: Lean finds them from the two proofs you give,
so Nat.le_trans h1 h2 needs no numbers.
Step 8 of 19 · Demo
See them used
Press Run: Lean prints the four statements and accepts the three proofs. Each proof is a fact with its numbers.
Step 9 of 19 · Try it
Try it: two steps make one
Prove le_three. One exact with Nat.le_trans does it.
Nat.le_trans takes the step from a first, then the step to c. In the other order Lean says Application type mismatch.
Step 10 of 19 · Concept
A chain of = and ≤ proves ≤
A calc chain may mix = and ≤ steps:
example (a b c : Nat) (h1 : a ≤ b) (h2 : b = c) :
a ≤ c + 1 := by
calc a ≤ b := h1
_ = c := h2
_ ≤ c + 1 := Nat.le_succ c
An = step keeps the chain's ≤: the chain proves a ≤ c + 1.
Step 11 of 19 · Quiz
What does the chain prove?
What does this chain prove?
calc a = b := h1
_ ≤ c := h2
_ = d := h3
Step 12 of 19 · Exercise
Your turn: a chain to a bound
Prove chain_le. Try a calc chain: from a to b, to c, to c + 1. Any proof Lean accepts passes.
The chain must end at the goal's right side, c + 1. A chain that stops at c leaves the last step to prove:
error: unsolved goals
case calc.step
a b c : Nat
h1 : a = b + 0
h2 : b ≤ c
⊢ c ≤ c + 1
The first step is a = b := by rw [h1, Nat.add_zero].
_ ≤ c := h2
Step 13 of 19 · Concept
have adds a stepping stone
have h : statement := proof proves a statement on the way, and adds it to the hypotheses, named h:
have h3 : a ≤ c := Nat.le_trans h1 h2
The goal stays the same. The next tactics can use h3 like any hypothesis.
Step 14 of 19 · Demo
See the stone appear
Press Run: Lean accepts both. Put the cursor on have and read the goal panel before and after it: h3 appears.
The second proof restates its goal with show, as the step show restates the goal explains.
Step 15 of 19 · Concept
show restates the goal
show replaces the goal by a statement that computes to it. For the goal ⊢ a < b, show a + 1 ≤ b gives
⊢ a + 1 ≤ b, since a < b means a + 1 ≤ b.
A statement that does not compute to the goal is refused:
example (x : Nat) (h : 4 ≤ x) : 3 < x := by
show 3 ≤ x
Main.lean:2:2: error: 'show' tactic failed, pattern
3 ≤ x
is not definitionally equal to target
3 < x
Step 16 of 19 · Quiz
Which show is accepted?
The goal is ⊢ 3 < x. Which tactic does Lean accept?
Step 17 of 19 · Check yourself
Check yourself: a bound with a stone
No shape this time.
Prove bound: if a < b and b = c, then a + 1 ≤ c + 5. Any proof Lean accepts passes.
A stone first, then a chain.
Step 18 of 19 · Challenge · optional
Challenge: a chain with a < in it
Optional.
Prove lt_chain. The goal is a <, so the chain needs a < step.
A chain with one < step and the rest ≤ or = proves a <.
Start the chain with the < step: calc a < b := h1. With a ≤ b := h1 Lean says Type mismatch, since h1 proves a < b.
_ ≤ c + 1 := Nat.le_succ c
Step 19 of 19 · Recap
Recap
calcproves a statement as a chain; each_stands for the right side before it.≤and<onNat:a < bmeansa + 1 ≤ b;Nat.le_refl,Nat.le_succ,Nat.le_add_right,Nat.le_trans.have h : statement := proofadds a stepping stone to the hypotheses.showrestates the goal as a statement that computes to it.
Next: lesson 11 lets automation do the arithmetic, and says why you can trust it.
Review
4 questions from this lesson, in a new order each time. Answer from memory: 3 of 4 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
theorem chain (a b : Nat) (h : a = b) : a + 0 = b := by
calc a + 0 = a := by rw [Nat.add_zero]
_ = b := h
theorem calc_two (a b c : Nat) (h1 : a = b) (h2 : b = c + 0) :
a = c := by
sorry
#check Nat.le_refl
#check Nat.le_succ
#check Nat.le_add_right
#check Nat.le_trans
example (n : Nat) : n ≤ n + 1 := Nat.le_succ n
example (a b : Nat) (h : a ≤ b) : a ≤ b + 5 := Nat.le_trans h
(Nat.le_add_right b 5)
example (n : Nat) : n ≤ n := Nat.le_refl n
theorem le_three (a b c : Nat) (h1 : a ≤ b) (h2 : b ≤ c) :
a ≤ c := by
sorry
theorem chain_le (a b c : Nat) (h1 : a = b + 0) (h2 : b ≤ c) :
a ≤ c + 1 := by
sorry
example (a b c : Nat) (h1 : a ≤ b) (h2 : b ≤ c) :
a ≤ c + 1 := by
have h3 : a ≤ c := Nat.le_trans h1 h2
exact Nat.le_trans h3 (Nat.le_succ c)
example (x : Nat) (h : 4 ≤ x) : 3 < x := by
show 4 ≤ x
exact h
theorem bound (a b c : Nat) (h1 : a < b) (h2 : b = c) :
a + 1 ≤ c + 5 := by
sorry
theorem lt_chain (a b c : Nat) (h1 : a < b) (h2 : b ≤ c) :
a < c + 1 := by
sorry