Unit tests check only the handful of inputs you choose by hand. Here we state a property of our function as a theorem and prove that it **holds for every possible input list**.
Put this part’s Lean code in an empty Induction.lean and run lean Induction.lean. The example defines sumList again, so keep it separate from the file you used in Part 4.
1. When You Need simp and Mathematical Induction (induction)
As previewed at the end of Part 5, Nat.add is defined by recursion on its second argument, so n + 0 = n closes immediately by rfl. Adding zero on the left in 0 + n = n cannot reduce by computation alone while n is a variable; in our first example below, simp applies the standard library theorem Nat.zero_add, which was itself proved by induction on natural numbers.
Now we use induction directly to prove that calling sumList from Part 4 on two concatenated lists (xs ++ ys) always equals the sum of calling sumList on each list separately!
Writing induction xs with splits the first list into the empty case and the head-cons case, and in the second case it also hands us an induction hypothesis stating that the equation already holds for the shorter tail. Passing a function name as in simp [sumList] tells the simplifier to unfold our own definition during the proof.1
Why do we induct on the first list xs rather than the second list ys? Because both list concatenation (xs ++ ys) and sumList peel one head element at a time off their first (left) list, inducting on the first list aligns the proof’s inductive step with the exact direction in which the functions compute.
In the formula below, stands for sumList and denotes list concatenation. The symbol defines the property on the left to mean the equation on the right, and the upside-down A symbol (for all) reads “for every possible list or element.”
The two obligations above the horizontal bar correspond to the two branches we fill in code: the left side is the proof for the empty list, and the right side checks that assuming the property for the tail is enough to establish it when one more head element is prepended. In code, ih is that piece of evidence for .2
nil and cons are the constructor names for [] and x :: rest from Part 4. induction xs with uses those names for its two goals. After cons, give names to the head and tail and then to the evidence Lean gives you for that tail, ih.
theorem zero_add_left (n : Nat) : 0 + n = n := by
simp
def sumList (xs : List Nat) : Nat :=
match xs with
| [] => 0
| x :: rest => x + sumList rest
theorem sumList_singleton (x : Nat) : sumList [x] = x := by
simp [sumList]
theorem sumList_append (xs ys : List Nat) :
sumList (xs ++ ys) = sumList xs + sumList ys := by
induction xs with
| nil =>
simp [sumList]
| cons x rest ih =>
simp [sumList, ih, Nat.add_assoc]2. Following the Proof Line by Line
Because the code in Part 6 contains no #eval or #check commands, running lean in a terminal finishes silently with no output. Silent success is completely normal when a proof compiles without errors! In VS Code, place your cursor just after the => of each branch of induction xs with to see its goal in InfoView.
Base case (
| nil =>): the first listxsis the empty list[]. The left-hand sidesumList ([] ++ ys)reduces tosumList ys, and the right-hand sidesumList [] + sumList ysreduces to0 + sumList ys. Tellingsimp [sumList]to unfoldsumListsimplifies both sides tosumList ysand closes the branch.Inductive step (
| cons x rest ih =>): we are given the induction hypothesisih : sumList (rest ++ ys) = sumList rest + sumList ysfor the shorter tailrest, and must show that the equation still holds when one more head elementxis prepended asx :: rest. Passing the definition ofsumList, the induction hypothesisih, and associativity of natural-number additionNat.add_assoc((a + b) + c = a + (b + c)) tosimpnormalizes both sides tox + (sumList rest + sumList ys)and completes the proof.
simp rewrites the goal using registered rules and performs simple reductions. The brackets supply extra rules for this proof. Adding sumList lets it unfold that function; adding ih lets it replace the sum over the tail with the right-hand side you've already proved.
The induction hypothesis does its work at the second equals sign below. After unfolding concatenation and sumList once in the first equation, we replace the uncomputed sum of the tail with our induction hypothesis.
Re-associating the parentheses on the third line is where Nat.add_assoc is used. Once that final shape is reached, it matches the right-hand side of our theorem.3
If a single bug had slipped into our function (say, returning 1 instead of 0 on the empty list), the two sides would disagree in the nil branch and the file would refuse to compile. In Part 7, we will look at lake project structure, the IO monad, and do notation so we can package verified pure functions into a native executable that runs in a terminal.
Footnotes
- Jeremy Avigad et al. Theorem Proving in Lean 4. Induction and Recursion · Lean Language Reference 4.36.0. Natural Numbers: Arithmetic (`Nat.add`). Explains second-argument recursion in
Nat.addand how theinductiontactic generates subgoals and induction hypotheses. ↩ - Lean Language Reference 4.36.0. Tactic Reference: Eliminating Inductive Types · Tactic Reference: Simplification.
inductioncreates one subgoal per constructor with induction hypotheses for recursive arguments, whilesimprewrites the goal using registered and user-supplied equations. ↩ - Lean Language Reference 4.36.0. Linked Lists: API Reference · Natural Numbers: Arithmetic. Provides the definitions of list concatenation and natural-number arithmetic used in this derivation. ↩