Structural Induction on Lists

Structural induction proves a property for empty input and shows how extending an arbitrary shorter list preserves that property.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Prove a length relation
  2. Connect a proof to an implementation
  3. Preserve the statement when one head is added
  4. Use the hypothesis only on the smaller structure

Prove a length relation

For concatenation with a fixed list B, the empty list joined with B has length \(|B|\). If the property holds for tail A, adding a head to A adds one to the concatenated length too. This mirrors first-item and remainder processing.

Connect a proof to an implementation

A merge routine might preserve the total number of items by consuming one head and writing one output at each step. Its postcondition needs that relation along with sortedness to describe a correct result.[1]

Preserve the statement when one head is added

Prove that appending two lists gives length equal to the sum of their lengths. Fix a second list \(B\). For an empty first list, appending yields \(B\). For a first list with head and tail, appending retains the head and appends \(B\) to the tail. See recursive list processing.

Base: len([] + B) = len(B)
Step: len([x] + T + B) = 1 + len(T + B)
By hypothesis: 1 + len(T) + len(B)
Hence len([x] + T) + len(B)

Use the hypothesis only on the smaller structure

The induction assumption supplies the tail identity, so the proof never assumes the target equality for the whole first list. If a function can drop the head on some input, the step needs to account for that branch explicitly.

References

  1. Jeff Erickson, Algorithms ↩