Motive is not type correct (Lean 4)
rw tried to replace a term, but other parts of the goal depend on that exact term, so the rewritten goal would no longer type-check.
What it looks like
Main.lean:9:6: error: Tactic `rewrite` failed: motive is not type correct:
fun _a => …
Error: …
Explanation: The rewrite tactic rewrites an expression 'e' using an equality 'a = b' by the following process. …
What does "Motive is not type correct (Lean 4)" mean?
rw abstracts every occurrence of the left-hand side into a function called the motive, then rewrites using congruence. If an occurrence is needed for something else to type-check — a proof that mentions it, or an index — the motive does not type-check and rw stops. The message says rewrite even when you wrote rw. Lean 4.23 and later print Tactic `rewrite` failed: motive is not type correct:; Lean 4.22 and earlier print tactic 'rewrite' failed, motive is not type correct:. Lean's own explanation follows, suggesting the occs option, simp or conv.
What causes "Motive is not type correct (Lean 4)"?
- The term you rewrite also appears inside a type — a proof argument, a Fin n bound, an index proof or a Decidable instance.
- Dependent types: rewriting n in a goal about v : Vector α n.
- Every occurrence is rewritten when only some can safely change.
How do I confirm the cause?
- Read the motive (fun _a => …): each _a marks an occurrence rw tried to replace — find the one inside a type or a proof.
- Try simp only [h]; if it works, a dependency was the problem.
How do I fix "Motive is not type correct (Lean 4)"?
- Use simp only [h] instead of rw [h] — simp can handle many of these dependencies.
- Rewrite only some occurrences: rw (occs := .pos [1]) [h], or nth_rewrite in Mathlib.
- Use conv to rewrite inside one specific part of the goal.
- If h : x = e with x a local variable, subst h replaces it everywhere at once.
How do I stop it happening again?
- State lemmas so the term you rewrite does not appear inside dependent arguments.
- Prefer simp for rewriting in dependent positions and under binders.