Stage 05 · Step 23 of 50

Addition in either order.

Core Lean8 minutesLean 4.36.0

01The idea

You can build a bridge using sections already checked. A proof can also reuse earlier lemmas.

Indy · Your guide for this idea
Indy pointing

Induction organizes the remaining work one successor at a time.

02In maths

For natural numbers a and b, a + b = b + a.

Picture it

The successor step of a + b = b + a, drawn as rows of blocksNumbers are drawn as rows of blocks: a long teal block a, a gold block b, and one small coral block for the extra 1. Top: the successor-case goal a + (b + 1) = b + 1 + a as two rows, a then b then 1, and b then 1 then a. A goal card repeats it: a b : Nat, ih : a + b = b + a, goal a + (b + 1) = b + 1 + a. The lemmas Nat.add_succ and Nat.succ_add pull each new 1 out to the end. Bottom: the rows become (a + b) + 1 and (b + a) + 1. A dashed box surrounds the a + b and b + a parts, which ih says are equal, and both rows end in the same 1, so no goals remain.THE GOAL AS BLOCKSSUCCESSOR CASEa b : Natih : a + b = b + a⊢ a + (b + 1) = b + 1 + aa + (b + 1)ab1b + 1 + ab1a(a + b) + 1ab1(b + a) + 1ba1Nat.add_succNat.succ_addpull each new 1 out to the endAFTER THE TWO LEMMASih : a + b = b + athe dashed parts match, andboth rows end in the same 1no goals

Swipe sideways to see the whole picture.

Picture each number as a row of blocks: a + b and b + a use the same blocks in a different order. Lean can’t just look, so the proof grows b one block at a time. The base case reuses last lesson’s fact that adding 0 on the left changes nothing. In the successor step, two library lemmas pull the new 1 to the end of each row, and then ih says the rest already matches.

03In Lean

This code comes directly from the file checked by Lean.

E23.leanTry it live (opens official Lean editor)
theorem trail_add_comm (a b : Nat) : a + b = b + a := by
  induction b with
  | zero =>
    exact (Nat.zero_add a).symm
  | succ b ih =>
    rw [Nat.add_succ, Nat.succ_add, ih]

An operation is commutative when swapping its two inputs preserves the result.

Addition is defined using recursion on its second argument. That makes induction on the second summand a useful choice here.

The proof reuses the left-zero and successor-addition lemmas. Read each rewrite as an equality already established by Lean.

04Step by step

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

After line 1
a b : Nat
⊢ a + b = b + a
After line 1: theorem trail_add_comm (a b : Nat) : a + b = b + a := by
a b : Nat
⊢ a + b = b + a
After line 2: induction b with
a b : Nat
⊢ a + b = b + a
After line 3: | zero =>
case zero
a : Nat
⊢ a + 0 = 0 + a
After line 4: exact (Nat.zero_add a).symm
no goals
After line 5: | succ b ih =>
case succ
a b : Nat
ih : a + b = b + a
⊢ a + (b + 1) = b + 1 + a
After line 6: rw [Nat.add_succ, Nat.succ_add, ih]
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

    Prove the special case with one summand equal to one.

  2. A little stretch

    Exchange the first two summands in the nested sum.

Exercises/E23.leanTry it live (opens official Lean editor)
example (n : Nat) : n + 1 = 1 + n := by
  sorry

example (a b c : Nat) : a + (b + c) = b + (a + c) := by
  sorry

08A quick check

1. What does commutative mean?

Read the answer and explanation

Changing input order preserves the result. Addition gives the same answer in either order.

2. Is using a previously checked lemma a valid proof step?

Read the answer and explanation

Yes. Lean checks that the lemma’s statement fits this use.

One more idea in your toolkit.

Take your time. Progress is yours to mark.