Repository navigation
Description
Prerequisites
Please put an X between the brackets as you perform the following steps:
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release, for example on
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”)
Description
The docstring of the method Lean.MVarId.replaceLocalDeclDefEq, in Lean.Meta.Tactic.Replace, states that this method is supposed to have the following property:
If
typeNewis equal to current type offvarId, then returnsmvarIdunchanged.
However, the equality check does not instantiate metavariables at fvarId, leading to unpredictable behaviour in use cases.
Context
I noticed unpredictable behaviour in some Mathlib tactics (ring_nf and push_neg) and this turned out to be the root cause. See details below in "Impact".
Steps to Reproduce
import Lean.Elab.Tactic
open Lean Elab Tactic
elab "change_to" t:term "at" h:ident : tactic => do
let e : Expr ← elabTerm t none
let fvarId ← getFVarId h
liftMetaTactic1 fun g ↦ do
let e₀ ← fvarId.getType
logInfo m!"{e₀} and {e} equal: {e₀.equal e}"
logInfo m!"{e₀} and {e} equal after mvar instantiation: {(← instantiateMVars e₀).equal e}"
let g' ← g.replaceLocalDeclDefEq fvarId e
logInfo m!"old goal {g.name}, new goal {g'.name}"
return g'
example {x y : Nat} (h : x ≤ y) : True := by
change_to x ≤ y at h
exact trivial
example {x y : Nat} : True := by
have h : x ≤ y := sorry
change_to x ≤ y at h
exact trivialExpected behavior: In both cases, the goal g' should be the same as the goal g.
Actual behavior: The goal g' is the same as the goal g in the first case, it's different in the second case.
Versions
Lean 4.23.0-nightly-2025-08-13
Target: x86_64-unknown-linux-gnu
Impact
This method is invoked in Lean.Meta.applySimpResultToLocalDecl, and as a result the behaviour of applySimpResultToLocalDecl doesn't quite match what is stated in its documentation ("we can avoid creating a new variable if there are dependencies").
Here is the downstream behaviour in two Mathlib tactics, which I believe is confusing for users:
import Mathlib
example {x y : ℚ} (h : x ≤ y) : True := by
push_neg at h -- fail, "push_neg made no progress"
example {x y : ℚ} : True := by
have h : x ≤ y := sorry
push_neg at h -- succeeds (no-op)
exact trivial
variable {R : Type*} [CommSemiring R]
example {x y : R} (h : x + y = 3) : True := by
ring_nf at h -- fail, "ring_nf made no progress"
example {x y : R} : True := by
have h : x + y = 3 := sorry
ring_nf at h -- succeeds (no-op)
exact trivialAdd 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
Activity
hrmacbeth commentedon Aug 13, 2025
Note: in leanprover-community/mathlib4#27562 I am making a change to the implementation of these two Mathlib tactics (
push_negandring_nf). After this change the two Mathlib tactics would no longer be affected by this issue, but I still think it would be nice to fix.kmill commentedon Aug 18, 2025
I independently noticed this issue yesterday as well, nice to see it already reported!
I see two options here:
A point in favor of the disappointing second option is that it lets users save an
instantiateMVars, which isn't necessarily free. However:Given these considerations, I think the first option makes the most sense.
That all said,
replaceLocalDeclDefEqis a scary function. It changes the type of a free variable while keeping the same fvarid. Yes, it might be defeq, but, for example, it might not be reducibly defeq, and so instance synthesis might come up with different results. That's one of many examples wherereplaceLocalDeclDefEqcan lead to stale caches, leading to who-knows-what strange behaviors. I noticed here that the local instance cache had never been updated for example. Another thing is thatreplaceLocalDeclDefEqmust not refer to any free variables with a higher index, otherwise the resulting local context is ill-typed (types of variables referring to future variables). That should be documented!The
Lean.MVarId.changeLocalDeclfunction is a safe alternative, though it has a few aspects that ought to be improved:withRevertedspecifically)Expr.equalslogic, which would be easy to addresswithReducible isDefEqlogic to be able to do fvarid reusefeat: `MVarId.assertAfter` fvar alias info, `MVarId.replace` mvar dep…