Stage 10 · Step 49 of 50

Combine two passes through a list.

Core Lean14 minutesLean 4.36.0

01The idea

A scorekeeper adds a bonus to every score, then doubles every result. Another scorekeeper does both operations as each score arrives.

Typo · Your guide for this idea
Typo pointing

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

Two passes or one combined pass give the same listExample with f adding a bonus of 5 and g doubling. The input list 3, 7, 10 splits into two routes. Top route, two passes: map f gives 8, 12, 15, then map g gives 16, 24, 30. Bottom route, one pass: map (fun x => g (f x)), which here is x goes to 2 times (x + 5), gives 16, 24, 30 directly. The two output lists are equal. Below, the Lean goal: List.map g (List.map f xs) = List.map (fun x => g (f x)) xs.xs3710two passesmap f+581215map g×2162430=samemap (fun x => g (f x))here: 2 × (x + 5)162430one pass⊢ List.map g (List.map f xs) = List.map (fun x => g (f x)) xs

Swipe sideways to see the whole picture.

Here 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.

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

After line 2
α : 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

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

    Apply the bonus and doubling rules to scores one and two.

  2. A little stretch

    Show that mapping across two joined lists agrees with mapping each list first.

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

08A quick check

1. Which expression applies f first?

Read the answer and explanation

g (f x). The inner application produces the value handed to the outer function.

2. What must match in the cons case?

Read the answer and explanation

Both the transformed head and the transformed tail. The heads compute to the same value and the induction hypothesis equates the tails.

One more idea in your toolkit.

Take your time. Progress is yours to mark.