Stage 10 · Step 49 of 50
Combine two passes through a list.
01The idea
A scorekeeper adds a bonus to every score, then doubles every result. Another scorekeeper does both operations as each score arrives.
Their lists should agree entry by entry.
02In maths
Mapping f and then g gives the same list as mapping the combined rule x ↦ g (f x).
Picture it
Swipe sideways to see the whole picture.
f adds a bonus of 5 and g doubles. Going through the list twice, or doing both jobs to each score as it passes, gives the same list. The induction proof checks one head at a time: on both sides the head becomes g (f x), and ih matches the tails.03In Lean
This code comes directly from the file checked by Lean.
example (f : α → β) (g : β → γ) (xs : List α) :
(xs.map f).map g = xs.map (fun x => g (f x)) := by
induction xs with
| nil => rfl
| cons x xs ih => simp only [List.map_cons, ih]
fun x => g (f x) builds one rule from two. Reading from the inside out keeps the order straight.
Expanding List.map_cons exposes a head and a tail on each side. Both heads are g (f x). The induction hypothesis relates the remaining lists.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
α : Type u_1 β : Type u_2 γ : Type u_3 f : α → β g : β → γ xs : List α ⊢ List.map g (List.map f xs) = List.map (fun x => g (f x)) xs
After line 2: (xs.map f).map g = xs.map (fun x => g (f x)) := by
α : Type u_1 β : Type u_2 γ : Type u_3 f : α → β g : β → γ xs : List α ⊢ List.map g (List.map f xs) = List.map (fun x => g (f x)) xs
After line 3: induction xs with
α : Type u_1 β : Type u_2 γ : Type u_3 f : α → β g : β → γ xs : List α ⊢ List.map g (List.map f xs) = List.map (fun x => g (f x)) xs
After line 4: | nil => rfl
no goals
After line 5: | cons x xs ih => simp only [List.map_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 : ([1].map (fun n => n + 1)).map (fun n => n * 2) = [3] := by
rfl
Tactic `rfl` failed: The left-hand side List.map (fun n => n * 2) (List.map (fun n => n + 1) [1]) is not definitionally equal to the right-hand side [3] ⊢ List.map (fun n => n * 2) (List.map (fun n => n + 1) [1]) = [3]
The expected result uses the operations in the wrong order. Adding one to 1 and then doubling gives 4.
The fix: Use [4] as the result. Apply the first function before the second.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Apply the bonus and doubling rules to scores one and two.
Show a hint
Start by computing the result of the first map.
One more hint
The second map doubles the intermediate values.
- A little stretch
Show that mapping across two joined lists agrees with mapping each list first.
Show a hint
Induct on the first list, since append examines that list.
One more hint
Simplify map_cons and cons_append, then use the induction hypothesis.
example : ([1, 2].map (fun n => n + 1)).map (fun n => n * 2) = [4, 6] := by
sorry
example (f : α → β) (xs ys : List α) : (xs ++ ys).map f = xs.map f ++ ys.map f := 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].map (fun n => n + 1)).map (fun n => n * 2) = [4, 6] := by
rfl
example (f : α → β) (xs ys : List α) : (xs ++ ys).map f = xs.map f ++ ys.map f := by
induction xs with
| nil => rfl
| cons x xs ih => simp only [List.cons_append, List.map_cons, ih]
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.