Stage 02 · Step 9 of 50
Swap equal things.
01The idea
If two labels name the same box, you can swap the labels. Rewriting replaces an expression using a proved equality.
The surrounding expression keeps its meaning.
02In maths
If a = b, then a + 1 = b + 1.
Picture it
Swipe sideways to see the whole picture.
h : a = b is a swap card: wherever you see a, you may put b. rw [h] makes that swap in the goal, turning the left side into b + 1. Now both sides are exactly the same, so rw finishes the proof on its own.03In Lean
This code comes directly from the file checked by Lean.
example (a b : Nat) (h : a = b) : a + 1 = b + 1 := by
rw [h]
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
a b : Nat h : a = b ⊢ a + 1 = b + 1
After line 1: example (a b : Nat) (h : a = b) : a + 1 = b + 1 := by
a b : Nat h : a = b ⊢ a + 1 = b + 1
After line 2: rw [h]
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 (n : Nat) (h : n = 2) : 3 = 3 := by
rw [h]
Tactic `rewrite` failed: Did not find an occurrence of the pattern n in the target expression 3 = 3 n : Nat h : n = 2 ⊢ 3 = 3
The goal does not contain the expression n that this rewrite would replace.
The fix: Inspect the goal before choosing a rewrite.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Rewrite the goal using the supplied equality between a and b.
Show a hint
The goal contains the left side of the hypothesis.
One more hint
Put the equality’s name inside the rewrite list.
- A little stretch
Connect a to c using the two supplied equalities.
Show a hint
First replace a with b.
One more hint
Then replace b with c in the same rewrite list.
example (a b : Nat) (h : a = b) : a * 2 = b * 2 := by
sorry
example (a b c : Nat) (h₁ : a = b) (h₂ : b = c) : 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 (a b : Nat) (h : a = b) : a * 2 = b * 2 := by
rw [h]
example (a b c : Nat) (h₁ : a = b) (h₂ : b = c) : a = c := by
rw [h₁, h₂]
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.