Stage 10 · Step 50 of 50

Capstone: combine two scoreboards.

Core Lean15 minutesLean 4.36.0

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.

Typo · Your guide for this idea
Typo pointing

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

Join then total, or total each then add: the same scoreExample with scorebook xs = 3, 5 and scorebook ys = 4, 2, 1. Top route, points (xs ++ ys): join the books into 3, 5, 4, 2, 1 and total them to get 15. Bottom route, points xs + points ys: xs totals 8, ys totals 7, and 8 + 7 = 15. The two totals are equal. Right panel, the cons step of the induction: the left side x + points (xs ++ ys) becomes x + (points xs + points ys) using ih. The right side (x + points xs) + points ys becomes the same thing using Nat.add_assoc, which only moves the parentheses.points (xs ++ ys)join the books, then total35421points15=same totalpoints xs + points ystotal each book, then add3584217+15The step for a head xleft sidex + points (xs ++ ys)↓ ihx + (points xs + points ys)right side(x + points xs) + points ys↓ Nat.add_assoc moves the bracketsx + (points xs + points ys)same on both sides

Swipe sideways to see the whole picture.

Two scorebooks can be joined with ++ 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.

E50.leanTry it live (opens official Lean editor)
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.

After line 5
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

Captured from Lean 4.36.0
no goals

A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.

07Your turn

sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.

  1. Warm-up

    Compute the total for the scores two, zero, and five.

  2. A little stretch

    Prove that appending one bonus score increases the total by exactly that score.

Exercises/E50.leanTry it live (opens official Lean editor)
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

08A quick check

1. Why induct on xs?

Read the answer and explanation

Append examines its first list. Splitting the first book makes the definition of xs ++ ys compute.

2. Which law settles the final regrouping?

Read the answer and explanation

Associativity of addition. The order of the three amounts stays fixed. Nat.add_assoc changes their grouping.

One more idea in your toolkit.

Take your time. Progress is yours to mark.