Stage 07 · Step 32 of 50
Induction on a list.
01The idea
A train is empty or has a first carriage followed by the rest. A list has the same two possibilities.
Induction on a list follows its constructors.
02In maths
The length of xs appended to ys equals the length of xs plus the length of ys.
Picture it
Swipe sideways to see the whole picture.
[], or a front carriage x coupled to a shorter train xs. So induction xs gives two cases. In the cons case, the hypothesis ih already counts the carriages of xs ++ ys, and the one extra carriage adds 1 to both sides.03In Lean
This code comes directly from the file checked by Lean.
theorem trail_length_append (xs ys : List α) :
(xs ++ ys).length = xs.length + ys.length := by
induction xs with
| nil => simp
| cons x xs ih =>
simp [ih, Nat.add_assoc, Nat.add_comm]
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
α : Type u_1 xs ys : List α ⊢ (xs ++ ys).length = xs.length + ys.length
After line 2: (xs ++ ys).length = xs.length + ys.length := by
α : Type u_1 xs ys : List α ⊢ (xs ++ ys).length = xs.length + ys.length
After line 3: induction xs with
α : Type u_1 xs ys : List α ⊢ (xs ++ ys).length = xs.length + ys.length
After line 4: | nil => simp
no goals
After line 5: | cons x xs ih =>
case cons α : Type u_1 ys : List α x : α xs : List α ih : (xs ++ ys).length = xs.length + ys.length ⊢ (x :: xs ++ ys).length = (x :: xs).length + ys.length
After line 6: simp [ih, Nat.add_assoc, Nat.add_comm]
no goals
05Lean says
no goals
A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.
06A common slip
Lean is telling us something useful here.
example (xs ys : List Nat) : (xs ++ ys).length = xs.length := by
induction xs with
| nil => rfl
| cons x xs ih => simp [ih]
Tactic `rfl` failed: The left-hand side ([] ++ ys).length is not definitionally equal to the right-hand side [].length case nil ys : List Nat ⊢ ([] ++ ys).length = [].length
The statement forgets the length contributed by the second list.
The fix: Include the second list’s length on the right.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Show that appending to the empty list preserves the other list’s length.
Show a hint
Appending an empty list on the left computes directly.
One more hint
Use definitional equality.
- A little stretch
Show how the length changes when one element is put at the front.
Show a hint
The list constructor adds exactly one element.
One more hint
This length rule computes directly.
example (ys : List Nat) : ([] ++ ys).length = ys.length := by
sorry
example (x : α) (xs : List α) : (x :: xs).length = xs.length + 1 := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
example (ys : List Nat) : ([] ++ ys).length = ys.length := by
rfl
example (x : α) (xs : List α) : (x :: xs).length = xs.length + 1 := by
rfl
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.