Error Reference › Lean 4
Lean 4
rw: did not find the pattern (Lean 4)
rw looked for the left-hand side of your equation in the goal and it is not there — at least not in exactly that form.
What it looks like
Main.lean:7:6: error: Tactic `rewrite` failed: Did not find an occurrence of the pattern
b + a
in the target expression
a + b = c
a b c : ℕ
h : b + a = c
⊢ a + b = c
What does "rw: did not find the pattern (Lean 4)" mean?
rw needs the left-hand side of the equation to appear in the goal in essentially the same form; it only unfolds reducible definitions while looking. Lean 4.23 and later print Tactic `rewrite` failed: Did not find an occurrence of the pattern … in the target expression …, followed by the goal; Lean 4.22 and earlier print tactic 'rewrite' failed, did not find instance of the pattern in the target expression.
What causes "rw: did not find the pattern (Lean 4)"?
- The goal has a different form of the term (b + a instead of a + b, or different parentheses).
- The term is hidden inside a definition that rw does not unfold.
- The occurrence is under a binder (inside fun, ∀ or ∃) and mentions the bound variable — rw cannot rewrite there.
- The equation is used in the wrong direction, or the term is in a hypothesis rather than the goal.
How do I confirm the cause?
- Compare the pattern with the goal: look for swapped arguments, extra parentheses or a coercion arrow ↑.
- Hover over both terms — implicit arguments that differ are the usual hidden cause.
- Check whether you meant the goal or a hypothesis (rw … at h).
How do I fix "rw: did not find the pattern (Lean 4)"?
- Rewrite right-to-left with rw [← h] if the goal contains the right-hand side.
- Rewrite in a hypothesis with rw [h] at h2.
- Unfold the definition first (unfold f or simp only [f]), or restate the goal with show.
- Use simp only [h], which can also rewrite under binders.
How do I stop it happening again?
- Put both sides in a normal form first — simp, or ring_nf in Mathlib — before rewriting.
- Keep lemma statements in the same form as the goals you use them on.