Stage 10 · Step 48 of 50

Change the values, keep the count.

Core Lean12 minutesLean 4.36.0

01The idea

A teacher converts every quiz score into a letter grade. One score produces one grade, so the class list keeps its length.

Typo · Your guide for this idea
Typo pointing

Prove this for any conversion rule.

02In maths

For every function f and list xs, mapping f over xs preserves its length.

Picture it

map changes every value but makes exactly one output per inputTop row: an input list of four scores, 72, 95, 60 and 88, with length 4. Each score goes down through its own copy of the function f into the output list below: C, A, D and B, also length 4. The first column is marked as the head, x above and f x below. The other three are bracketed as the tail, xs above and xs.map f below. Footnote: each head counts one on its side, and the induction hypothesis ih says the two tails have the same length.inputscoresoutputgradesxxs72956088length 4ffffone output per inputCADBlength 4f xxs.map fheads: one eachtails: equal, by ih

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.

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

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

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

    Add ten to each of three scores and check the length.

  2. A little stretch

    Prove that two successive maps also preserve length.

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

08A quick check

1. What does map do with each input entry?

Read the answer and explanation

It produces one output entry. The function is applied once at each list position.

2. What does ih tell you in the cons case?

Read the answer and explanation

The mapped tail has the same length as the tail. Adding one head to both tails preserves their equality of lengths.

One more idea in your toolkit.

Take your time. Progress is yours to mark.