Stage 10 · Step 50 of 50
Capstone: combine two scoreboards.
01The idea
Two teams record points in separate scorebooks. You can join their entries and total them once, or total each book and add the results.
Prove both methods agree for scorebooks of any length.
02In maths
The total of xs followed by ys equals the total of xs plus the total of ys.
Picture it
Swipe sideways to see the whole picture.
++ and totalled once, or totalled separately and added. In this example both give 15, and the proof shows they always agree, whatever the books hold. Induction peels off the first score x of the first book. Then ih handles the rest, and Nat.add_assoc fixes the brackets.03In Lean
This code comes directly from the file checked by Lean.
def points : List Nat → Nat
| [] => 0
| x :: xs => x + points xs
example (xs ys : List Nat) : points (xs ++ ys) = points xs + points ys := by
induction xs with
| nil => simp [points]
| cons x xs ih => simp only [List.cons_append, points, ih, Nat.add_assoc]
The definition of points is a tiny program. An empty book earns zero. A nonempty book earns its first score plus the total for the rest.
For the proof, keep the second book fixed. If the first book has a head entry, set that score aside and apply ih to its tail. Only the parentheses differ after that step, exactly what Nat.add_assoc handles.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
xs ys : List Nat ⊢ points (xs ++ ys) = points xs + points ys
After line 5: example (xs ys : List Nat) : points (xs ++ ys) = points xs + points ys := by
xs ys : List Nat ⊢ points (xs ++ ys) = points xs + points ys
After line 6: induction xs with
xs ys : List Nat ⊢ points (xs ++ ys) = points xs + points ys
After line 7: | nil => simp [points]
no goals
After line 8: | cons x xs ih => simp only [List.cons_append, points, ih, Nat.add_assoc]
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 : [2, 5] ++ [1] = [1, 2, 5] := by
rfl
Tactic `rfl` failed: The left-hand side [2, 5] ++ [1] is not definitionally equal to the right-hand side [1, 2, 5] ⊢ [2, 5] ++ [1] = [1, 2, 5]
Append preserves order. Appending the bonus entry places it at the end of the first list.
The fix: Compare with [2, 5, 1]. A statement about total points is different from an equality of ordered lists.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Compute the total for the scores two, zero, and five.
Show a hint
Unfold points for each entry.
One more hint
A concrete list lets rfl finish the calculation.
- A little stretch
Prove that appending one bonus score increases the total by exactly that score.
Show a hint
Induct on the old scorebook.
One more hint
In the head case rewrite the tail with ih and regroup the additions.
def points : List Nat → Nat
| [] => 0
| x :: xs => x + points xs
example : points [2, 0, 5] = 7 := by
sorry
example (xs : List Nat) (bonus : Nat) : points (xs ++ [bonus]) = points xs + bonus := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
def points : List Nat → Nat
| [] => 0
| x :: xs => x + points xs
example : points [2, 0, 5] = 7 := by
rfl
example (xs : List Nat) (bonus : Nat) : points (xs ++ [bonus]) = points xs + bonus := by
induction xs with
| nil => simp [points]
| cons x xs ih => simp only [List.cons_append, points, ih, Nat.add_assoc]
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.