Chapter 4 · Your own data
Induction on lists
Step 1 of 21 · Concept
Induction works on lists too
In this lesson you prove facts about every list, such as app xs [] = xs for any xs.
A list is [] or x :: xs (lesson 15). So two proofs cover every list, as in lesson 13:
- the fact for
[]; - the fact for
x :: xs, assuming it for the shorterxs.
[] plays the part of 0, and x :: xs the part of k + 1.
Step 2 of 21 · Quiz
The two cases of a list
Induction on a list xs proves a fact for two kinds of list. Which two?
Step 3 of 21 · Concept
induction xs with names both cases
induction xs with names the two cases, as lesson 13's induction n with did:
induction xs with
| nil => sorry
| cons x xs ih => sorry
| case | its goal |
|---|---|
nil | the fact for [] |
cons x xs ih | the fact for x :: xs |
The names are the list's two constructors: [] is short for List.nil, and x :: xs for List.cons x xs. In
case cons, x is the head and xs the tail. ih is the statement for the tail xs.
Step 4 of 21 · Demo
A first proof: app xs [] = xs
app is lesson 15's join. rfl refuses app xs [] = xs: app takes its first list apart, and xs is unknown.
Press Run: Lean accepts it. Put the cursor on rfl, then on simp, and read each case's goal.
Step 5 of 21 · Trace
Watch the goals of app_nil
The proof of app_nil, with app from the demo. Press Next: each frame shows the goal as the panel will show it when you run this proof.
theorem app_nil (xs : List Nat) : app xs [] = xs := by
induction xs with
| nil => rfl
| cons x xs ih => simp [app, ih]- Line 2 is next. The goal is the statement, for any list
xs. case nilis in focus:⊢ app [] [] = [], which computes.case conswaits for line 4. Line 3 is next.rflclosedcase nil.case conshasx,xsandih : app xs [] = xs. Line 4 is next.simp [app, ih]unfoldedapponce, then usedih. No goals.
Step 6 of 21 · Quiz
What does ih say?
In case cons of app_nil, which line does the goal panel show for ih?
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => x :: app xs ys
theorem app_nil (xs : List Nat) : app xs [] = xs := by
induction xs with
| nil => rfl
| cons x xs ih => simp [app, ih]Step 7 of 21 · Try it
Try it: give simp the hypothesis
len and doubleAll are lesson 15's. Lean refuses the proof, and the message shows ih and the goal left:
Main.lean:11:17: error: unsolved goals
case cons
x : Nat
xs : List Nat
ih : len (doubleAll xs) = len xs
⊢ len (doubleAll xs) = len xs
Press Run and read the message. Then add ih to the list of simp in case cons. Any proof Lean accepts passes.
The goal left is ih itself, but simp uses only the hypotheses in its list. Write ih after len.
Step 8 of 21 · Concept
The length of a join
len (app xs ys) = len xs + len ys holds for any two lists. Prove it by induction on xs, the list app takes
apart; ys stays as it is.
case nil:simp [len, app]unfolds both definitions and closes it.case cons:simp [len, app, ih]unfolds, then usesih. It leaves1 + (len xs + len ys) = 1 + len xs + len ys.omegafinishes: the same sum, grouped two ways.
These are lesson 13's three moves: unfold, use ih, finish. omega also uses the hypotheses it finds (lesson 11),
ih among them. So the check accepts a proof that never names ih; naming it shows where the induction is used.
Step 9 of 21 · Trace
Watch len_app, one tactic at a time
len and app are lesson 15's. Press Next: each frame shows the goal as the panel will show it when you run this proof.
theorem len_app (xs ys : List Nat) :
len (app xs ys) = len xs + len ys := by
induction xs with
| nil => simp [len, app]
| cons x xs ih =>
simp [len, app, ih]
omega- Line 3 is next. Two lists,
xsandys, and the goal about both. case nilis in focus:xsbecame[], andysstayed.case conswaits for line 5. Line 4 is next.simpclosedcase nil.case conshasih, the statement for the tailxs. Line 6 is next.simp [len, app, ih]unfolded both definitions and usedih. Only arithmetic is left. Line 7 is next.omegaclosed the goal: No goals.
Step 10 of 21 · Quiz
Why omega at the end?
After simp [len, app, ih], the goal panel shows this. Why can omega close the goal?
case cons
ys : List Nat
x : Nat
xs : List Nat
ih : len (app xs ys) = len xs + len ys
⊢ 1 + (len xs + len ys) = 1 + len xs + len ys
Step 11 of 21 · Demo
See len_app accepted
Press Run: Lean accepts it. Put the cursor on omega and read the goal before it.
Step 12 of 21 · Exercise
Your turn: the sum of a join
app and sum are lesson 15's. Prove sum_app by induction, with the three moves: unfold, use ih, finish. Any proof Lean
accepts passes.
Induct on xs, the list app takes apart. With induction ys, case nil is stuck at ⊢ sum (app xs []) = sum xs: app xs [] does not unfold.
rfl refuses case nil here: sum [] + sum ys is 0 + sum ys, which does not compute (lesson 4). Unfold with simp and the two definitions instead.
| cons x xs ih =>
simp [sum, app, ih]
Step 13 of 21 · Exercise
Your turn: doubling doubles the sum
Prove sum_double: the sum of doubleAll xs is twice the sum of xs.
Induct on xs. In case nil both sides compute to 0.
In case cons, once simp has unfolded and used ih, the goal is 2 * x + 2 * sum xs = 2 * (x + sum xs). simp stops there: what is left is arithmetic, with * by a number (lesson 11).
simp's list in case cons: the two definitions, sum and doubleAll, and ih.
Step 14 of 21 · Quiz
Induction on which list?
To prove len (app xs ys) = len xs + len ys, which list do you induct on?
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => x :: app xs ys
Step 15 of 21 · Concept
A theorem you proved is a fact for simp
len (app xs xs) = 2 * len xs needs no new induction. len_app holds for any two lists, so for xs and xs too.
simp [len_app] rewrites with it, as with any fact in its list (lesson 11):
| the goal | |
|---|---|
| before | len (app xs xs) = 2 * len xs |
after simp [len_app] | len xs + len xs = 2 * len xs |
omega finishes. A theorem's name is a proof you can reuse (lesson 2), here as a fact for simp.
Step 16 of 21 · Demo
See it used
Press Run: Lean accepts it. Put the cursor on omega and read the goal simp [len_app] left.
Step 17 of 21 · Quiz
Why no induction this time?
len_app is proved by induction. Why does this proof need none?
theorem len_twice (xs : List Nat) :
len (app xs xs) = 2 * len xs := by
simp [len_app]
omega
Step 18 of 21 · Try it
Try it: three lists
len_app is proved for you. Prove len_three with one simp line that uses it. Any proof Lean accepts passes.
simp [len_app] uses len_app twice: for the outer app, then for the inner one.
Step 19 of 21 · Check yourself
Check yourself: joins group either way
No shape this time.
Prove app_assoc: a join of three lists gives the same list, whichever two you join first.
Both sides take the same list apart first. Induct on that one.
Step 20 of 21 · Challenge · optional
Challenge: your len is the library's
Optional.
Prove len_eq_length: lesson 15's len gives the same number as the library's xs.length, for every list.
Induct on xs. In case nil both sides compute to 0.
simp knows the library's facts about length: the length of x :: xs is xs.length + 1. In case cons, simp [len, ih] leaves ⊢ 1 + xs.length = xs.length + 1.
1 + xs.length = xs.length + 1 is arithmetic about one unknown number, xs.length. Finish with the tactic for that (lesson 11).
Step 21 of 21 · Recap
Recap
induction xs with | nil => ... | cons x xs ih => ...proves a fact for every list.- In
case cons,ihis the statement for the tailxs. - Unfold and use
ihwithsimp [len, app, ih], then finish withomega. - Induct on the list the function takes apart.
- A theorem you proved is a fact for
simp:simp [len_app].
Next: lesson 17 groups values into structures.
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.
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => x :: app xs ys
theorem app_nil (xs : List Nat) : app xs [] = xs := by
induction xs with
| nil => rfl
| cons x xs ih => simp [app, ih]
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
def doubleAll : List Nat → List Nat
| [] => []
| x :: xs => 2 * x :: doubleAll xs
theorem len_doubleAll (xs : List Nat) :
len (doubleAll xs) = len xs := by
induction xs with
| nil => rfl
| cons x xs ih => simp [doubleAll, len]
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => x :: app xs ys
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
theorem len_app (xs ys : List Nat) :
len (app xs ys) = len xs + len ys := by
induction xs with
| nil => simp [len, app]
| cons x xs ih =>
simp [len, app, ih]
omega
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => x :: app xs ys
def sum : List Nat → Nat
| [] => 0
| x :: xs => x + sum xs
theorem sum_app (xs ys : List Nat) :
sum (app xs ys) = sum xs + sum ys := by
sorry
def sum : List Nat → Nat
| [] => 0
| x :: xs => x + sum xs
def doubleAll : List Nat → List Nat
| [] => []
| x :: xs => 2 * x :: doubleAll xs
theorem sum_double (xs : List Nat) :
sum (doubleAll xs) = 2 * sum xs := by
sorry
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => x :: app xs ys
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
theorem len_app (xs ys : List Nat) :
len (app xs ys) = len xs + len ys := by
induction xs with
| nil => simp [len, app]
| cons x xs ih =>
simp [len, app, ih]
omega
theorem len_twice (xs : List Nat) :
len (app xs xs) = 2 * len xs := by
simp [len_app]
omega
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => x :: app xs ys
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
theorem len_app (xs ys : List Nat) :
len (app xs ys) = len xs + len ys := by
induction xs with
| nil => simp [len, app]
| cons x xs ih =>
simp [len, app, ih]
omega
theorem len_three (xs ys zs : List Nat) :
len (app (app xs ys) zs) = len xs + len ys + len zs := by
sorry
def app : List Nat → List Nat → List Nat
| [], ys => ys
| x :: xs, ys => x :: app xs ys
theorem app_assoc (xs ys zs : List Nat) :
app (app xs ys) zs = app xs (app ys zs) := by
sorry
def len : List Nat → Nat
| [] => 0
| _ :: xs => 1 + len xs
theorem len_eq_length (xs : List Nat) : len xs = xs.length := by
sorry