Stage 05 · Step 23 of 50
Addition in either order.
01The idea
You can build a bridge using sections already checked. A proof can also reuse earlier lemmas.
Induction organizes the remaining work one successor at a time.
02In maths
For natural numbers a and b, a + b = b + a.
Picture it
Swipe sideways to see the whole picture.
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.
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.
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
no goals
A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.
06A common slip
Lean is telling us something useful here.
example (a b : Nat) : a + b = b + a := by
rfl
Tactic `rfl` failed: The left-hand side a + b is not definitionally equal to the right-hand side b + a a b : Nat ⊢ a + b = b + a
Commutativity is true but is not a computation rule for these unknown inputs.
The fix: Use the induction proof or the checked commutativity lemma.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Prove the special case with one summand equal to one.
Show a hint
The library already has addition commutativity.
One more hint
Apply the library theorem to the supplied arguments.
- A little stretch
Exchange the first two summands in the nested sum.
Show a hint
Associativity lets you regroup before swapping.
One more hint
Regroup, commute the first pair, then regroup back.
example (n : Nat) : n + 1 = 1 + n := by
sorry
example (a b c : Nat) : a + (b + c) = b + (a + c) := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
example (n : Nat) : n + 1 = 1 + n := by
exact Nat.add_comm n 1
example (a b c : Nat) : a + (b + c) = b + (a + c) := by
rw [← Nat.add_assoc, Nat.add_comm a b, Nat.add_assoc]
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.