Stage 07 · Step 32 of 50

Induction on a list.

Core Lean12 minutesLean 4.36.0

01The idea

A train is empty or has a first carriage followed by the rest. A list has the same two possibilities.

Indy · Your guide for this idea
Indy pointing

Induction on a list follows its constructors.

02In maths

The length of xs appended to ys equals the length of xs plus the length of ys.

Picture it

Lists are trains, and induction follows how they are builtTop: a list is one of two things. Either the empty list, drawn as an empty track ending at a buffer stop, or x cons xs, drawn as one front carriage x coupled to the rest of the train xs. Middle left, case nil: the empty list appended to ys is just the train ys, so its length is 0 plus the length of ys. Middle right, case cons: carriage x, then the carriages of xs, then the carriages of ys. A brace over xs and ys says the induction hypothesis already counts those correctly. The front carriage x adds one to both sides. Bottom: the induction hypothesis ih, (xs ++ ys).length = xs.length + ys.length, and the goal (x :: xs ++ ys).length = (x :: xs).length + ys.length, which simp closes using ih.A LIST IS ONE OF TWO THINGS[]empty trainorxxsa front carriage, then the rest of the trainx :: xsCASE nil[] ++ ysysnothing in front: 0 + ys.lengthCASE consih: these are already counted rightx+1xsysthe front carriage adds 1 to both sidesih : (xs ++ ys).length = xs.length + ys.length⊢ (x :: xs ++ ys).length = (x :: xs).length + ys.lengthsimpuses ih

Swipe sideways to see the whole picture.

Every list is built in one of two ways: the empty train [], or a front carriage x coupled to a shorter train xs. So induction xs gives two cases. In the cons case, the hypothesis ih already counts the carriages of xs ++ ys, and the one extra carriage adds 1 to both sides.

03In Lean

This code comes directly from the file checked by Lean.

E32.leanTry it live (opens official Lean editor)
theorem trail_length_append (xs ys : List α) :
    (xs ++ ys).length = xs.length + ys.length := by
  induction xs with
  | nil => simp
  | cons x xs ih =>
    simp [ih, Nat.add_assoc, Nat.add_comm]

The notation [] is an empty list. The notation x :: xs puts a head element in front of a tail list.

The constructor cons builds the nonempty case. Its tail is the smaller input for induction.

The type variable α lets the theorem work for lists of any element type. Type α with \alpha.

04Step by step

Choose a line to see Lean’s recorded goal state after that line.

After line 2
α : Type u_1
xs ys : List α
⊢ (xs ++ ys).length = xs.length + ys.length
After line 2: (xs ++ ys).length = xs.length + ys.length := by
α : Type u_1
xs ys : List α
⊢ (xs ++ ys).length = xs.length + ys.length
After line 3: induction xs with
α : Type u_1
xs ys : List α
⊢ (xs ++ ys).length = xs.length + ys.length
After line 4: | nil => simp
no goals
After line 5: | cons x xs ih =>
case cons
α : Type u_1
ys : List α
x : α
xs : List α
ih : (xs ++ ys).length = xs.length + ys.length
⊢ (x :: xs ++ ys).length = (x :: xs).length + ys.length
After line 6: simp [ih, Nat.add_assoc, Nat.add_comm]
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

    Show that appending to the empty list preserves the other list’s length.

  2. A little stretch

    Show how the length changes when one element is put at the front.

Exercises/E32.leanTry it live (opens official Lean editor)
example (ys : List Nat) : ([] ++ ys).length = ys.length := by
  sorry

example (x : α) (xs : List α) : (x :: xs).length = xs.length + 1 := by
  sorry

08A quick check

1. What are the two list constructors?

Read the answer and explanation

Empty and cons. The cons constructor attaches a head element to a tail list.

2. What does the induction hypothesis describe?

Read the answer and explanation

The shorter tail list. The recursive constructor provides the tail as the smaller input.

One more idea in your toolkit.

Take your time. Progress is yours to mark.