Module 4: Tactic Proofs·Lesson 12 of 16·4 min read

Why Lean Accepts n + 0 = n but Not 0 + n = n: Induction on Nat

Here is a problem. Lean accepts n + 0 = n with a plain rfl, but it rejects 0 + n = n the same way. Both are obviously true. So why does one go through and the other fail?

The answer is in how addition is defined, and the fix is a proof method you may know from school: induction. This lesson shows why the asymmetry exists, how to run an induction proof in Lean, and how to prove a sum formula about your own function.

Why n + 0 = n is free

Natural numbers in Lean are built from two constructors: zero, and succ n, which means "one more than n". Addition is defined by recursion on the second argument. In words: n + 0 is n, and n + (m + 1) is (n + m) + 1.

So n + 0 = n is literally the first line of the definition. Lean can compute it even when n is unknown, and rfl works.

lean
-- n + 0 = n holds by definition, so rfl works
example (n : Nat) : n + 0 = n := rfl

Facts like this, which hold by unfolding definitions, are called definitional equalities.

Why 0 + n = n is not

Now look at 0 + n. To compute it, Lean needs to know whether n is zero or a successor, because that is what the definition splits on. With n unknown, it is stuck.

lean
-- 0 + n = n is not true by definition, so rfl fails
example (n : Nat) : 0 + n = n := rfl

This gives a type mismatch error. The statement is still true for every number. It just needs a real proof, one that covers every possible n.

The idea of induction

Induction says: to prove something for every natural number, prove two things. First, the base case: it holds for 0. Second, the step: if it holds for some k, it also holds for k + 1. Together these reach every number, the same way you can climb any number of stairs if you can get on the first one and always take one more.

In Lean, the induction tactic sets this up for you. It gives you one goal per constructor of Nat, and in the step it hands you the assumption for k, called the induction hypothesis.

Proving 0 + n = n

lean
theorem my_zero_add (n : Nat) : 0 + n = n := by
  induction n with
  | zero => rfl
  | succ k ih =>
    rw [Nat.add_succ, ih]

Walk through the goal view. In the zero case the goal is 0 + 0 = 0. Both sides compute to 0, so rfl works.

In the succ case the infoview shows this:

text
k : Nat
ih : 0 + k = k
⊢ 0 + (k + 1) = k + 1

The library lemma Nat.add_succ states n + m.succ = (n + m).succ, which is the second line of the addition definition. Rewriting with it turns the goal into (0 + k).succ = k + 1. Now the goal contains 0 + k, which is exactly the left side of ih. Rewriting with ih gives k.succ = k + 1, and those are the same thing, so the automatic rfl closes it.

The pattern to remember: in the step, rearrange the goal until the induction hypothesis appears inside it, then use it.

A sum formula with your own function

Here is a classic problem. Add up 0 + 1 + 2 + ... + n. The answer is n * (n + 1) / 2. Division on natural numbers rounds down, which makes proofs messy, so we prove the version without it: twice the sum equals n * (n + 1).

First, define the sum as a recursive function, the way lesson 6 did.

lean
-- our own sum: 0 + 1 + 2 + ... + n
def sumTo : Nat → Nat
  | 0 => 0
  | n + 1 => sumTo n + (n + 1)

#eval sumTo 4   -- 10

theorem sumTo_formula (n : Nat) : 2 * sumTo n = n * (n + 1) := by
  induction n with
  | zero => rfl
  | succ k ih =>
    rw [sumTo, Nat.mul_add, ih]
    simp only [Nat.mul_add, Nat.add_mul, Nat.mul_one, Nat.one_mul]
    omega

Notice the shape of sumTo matches the shape of induction: one case for 0, one for n + 1. That is not a coincidence. A function defined by recursion is usually easiest to reason about with induction that follows the same cases.

Reading the step

The base case is 2 * sumTo 0 = 0 * (0 + 1), and both sides compute to 0. In the step, three rewrites do the main work. rw [sumTo] unfolds one layer of the definition, so sumTo (k + 1) becomes sumTo k + (k + 1). Nat.mul_add spreads the 2 * across the sum. Then ih replaces 2 * sumTo k.

After those, the goal is:

text
⊢ k * (k + 1) + 2 * (k + 1) = (k + 1) * (k + 1 + 1)

This is now pure algebra with no sumTo left. The simp only line multiplies out the brackets on both sides. After it, both sides are sums of k * k, k and constants. The omega tactic, which handles linear arithmetic over natural numbers and integers, checks that they match. It treats k * k as a single unknown, which is enough here.

Animation: induction splits 0 + n = n into a base case closed by rfl and a step closed with Nat.add_succ and the hypothesis ih. Goal states as Lean 4 prints them.

Try this

On live.lean-lang.org, prove that adding the first n odd numbers gives n * n. The definition and proof follow the same pattern as sumTo.

lean
def oddSum : Nat → Nat
  | 0 => 0
  | n + 1 => oddSum n + (2 * n + 1)

#eval oddSum 5   -- 25

theorem oddSum_sq (n : Nat) : oddSum n = n * n := by
  induction n with
  | zero => rfl
  | succ k ih =>
    rw [oddSum, ih]
    simp only [Nat.mul_add, Nat.add_mul, Nat.mul_one, Nat.one_mul]
    omega

Before running it, click into the succ case and predict what the goal will look like after rw [oddSum, ih].

Sources

Try it yourselfMedium

Prove a doubling formula by induction

`double` is defined by recursion. Prove that it always equals `2 * n`. Use `induction n` and, in the step case, unfold one step of `double` with `simp [double]` or `rw [double]` before using the induction hypothesis. `omega` can finish linear arithmetic.

def double : Nat → Nat
  | 0 => 0
  | n + 1 => double n + 2

theorem double_eq (n : Nat) : double n = 2 * n := by
  sorry
Open in the Lean web editor

The official Lean editor runs in your browser and checks the file on Lean's server. Replace every sorry until no errors or warnings are left, then mark it solved.

The worked solution unlocks after your first attempt.

Saved in this browser. Pass the quiz while signed in to count it for the certificate.

Check what you learned

Answer all 3 questions. You need at least 2 right to pass.

1. Why does rfl prove n + 0 = n for an unknown n?

2. In the succ step of an induction proof, what is ih?

3. In the sumTo proof, what does rw [sumTo] do in the step case?