8. Induction follows the shape of data
Evaluation handles a particular finite input. A theorem about every list needs an argument that covers lists of arbitrary size. Induction supplies that argument by following the constructors of the data.
For a list, prove the property for [], then prove it for x :: xs assuming it already holds for xs. That assumption is the induction hypothesis. It is available only for the smaller input, which is why this reasoning is justified.
A complete list proof
def count {α : Type} : List α → Nat
| [] => 0
| _ :: xs => count xs + 1
theorem countAppend {α : Type} (xs ys : List α) :
count (xs ++ ys) = count xs + count ys := by
induction xs with
| nil => simp [count]
| cons x xs ih =>
simp [count, ih, Nat.add_assoc, Nat.add_comm]
In the base case, appending to an empty list leaves ys, and count [] is 0.
In the recursive case, both count and append expose the head. The induction hypothesis rewrites the count of xs ++ ys as the sum of their counts. Associativity and commutativity of addition finish the proof.
Induction supplies the hypothesis about the tail. Simplification then uses it to finish the local algebra.
The recursive call computes on the tail, and the induction hypothesis supplies evidence about that same tail.
Generalise changing parameters
Accumulator-based algorithms are a common source of weak induction hypotheses. If the recursive call changes an accumulator, the induction hypothesis must apply to every accumulator value.
def sum : List Nat → Nat
| [] => 0
| x :: xs => x + sum xs
def sumAcc : List Nat → Nat → Nat
| [], acc => acc
| x :: xs, acc => sumAcc xs (acc + x)
theorem sumAccSpec (xs : List Nat) (acc : Nat) :
sumAcc xs acc = acc + sum xs := by
induction xs generalizing acc with
| nil => simp [sumAcc, sum]
| cons x xs ih =>
simp [sumAcc, sum, ih, Nat.add_assoc]
theorem sumAccCorrect (xs : List Nat) : sumAcc xs 0 = sum xs := by
simpa using sumAccSpec xs 0
The strengthened statement exposes the accumulator invariant: the final answer is the initial accumulator plus the sum of the remaining list. generalizing acc prevents induction from fixing one accumulator too early.
When an induction proof fails at the recursive call, compare the call’s arguments with those in the induction hypothesis. If one argument changes, strengthen the theorem before adding tactics. A useful invariant often contains the missing generality.
Cases and induction are different
cases xs splits the proof into the empty and non-empty cases. Use it when inspecting the constructors is enough.
induction xs also supplies an induction hypothesis. Use induction when the argument needs a property of a tail, a subtree, or a smaller number.
Custom inductive types come with recursors generated from their constructors. These recursors explain why case analysis and induction work. You usually invoke them through tactics or recursive equations, but inspecting #check List.rec makes the connection explicit.
Exercises
- Prove
count xs = xs.lengthby induction. - Prove
sum (xs ++ ys) = sum xs + sum ys. - Explain why an induction hypothesis fixed at accumulator
0cannot justify the recursive call insumAcc.
Being able to strengthen a statement is more valuable than memorising a long tactic script.