Stage 10 · Step 48 of 50
Change the values, keep the count.
01The idea
A teacher converts every quiz score into a letter grade. One score produces one grade, so the class list keeps its length.
Prove this for any conversion rule.
02In maths
For every function f and list xs, mapping f over xs preserves its length.
Picture it
Swipe sideways to see the whole picture.
map f sends every box through f on its own, so values change but no box is added or lost. The induction proof counts the same way. An empty list gives an empty list. For a list with a head x, each side has one head, and ih says the two tails have the same length.03In Lean
This code comes directly from the file checked by Lean.
example (f : α → β) (xs : List α) : (xs.map f).length = xs.length := by
induction xs with
| nil => rfl
| cons x xs ih => simp only [List.map_cons, List.length_cons, ih]
xs.map f applies the function at each position. The output values can even have a different type from the input values.
In the empty case both lengths are zero. In the head-and-tail case each side counts one head, and the induction hypothesis matches the two tail lengths.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
α : Type u_1 β : Type u_2 f : α → β xs : List α ⊢ (List.map f xs).length = xs.length
After line 1: example (f : α → β) (xs : List α) : (xs.map f).length = xs.length := by
α : Type u_1 β : Type u_2 f : α → β xs : List α ⊢ (List.map f xs).length = xs.length
After line 2: induction xs with
α : Type u_1 β : Type u_2 f : α → β xs : List α ⊢ (List.map f xs).length = xs.length
After line 3: | nil => rfl
no goals
After line 4: | cons x xs ih => simp only [List.map_cons, List.length_cons, ih]
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, 2].map (fun n => n + 1)).length = 1 := by
rfl
Tactic `rfl` failed: The left-hand side (List.map (fun n => n + 1) [2, 2]).length is not definitionally equal to the right-hand side 1 ⊢ (List.map (fun n => n + 1) [2, 2]).length = 1
Mapping changes both values, even when the inputs are duplicates. The list still has two entries.
The fix: Change the claimed length to two. Each input contributes one output.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Add ten to each of three scores and check the length.
Show a hint
The input is a concrete list.
One more hint
rfl can compute the mapped list and its length.
- A little stretch
Prove that two successive maps also preserve length.
Show a hint
List.length_map removes one map from inside length.
One more hint
Rewrite twice, once for each conversion.
example : ([1, 2, 3].map (fun n => n + 10)).length = 3 := by
sorry
example (f : α → β) (g : β → γ) (xs : List α) : ((xs.map f).map g).length = xs.length := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
example : ([1, 2, 3].map (fun n => n + 10)).length = 3 := by
rfl
example (f : α → β) (g : β → γ) (xs : List α) : ((xs.map f).map g).length = xs.length := by
rw [List.length_map, List.length_map]
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.