Skip to content

MVarId.replaceLocalDeclDefEq doesn't instantiate metavariables #9893

Closed

Description

@hrmacbeth

Prerequisites

Please put an X between the brackets as you perform the following steps:

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 typeNew is equal to current type of fvarId, then returns mvarId unchanged.

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 trivial

Expected 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 trivial

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Activity

  1. added
    bug
    on Aug 13, 2025
  2. hrmacbeth commented on Aug 13, 2025

    @hrmacbeth
    ContributorAuthor

    Note: in leanprover-community/mathlib4#27562 I am making a change to the implementation of these two Mathlib tactics (push_neg and ring_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.

  3. kmill commented on Aug 18, 2025

    @kmill
    Collaborator

    I independently noticed this issue yesterday as well, nice to see it already reported!

    I see two options here:

    1. Do the obvious fix, have it instantiate metavariables for the new type and the old type when doing the equality check.
    2. Document that it's up to you to instantiate metavariables.

    A point in favor of the disappointing second option is that it lets users save an instantiateMVars, which isn't necessarily free. However:

    • That's rather subtle and easy to get wrong.
    • The old type might have metavariables that have since been solved for, so there probably ought to be some instantiation anyway, at least for the old type!
    • The common case is no unsolved-for metavariables, so instantiating metavariables is basically free.

    Given these considerations, I think the first option makes the most sense.

    That all said, replaceLocalDeclDefEq is 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 where replaceLocalDeclDefEq can 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 that replaceLocalDeclDefEq must 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.changeLocalDecl function is a safe alternative, though it has a few aspects that ought to be improved:

    • it should try to put the local context back into order as well as it can (that's a problem with withReverted specifically)
    • it doesn't have any Expr.equals logic, which would be easy to address
    • it might consider having withReducible isDefEq logic to be able to do fvarid reuse
    • it should return an FVarSubst to let metaprograms know what the new fvarids are
  4. added
    P-medium
    on Aug 22, 2025
  5. added a commit that references this issue on Apr 30, 2026
    19baa47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-mediumbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions