rw vs simp in Lean 4: Rewriting Goals Without Losing Track

5 min read

Here is a problem. You know a = b, and you want to prove a + 1 = b + 1. On paper you would just say "replace a with b". None of the tactics from the last lesson do that directly. exact and apply match whole goals, but here you need to change one piece inside the goal.

The tool for this is rewriting. Lean has two main versions: rw, which does one precise replacement you choose, and simp, which does many replacements on its own.

rw: replace using an equation

If h : a = b, then rw [h] finds every a in the goal and replaces it with b. After rewriting, rw also tries rfl, so if both sides now match, the goal closes by itself.

lean
-- rw replaces the left side of an equation with the right side
example (a b : Nat) (h : a = b) : a + 1 = b + 1 := by
  rw [h]

-- ← rewrites in the other direction: right side to left side
example (a b : Nat) (h : a = b) : b + 1 = a + 1 := by
  rw [← h]

In the first example the goal becomes b + 1 = b + 1, and the automatic rfl finishes it. That is why there is nothing after rw [h].

Direction matters

An equation has a left side and a right side, and rw always replaces the left side with the right side. In the second example, rw [h] would look for a and turn the goal into b + 1 = b + 1, which also works. But often only one direction makes progress.

When you need to replace the right side with the left side, put a left arrow in front: rw [← h]. You type the arrow as \l or \<-. Here it turns every b into a.

Animation: rw [h] replaces a with b, and the goal closes. The left arrow would replace b with a.

Rewriting with library lemmas

Equations do not have to be hypotheses. Any theorem whose statement is an equation can be used, including ones from the library. You can also list several, and rw applies them in order.

lean
-- rw with library lemmas, and with several steps at once
example (a b : Nat) : a + b + 0 = b + a := by
  rw [Nat.add_zero, Nat.add_comm]

#check @Nat.add_comm
#check @Nat.add_zero

The #check lines print the statements: Nat.add_zero : ∀ (n : Nat), n + 0 = n and Nat.add_comm : ∀ (n m : Nat), n + m = m + n. The first rewrite removes + 0. The second swaps a + b into b + a, and then rfl closes the goal.

Because these lemmas are "for all" statements, rw fills in the variables by matching. If you want to control which spot gets rewritten, you can give the arguments yourself, as in rw [Nat.add_comm a b].

Rewriting a hypothesis

Sometimes the thing you want to change sits above the line, not in the goal. Add at and the hypothesis name.

lean
-- rewriting a hypothesis with "at"
example (a b c : Nat) (h1 : a = b) (h2 : a + c = 10) : b + c = 10 := by
  rw [h1] at h2
  exact h2

After the rewrite, h2 says b + c = 10, which is exactly the goal.

When rw cannot find the pattern

rw is strict. It looks for the exact left side of your equation, written the same way. If it is not there, you get an error.

lean
-- rw fails when the pattern is not in the goal
example (a b c : Nat) (h : a = b) : c + 1 = c + 1 + 0 := by
  rw [h]

Lean says it did not find an occurrence of the pattern a in the target, and shows you the goal. When you see this, check the goal view. The term might be written in a different form than you expected.

simp: rewrite until stuck

simp is the simplifier. It has a large set of lemmas marked as simplification rules, such as x + 0 = x and x * 1 = x. It keeps rewriting with them until nothing more changes, then tries to close the goal.

lean
-- simp uses a big set of lemmas automatically
example (a b : Nat) : (a + 0) * 1 + b * 0 = a := by
  simp

-- simp only uses just the lemmas you list
example (a b : Nat) : (a + 0) * 1 + b * 0 = a := by
  simp only [Nat.add_zero, Nat.mul_one, Nat.mul_zero]

-- simp can also use your hypotheses
example (xs : List Nat) (h : xs = []) : xs.length = 0 := by
  simp [h]

simp only [...] restricts it to the lemmas you name. That is slower to write but much easier to read later, and it does not break when someone adds new simp rules to the library.

When simp is opaque

The downside of plain simp is that it hides its work. The proof says simp, and a reader cannot tell which facts it used. If a library update changes the simp set, a proof that worked can stop working, and the error will not tell you which lemma went missing.

There is a helper for this. Write simp? instead, and Lean reports a simp only call that does the same job.

lean
-- simp? tells you which lemmas simp used
example (a b : Nat) : (a + 0) * 1 + b * 0 = a := by
  simp?

Here Lean suggests simp only [Nat.add_zero, Nat.mul_one, Nat.mul_zero]. You can paste that in to make the proof readable.

simp can also simply fail. If none of its rules apply, it stops with the error that simp made no progress.

lean
-- simp only with nothing useful to apply makes no progress
example (a b : Nat) (h : a ≤ b) : a ≤ b + 1 := by
  simp only [Nat.mul_one]

That goal needs a fact about inequalities, not a rewrite. A direct lemma does it in one line.

lean
-- the same goal closed by a precise library lemma
example (a b : Nat) (h : a ≤ b) : a ≤ b + 1 :=
  Nat.le_succ_of_le h

A good habit: use rw when you know the exact step, simp only when you want several known steps, and plain simp when you are exploring.

Try this

On live.lean-lang.org, prove this with a single rw. Then check the goal view to see why nothing else is needed.

lean
example (x y : Nat) (h : x = y + 2) : x + 1 = y + 3 := by
  rw [h]

Now try rw [← h] on the same goal and read the error message. What pattern is it looking for?

Sources