Rewriting failures in Lean usually come from a small set of recurring issues.
Rewriting failures in Lean usually come from a small set of recurring issues. This section lists the typical failure modes and the corresponding fixes, with a bias toward fast diagnosis.
Pattern mismatch
rw [h] does nothing when the left-hand side of h does not match any subterm.
-- h : a = b
-- goal contains f a, but rewrite expects aFix by matching the exact shape:
- Apply congruence:
rw [congrArg f h] - Reshape the goal: unfold definitions or introduce arguments
- Use
simp [h]if normalization is intended
Wrong orientation
The equality is correct but used in the wrong direction.
rw [h] -- no effect
rw [← h] -- worksAlways check both orientations. Prefer the direction that moves toward a canonical form.
Hidden occurrences under binders
rw cannot reach terms inside lambdas or quantifiers.
-- goal: (fun x => x + a) = (fun x => x + b)Fix by exposing the body:
funext x
rw [h]or use conv for targeted rewriting.
Missing arguments or implicit inference failure
A lemma requires parameters that are not inferred.
rw [Nat.add_comm] -- ambiguousFix by supplying arguments:
rw [Nat.add_comm a b]or by reshaping the goal so inference succeeds.
Rewriting too much
rw replaces all matching occurrences, which can break structure.
-- multiple occurrences of a
rw [h] -- changes allFix with conv to target a specific occurrence.
Rewrite loops and instability
simp or repeated rw oscillates between equivalent forms.
-- conflicting rules
a + b ↔ b + aFix by:
- Removing symmetric rules from the simp set
- Using
simp only [...] - Applying
rwexplicitly for non-canonical steps
Dependent type mismatch
Rewriting changes indices and breaks types.
-- goal: P b, hypothesis: P a, h : a = bFix by rewriting in the correct place:
rw [← h]
exact Haor rewrite the hypothesis instead of the goal.
Function equality not reducing
Trying to rewrite functions directly fails.
-- goal: f = g
rw [h] -- no effectFix with extensionality:
funext x
rw [h]Structure equality not decomposed
Rewriting a whole structure fails when field-level reasoning is needed.
-- goal: p = q
rw [h] -- insufficientFix with ext:
ext
-- prove field equalitiesSimplifier not doing what you expect
simp may miss a rule or apply too many.
Fix by:
- Adding lemmas locally:
simp [h] - Restricting rules:
simp only [...] - Inspecting suggestions:
simp?
Diagnostic workflow
When rewriting fails:
- Inspect the goal shape.
- Check the lemma type and orientation.
- Identify whether the occurrence is visible.
- Try
rwin both directions. - Switch to
simp,calc, orconvif needed. - Reduce the problem to a smaller subgoal.
Minimal example reduction
Isolate the issue:
-- extract a smaller goal
have : ... := by
...Work on the reduced form until the rewrite succeeds, then reinsert into the main proof.
Summary
Most rewriting issues reduce to mismatched patterns, wrong direction, hidden structure, or uncontrolled rule sets. A systematic approach to inspecting goals, choosing tools, and applying small steps resolves these problems efficiently.