Chapter 3 · Numbers and induction
Rewriting
Step 1 of 22 · Concept
rw replaces one side of an equation by the other
In this lesson you use equations and library facts to change a goal.
With h : a = b, rw [h] replaces every a in the goal by b. Then it closes the goal if both sides are the same,
or are given numbers that compute alike. rw [h1, h2] rewrites with two facts, in order.
That closing check never unfolds a definition, not even + on unknown numbers. a + 0 = a stays open:
example (a b : Nat) (h : b = a) : b + 0 = a := by
rw [h]
Main.lean:1:47: error: unsolved goals
a b : Nat
h : b = a
⊢ a + 0 = a
Step 2 of 22 · Quiz
Predict the goal after rw
The goal panel shows this. What does it show after rw [h]?
x y : Nat
h : x = 3
⊢ x + 1 = y
Step 3 of 22 · Demo
See rw work
Press Run: Lean accepts all three. Put the cursor on each rw and read the goal panel. The second proof lists
both facts in one rw; the third rewrites the hypothesis h2, with at h2.
Step 4 of 22 · Trace
Watch the goal change
Press Next: each frame shows the goal as the panel will show it when you run this proof.
example (x y : Nat) (h1 : x = 3) (h2 : y = 4) : x + y = 7 := by
rw [h1]
rw [h2]- Line 2 is next. The goal is
x + y = 7. rw [h1]replacedxby3: the goal is3 + y = 7. Line 3 is next.rw [h2]made it3 + 4 = 7. Both sides compute to7, sorwclosed the goal.
Step 5 of 22 · Concept
rw [← h] goes the other way
rw [h] with h : a = b looks for a. rw [← h] looks for b and puts a in its place. Type ← as \l, or
write <-.
h : 5 = x | looks for | puts in its place |
|---|---|---|
rw [h] | 5 | x |
rw [← h] | x | 5 |
Step 6 of 22 · Try it
Try it: turn the arrow
Prove ten. The goal has no 5, so rw [h] finds nothing: rewrite with h the other way. Any proof Lean
accepts passes.
rw [← h] replaces each x by 5. Then 5 + 5 = 10 computes, and rw closes the goal.
Step 7 of 22 · Concept
rw [h] at h2 rewrites a hypothesis
rw [h1] at h2 rewrites the hypothesis h2 instead of the goal. With h1 : a = 3, it turns h2 : b = a + 1 into
h2 : b = 3 + 1.
Now exact h2 closes ⊢ b = 4: 3 + 1 computes to 4, so h2 fits.
Step 8 of 22 · Exercise
Your turn: b is 4
Prove b_four. Try it this way: rewrite a hypothesis, then close the goal with it. Any proof Lean accepts passes.
rw [h1] alone looks in the goal, b = 4, which has no a:
error: Tactic `rewrite` failed: Did not find an occurrence of the pattern
a
The a is in h2: rewrite there, with at h2.
rw [h1] at h2
Step 9 of 22 · Concept
Library facts are theorems
Lean's library holds many proved theorems. You can use them in rw as you use your own:
| name | states, for all numbers |
|---|---|
Nat.add_zero | n + 0 = n |
Nat.zero_add | 0 + n = n |
Nat.add_comm | n + m = m + n |
Nat.add_assoc | n + m + k = n + (m + k) |
Nat.mul_comm | n * m = m * n |
Nat.mul_one | n * 1 = n |
Each is a theorem like yours: a name, a statement, a proof the kernel checked (lesson 2).
Step 10 of 22 · Quiz
Predict the missing line
#check shows a theorem's statement. Type the missing line.
#check Nat.add_comm
#check Nat.mul_one
Step 11 of 22 · Demo
See it run
Press Run and compare with your answer. (n m : Nat) says the fact holds for every n and m.
Step 12 of 22 · Concept
+ groups from the left
a + b + c means (a + b) + c: Lean adds from the left. So Nat.add_assoc moves brackets:
Nat.add_assoc (n m k : Nat) : n + m + k = n + (m + k)
A fact's number can be a whole sum. In b + a + c, the outer sum adds b + a and c. So
Nat.add_comm (b + a) c is the fact b + a + c = c + (b + a).
Step 13 of 22 · Concept
Nat.add_comm a b chooses the numbers
rw [Nat.add_comm] rewrites the first sum it finds, the outermost. On b + a + c it gives c + (b + a).
To choose, give the numbers: Nat.add_comm a b is exactly a + b = b + a. rw [Nat.add_comm a b] rewrites that
sum and no other.
#check Nat.add_comm 2 3
Nat.add_comm 2 3 : 2 + 3 = 3 + 2
Step 14 of 22 · Quiz
Which rewrite turns it around?
The goal is ⊢ a + b = c. Which tactic changes it to ⊢ b + a = c?
Step 15 of 22 · Try it
Try it: one more fact
rw [Nat.add_zero] left the goal a + b = b + a. Replace sorry with a rewrite that closes it. Any proof Lean accepts
passes.
Nat.add_comm turns a + b into b + a. Then both sides are the same, and rw closes the goal.
Step 16 of 22 · Concept
h.symm turns an equation around
From h : a = b, h.symm is a proof of b = a. So exact h.symm closes the goal ⊢ b = a:
example (a b : Nat) (h : a = b) : b = a := by
exact h.symm
Use it when a hypothesis has the right sides, the wrong way round.
Step 17 of 22 · Concept
exact? searches the library
A library fact may exist whose name you do not know. exact? searches for one that closes the goal. It
prints two lines:
Try this:
[apply] exact Nat.zero_add n
Copy the line into your proof in place of exact?, without the [apply] marker. A proof you submit may not
contain exact? itself: the check refuses it.
Step 18 of 22 · Demo
Ask the library
Press Run. exact? finds the fact and prints its suggestion. Lean accepts the proof, but only as a way to search.
Step 19 of 22 · Exercise
Your turn: a sum turned around
Prove turn with library facts. Three rewrites do it, one per line; any proof Lean accepts passes.
A bare rw [Nat.add_comm] turns the outermost sum. Name the numbers: Nat.add_comm a b turns a + b into b + a.
Now the left side is b + a + c, which is (b + a) + c. Nat.add_comm (b + a) c moves c to the front.
The goal is then c + (b + a) = c + b + a: only the brackets differ. Which fact in the table moves brackets?
Step 20 of 22 · Check yourself
Check yourself: from c to b * a
No shape this time.
Prove swap_mul by rewriting. Any proof Lean accepts passes.
Three changes: drop the * 1, replace c, turn a product around.
Step 21 of 22 · Challenge · optional
Challenge: the middle two
Optional.
Prove mid. One library fact swaps the last two numbers of a sum. Find it.
Replace sorry with exact? and press Run: it prints the fact it found.
The line exact? printed names a library fact. #check it, as in the step See it run, to read its statement.
Copy that line without [apply], or rewrite once with the fact. A proof that still says exact? does not pass.
Step 22 of 22 · Recap
Recap
rw [h]replaces the left side ofhby its right side;rw [← h]goes the other way.rwthen closes a goal whose sides are the same, or are given numbers that compute alike.rw [h] at h2rewrites a hypothesis;h.symmturns an equation around.- Library facts are theorems:
Nat.add_comm a bchooses its numbers. exact?finds a fact: copy its line without[apply].
Next: lesson 10 chains steps with calc and proves ≤.
Review
3 questions from this lesson, in a new order each time. Answer from memory: 2 of 3 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
example (x y : Nat) (h1 : x = 3) (h2 : y = 4) : x + y = 7 := by
rw [h1]
rw [h2]
example (x y : Nat) (h1 : x = 3) (h2 : y = 4) : x + y = 7 := by
rw [h1, h2]
example (x y : Nat) (h1 : x = 3) (h2 : y = x + 4) : y = 7 := by
rw [h1] at h2
exact h2
theorem ten (x : Nat) (h : 5 = x) : x + x = 10 := by
sorry
theorem b_four (a b : Nat) (h1 : a = 3) (h2 : b = a + 1) :
b = 4 := by
sorry
#check Nat.add_comm
#check Nat.mul_one
theorem turn_two (a b : Nat) : a + b + 0 = b + a := by
rw [Nat.add_zero]
sorry
example (n : Nat) : 0 + n = n := by
exact?
theorem turn (a b c : Nat) : a + b + c = c + b + a := by
sorry
theorem swap_mul (a b c : Nat) (h : c = a * b) :
c * 1 = b * a := by
sorry
theorem mid (a b c : Nat) : a + b + c = a + c + b := by
sorry