Chapter 3 · Numbers and induction
Recursion on numbers
Step 1 of 22 · Concept
Nat is built from zero and succ
In this lesson you define functions by recursion, and see why 0 + n = n is not rfl. Lean defines Nat so:
inductive Nat where
| zero : Nat
| succ (n : Nat) : Nat
A number is Nat.zero, or Nat.succ n: one more than a number n. So 3 is
Nat.succ (Nat.succ (Nat.succ Nat.zero)). Goals and definitions write Nat.succ n as n + 1; a #check of a fact may still print n.succ.
Step 2 of 22 · Quiz
Which number is it?
Which number is Nat.succ (Nat.succ Nat.zero)?
Step 3 of 22 · Demo
See it run
Press Run. #eval prints the number, #check shows that Nat.succ takes a number. Both examples are
accepted.
Step 4 of 22 · Concept
A definition by cases
Since every number is 0 or n + 1, a function can give one equation for each shape:
def pred : Nat → Nat
| 0 => 0
| n + 1 => n
No := after the type: the | lines are the definition. Each has a shape, => and the result. For pred 5, the shape n + 1 fits with n being 4.
Step 5 of 22 · Quiz
Predict the missing line
Type the missing line.
def pred : Nat → Nat
| 0 => 0
| n + 1 => n
#eval pred 5
#eval pred 0
Step 6 of 22 · Demo
See it run
Press Run and compare with your answer.
Step 7 of 22 · Concept
A function can call itself on n
In the case n + 1, a function may call itself on n, the smaller number:
def sumTo : Nat → Nat
| 0 => 0
| n + 1 => (n + 1) + sumTo n
sumTo 4 is 4 + sumTo 3, and so on down to sumTo 0, which is 0. So #eval sumTo 4 gives 10: the
numbers from 1 to 4, added.
Step 8 of 22 · Trace
Watch sumTo 3 unfold
Press Next: each frame uses one equation, until only arithmetic is left.
def sumTo : Nat → Nat
| 0 => 0
| n + 1 => (n + 1) + sumTo n
example : sumTo 3 = 6 := rfl3is2 + 1, so line 3's equation fits withnbeing2.sumTo 2unfolds by line 3 again, withnbeing1.sumTo 1unfolds by line 3, withnbeing0.sumTo 0fits line 2's equation: it is0.- No
sumTois left. The arithmetic gives6, the value line 4 claims. 6:rflaccepts line 4.
Step 9 of 22 · Quiz
Predict the missing line
Type the missing line.
def sumTo : Nat → Nat
| 0 => 0
| n + 1 => (n + 1) + sumTo n
#eval sumTo 4
#eval sumTo 10
Step 10 of 22 · Demo
See it run
Press Run and compare with your answer.
Step 11 of 22 · Exercise
Your turn: double by recursion
- Replace
sorrywith two equations:double 0is0, anddouble (n + 1)isdouble n + 2. - Show
double 5with#eval. Run prints10.
The check asks for these two equations, so 2 * n or n + n does not pass.
The call goes on n, the smaller number. double (n + 1) inside its own case never gets smaller, and Lean refuses it:
error: fail to show termination for
double
Write def double : Nat → Nat with no := sorry, then one line per shape, as pred does. Keeping := sorry gives:
error: unexpected token '|'; expected command
def double : Nat → Nat
| 0 => 0
Step 12 of 22 · Concept
Two definitions Lean refuses
Lean checks every definition by cases before it accepts it:
| definition | Lean says |
|---|---|
| a call that does not get smaller | fail to show termination |
| a shape left out | Missing cases |
A half : Nat → Nat with only the line | 0 => 0 gets:
error: Missing cases:
(Nat.succ _)
A call on a smaller number always ends, at 0. A call on a bigger one, such as loop (n + 1) from n, never
would. A missing shape leaves some numbers with no result.
Step 13 of 22 · Demo
See both refusals
Press Run. Lean refuses line 1 and line 3. Read the first line of each message: the rest lists what Lean tried.
Step 14 of 22 · Try it
Try it: squares
sumSq is a copy of sumTo. Change it so that each step adds (n + 1) * (n + 1). Then Run prints 14:
1 + 4 + 9.
Only line 3 changes: (n + 1) becomes (n + 1) * (n + 1).
Step 15 of 22 · Concept
+ is a recursion on its second number
Lean's + is defined like sumTo, by cases on its second number. A sketch:
def add : Nat → Nat → Nat
| n, 0 => n
| n, m + 1 => add n m + 1
So n + 0 = n and n + (m + 1) = n + m + 1 hold by rfl. In 0 + n the second number is n, unknown: no
equation fits. That is lesson 4's puzzle.
Step 16 of 22 · Quiz
Which one is rfl?
For numbers n and m, which statement does rfl prove?
Step 17 of 22 · Demo
See it run
Press Run: Lean accepts all three. The first two hold by an equation of +. rw [double] uses an equation of
double.
Step 18 of 22 · Concept
rw [double] unfolds one equation
rw [double] rewrites with the equation of double that fits. On double (k + 1) that is the second one, so
double (k + 1) becomes double k + 2.
Now the goal holds double k, and rw [h] can use h : double k = m.
For a given number, decide works the value out: double 5 = 10 (lesson 1).
Step 19 of 22 · Exercise
Your turn: one step of double
Prove both theorems. For dbl_step, try this: unfold double (k + 1), then rewrite with h. Any proof Lean accepts passes.
rw [h] first finds no double k in the goal, only double (k + 1):
error: Tactic `rewrite` failed: Did not find an occurrence of the pattern
double k
Unfold first: after rw [double] the goal is double k + 2 = m + 2. Then h fits.
rw [double]
Step 20 of 22 · Check yourself
Check yourself: triple by recursion
No shape this time.
- Define
tripleby recursion:triple 0is0,triple (n + 1)istriple n + 3. - Show
triple 4with#eval. Run prints12. - Prove
triple_step.
The definition has two shapes. The proof unfolds first, then uses the hypothesis.
Step 21 of 22 · Challenge · optional
Challenge: Fibonacci
Optional.
Define fib by its three equations, then prove fib_ten. The last equation calls fib on two smaller numbers.
A shape can be n + 2: a number at least 2. Then n and n + 1 are both smaller, so both calls are allowed.
With only the shapes 0 and n + 2, the number 1 has no equation:
error: Missing cases:
(Nat.succ Nat.zero)
fib 10 = 55 is a fact about a given number, which decide works out.
Step 22 of 22 · Recap
Recap
- A
NatisNat.zeroorNat.succ n, writtenn + 1. - A definition by cases gives one equation per shape:
| 0 => ...and| n + 1 => .... - A recursive call on
nfrom the casen + 1gets smaller, so it ends. - Lean refuses a call that does not get smaller, and a missing shape.
+recurses on its second number:n + 0 = nisrfl,0 + n = nis not.
Next: lesson 13 proves facts about every number: induction.
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.
#eval Nat.succ (Nat.succ Nat.zero)
#check Nat.succ
example : Nat.succ 4 = 5 := rfl
example (n : Nat) : Nat.succ n = n + 1 := rfl
def pred : Nat → Nat
| 0 => 0
| n + 1 => n
#eval pred 5
#eval pred 0
def sumTo : Nat → Nat
| 0 => 0
| n + 1 => (n + 1) + sumTo n
#eval sumTo 4
#eval sumTo 10
-- by recursion: double 0 is 0,
-- double (n + 1) is double n + 2
def double : Nat → Nat := sorry
-- then show double 5 with #eval
def loop (n : Nat) : Nat := loop (n + 1)
def half : Nat → Nat
| 0 => 0
def sumSq : Nat → Nat
| 0 => 0
| n + 1 => (n + 1) + sumSq n
#eval sumSq 3
example (n m : Nat) : n + (m + 1) = n + m + 1 := rfl
example (n : Nat) : n + 0 = n := rfl
def double : Nat → Nat
| 0 => 0
| n + 1 => double n + 2
example (k : Nat) : double (k + 1) = double k + 2 := by
rw [double]
def double : Nat → Nat
| 0 => 0
| n + 1 => double n + 2
theorem dbl_step (k m : Nat) (h : double k = m) :
double (k + 1) = m + 2 := by
sorry
theorem dbl_five : double 5 = 10 := by
sorry
-- by recursion: triple 0 is 0,
-- triple (n + 1) is triple n + 3
def triple : Nat → Nat := sorry
-- show triple 4 with #eval
theorem triple_step (k m : Nat) (h : triple k = m) :
triple (k + 1) = m + 3 := by
sorry
-- fib 0 is 0, fib 1 is 1,
-- fib (n + 2) is fib n + fib (n + 1)
def fib : Nat → Nat := sorry
theorem fib_ten : fib 10 = 55 := by
sorry