Chapter 3 · Numbers and induction
Induction
Step 1 of 19 · Concept
Some facts need one more idea
In this lesson you prove facts about every number, such as 0 + n = n and double n = 2 * n.
rfl cannot prove 0 + n = n: + recurses on its second number, and n is unknown (lesson 12). Checking 0, 1, 2
and on never ends.
omega proves this one, but it knows nothing about your own functions. It treats double n as an unknown number, b:
def double : Nat → Nat
| 0 => 0
| n + 1 => double n + 2
example (n : Nat) : double n = 2 * n := by
omega
Main.lean:5:2: error: omega could not prove the goal:
a possible counterexample may satisfy the constraints
b ≥ 0
a ≥ 0
2*a - b ≥ 1
where
a := ↑n
b := ↑(double n)
Step 2 of 19 · Concept
Two proofs give every number
To prove ∀ n, P n, prove two things. A theorem with (n : Nat) states such a fact (lesson 4).
| case | prove |
|---|---|
zero | P 0: the first rung |
succ | P k → P (k + 1): from any rung, the next |
From P 0 the step gives P 1, then P 2, and on to any n. This is induction.
Step 3 of 19 · Quiz
How often is the step used?
You have a proof of P 0 and the step, P k → P (k + 1). How often is the step used to reach P 5?
Step 4 of 19 · Concept
induction n with names both cases
induction n with splits a goal about n into its two cases, each with its own tactics:
theorem my_zero_add (n : Nat) : 0 + n = n := by
induction n with
| zero => sorry
| succ k ih => sorry
In case succ, k is any number and ih is the statement for k: the induction hypothesis. The goal is the
statement for k + 1.
Step 5 of 19 · Quiz
What is ih?
In this proof, what is ih in case succ?
theorem my_zero_add (n : Nat) : 0 + n = n := by
induction n with
| zero => sorry
| succ k ih => sorry
Step 6 of 19 · Demo
See 0 + n = n proved
case zero is rfl. In case succ, show restates the goal (lesson 10): 0 + (k + 1) computes to 0 + k + 1.
Then rw [ih] turns 0 + k into k.
Press Run: Lean accepts it. Put the cursor on rfl in case zero, then on show and rw [ih] in case succ,
and read the goal panel.
Step 7 of 19 · Trace
Watch the goals of an induction
Press Next: each frame shows the goal as the panel will show it when you run this proof.
theorem my_zero_add (n : Nat) : 0 + n = n := by
induction n with
| zero => rfl
| succ k ih =>
show 0 + k + 1 = k + 1
rw [ih]- Line 2 is next. The goal is
0 + n = n, for anyn. inductionmade two goals; the frame shows the one line 3 works on,case zero.case succwaits.rflclosedcase zero. Incase succ,ihis the fact fork. Line 5 is next.showrestated the goal as0 + k + 1 = k + 1. Line 6 is next.rw [ih]turned0 + kintok. Both sides arek + 1, sorwclosed the goal.
Step 8 of 19 · Concept
In the step: unfold, use ih, finish
Lesson 12's double:
def double : Nat → Nat
| 0 => 0
| n + 1 => double n + 2
In case succ of double n = 2 * n, three moves close the goal:
- Unfold:
rw [double]turnsdouble (k + 1)intodouble k + 2. - Use
ih: nowdouble kis in the goal, sorw [ih]fits. - Finish:
omegadoes the arithmetic.
Step 9 of 19 · Quiz
Predict the goal after rw [ih]
The goal panel shows this. What does it show after rw [ih]? The lines k and ih stay, so the options leave them
out.
case succ
k : Nat
ih : double k = 2 * k
⊢ double k + 2 = 2 * (k + 1)
Step 10 of 19 · Trace
Watch the step of double n = 2 * n
double is lesson 12's. Press Next: each frame shows the goal as the panel will show it when you run this proof.
theorem double_eq (n : Nat) : double n = 2 * n := by
induction n with
| zero => rfl
| succ k ih =>
rw [double]
rw [ih]
omega- Line 2 is next. The goal is
double n = 2 * n. - Two goals; the frame shows
case zero, which line 3 works on. Both sides compute to0, sorflfits. case succ, withihfork. Line 5 unfoldsdouble (k + 1).rw [double]gavedouble k + 2. Line 6 usesih.rw [ih]replaceddouble kby2 * k. Line 7 finishes the arithmetic.omegaproved2 * k + 2 = 2 * (k + 1): No goals.
Step 11 of 19 · Demo
See it checked
Press Run: Lean accepts both declarations. Put the cursor on rw [double], rw [ih] and omega in case succ,
and read the goal panel.
Step 12 of 19 · Try it
Try it: fill both cases
triple is lesson 12's. The two cases are written for you: replace each sorry. case succ takes the three moves.
Put the cursor on each sorry to read its goal. In case zero both sides compute to 0. case succ goes as in double_eq, on new lines.
Step 13 of 19 · Exercise
Your turn: adding by recursion
addTo m n adds n to m one at a time, by recursion on n. m stays the same in every call.
Prove addTo_eq by induction. In case succ, try the three moves: unfold, use ih, finish. Any proof Lean accepts passes.
Use induction on the number addTo recurses on: n. After induction m, case zero is addTo 0 n = 0 + n, where no equation of addTo fits:
error: Tactic `rfl` failed: The left-hand side
addTo 0 n
is not definitionally equal to the right-hand side
0 + n
After unfolding and using ih, the goal m + k + 1 = m + (k + 1) is still open: rw closes a goal only when its two sides are the same (lesson 9). The arithmetic is omega's.
rw [addTo, ...]
Step 14 of 19 · Concept
Without names, you get n✝
induction n without with gives names you cannot use. After it, the goal panel shows two goals:
case zero
⊢ 0 + 0 = 0
case succ
n✝ : Nat
a✝ : 0 + n✝ = n✝
⊢ 0 + (n✝ + 1) = n✝ + 1
n✝ and a✝ are inaccessible names: Lean made them up, and you cannot type them. So no tactic can name the
hypothesis a✝, as rw [ih] names ih.
Name the cases with with, and you choose the names: k and ih.
Step 15 of 19 · Quiz
Why name the cases?
Why write induction n with | zero => ... | succ k ih => ... rather than induction n alone?
Step 16 of 19 · Exercise
Your turn: bump
bump adds one, by recursion.
Prove bump_eq by induction on n, with named cases. Any proof Lean accepts passes.
In case zero the goal is bump 0 = 0 + 1: both sides compute to 1.
After unfolding and using ih, both sides are k + 1 + 1, so rw closes the goal itself (lesson 9). An omega after it is one tactic too many:
error: No goals to be solved
rw [bump, ...]
Step 17 of 19 · Check yourself
Check yourself: odd numbers
No shape this time.
odd n is the odd number at place n: 1, 3, 5, and so on. Prove odd_eq.
Two cases. In the second, the same three moves as for double.
Step 18 of 19 · Challenge · optional
Challenge: double is at least n
Optional.
Prove double_ge. Here ih is a ≤, not an equation, so rw [ih] cannot use it. Find a tactic that can.
rw needs an equation. With ih : k ≤ double k, Lean refuses rw [ih]:
error: Invalid rewrite argument: Expected an equality or iff proof or definition name, but `ih` is a proof of
k ≤ double k
omega reads the hypotheses, ih too (lesson 11). After rw [double], the goal k + 1 ≤ double k + 2 follows from ih.
case zero is 0 ≤ double 0, a fact about given numbers: decide. rfl refuses it, since it is not an equation.
Step 19 of 19 · Recap
Recap
- Induction proves a fact for every number:
case zero, thencase succfromih. induction n with | zero => ... | succ k ih => ...;ihis the statement fork.- In
case succ: unfold,rw [ih], thenomegafor the arithmetic. - Without
with, Lean names thingsn✝anda✝, which you cannot type.
Next: lesson 14 defines types of your own, with their own cases.
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 my_zero_add (n : Nat) : 0 + n = n := by
induction n with
| zero => rfl
| succ k ih =>
show 0 + k + 1 = k + 1
rw [ih]
def double : Nat → Nat
| 0 => 0
| n + 1 => double n + 2
theorem double_eq (n : Nat) : double n = 2 * n := by
induction n with
| zero => rfl
| succ k ih =>
rw [double]
rw [ih]
omega
def triple : Nat → Nat
| 0 => 0
| n + 1 => triple n + 3
theorem triple_eq (n : Nat) : triple n = 3 * n := by
induction n with
| zero => sorry
| succ k ih => sorry
def addTo (m : Nat) : Nat → Nat
| 0 => m
| n + 1 => addTo m n + 1
theorem addTo_eq (m n : Nat) : addTo m n = m + n := by
sorry
def bump : Nat → Nat
| 0 => 1
| n + 1 => bump n + 1
theorem bump_eq (n : Nat) : bump n = n + 1 := by
sorry
def odd : Nat → Nat
| 0 => 1
| n + 1 => odd n + 2
theorem odd_eq (n : Nat) : odd n = 2 * n + 1 := by
sorry
def double : Nat → Nat
| 0 => 0
| n + 1 => double n + 2
theorem double_ge (n : Nat) : n ≤ double n := by
sorry