lean4-htt/tests/lean/run/6655.lean
Kyle Miller 84f15ac93a
fix: refine how simp tracks unfolded local definitions (#8753)
This PR fixes a bug in `simp` where it was not resetting the set of
zeta-delta reduced let definitions between `simp` calls. It also fixes a
bug where `simp` would report zeta-delta reduced let definitions that
weren't given as simp arguments (these extraneous let definitions appear
due to certain processes temporarily setting `zetaDelta := true`). This
PR also modifies the metaprogramming interface for the zeta-delta
tracking functions to be re-entrant and to prevent this kind of no-reset
bug from occurring again. Closes #6655.

Re-entrance of this metaprogramming interface is not needed to fix
#6655, but it is needed for some future PRs.

The `tests/lean/run/6655.lean` file has an example of a deficiency of
`simp?`, where `simp?` still over-reports unfolded let declarations.
This is likely due to `withInferTypeConfig` setting `zetaDelta := true`
from within `isDefEq`, but I did not verify this.

This PR supersedes #7539. The difference is that this PR has
`withResetZetaDeltaFVarIds` save and restore `zetaDeltaFVarIds`, but
that PR saves and then extends `zetaDeltaFVarIds` to persist unfolded
fvars. The behavior in this PR lets metaprograms control whether they
want to persist any of the unfolded fvars in this context themselves. In
practice, metaprograms that use `withResetZetaDeltaFVarIds` are creating
many temporary fvars and are doing dependence computations. These
temporary fvars shouldn't be persisted, and also dependence shouldn't be
inferred from the fact that a dependence calculation was done. (Concrete
example: the let-to-have transformation in an upcoming PR can be run
from within simp. Just because let-to-have unfolds an fvar while
calculating dependencies of lets doesn't mean that this fvar should be
included by `simp?`.)
2025-06-13 00:57:57 +00:00

109 lines
2.3 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-!
# Improve zeta-delta tracking for `simp?`
https://github.com/leanprover/lean4/issues/6655 reports issues with `simp?` where
it would over-report local variables. This comes down to two kinds of issues:
- zeta-delta tracking wasn't being reset, so previous `simp?`s would contribute variables
- `simp?` would report variables that weren't explicitly mentioned,
because `whnf` would be run with different configurations during the tracking.
(e.g. `withInferTypeConfig` enables `zetaDelta`.)
This file tests that it resets the tracking and filters the list.
-/
/-!
Example from #6655. This used to suggest `simp only [e, d]`.
-/
/--
info: Try this: simp only [e]
---
trace: α : Type
c : αα
x : α
d : αα := c
e : αα := d
⊢ d x = x
---
warning: declaration uses 'sorry'
-/
#guard_msgs in
example {α : Type} (c : αα) (x : α) : c x = x := by
let d := c
let e := d
change e x = x
simp? [e]
trace_state
sorry
/-!
Example from #6655. This used to suggest `simp only [d]`.
-/
/--
info: Try this: simp only
---
warning: declaration uses 'sorry'
-/
#guard_msgs in
example {α : Type} (c : αα) (x : α) : c x = x := by
let d := c
change d x = x
simp [d]
have : x = x := by
simp?
sorry
/-!
Example from comments of #6655. This used to suggest `simp only [Int.add_sub_cancel, p]`.
(N.B. the goal at that point does not have `p` in it!)
-/
/-- info: Try this: simp only [Int.add_sub_cancel] -/
#guard_msgs in
example (a b : Int) : a + b - b = a := by
let p := 1
have h : p = 1 := by
simp only [p]
simp?
/-!
Example from https://github.com/leanprover/lean4/pull/7539 by JovanGerb.
This used to suggest `simp only [a, b] ` and `simp only [a, b]`
-/
/--
info: Try this: simp only [a]
---
info: Try this: simp only
-/
#guard_msgs in
example : True := by
let a := 1
let b := 2
have : b = 2 := by simp [a,b]
have : a = 1 := by simp? [a]
have : 1 = 1 := by simp?
trivial
/-!
Test that there is still a deficiency. This should say `simp only [e]`.
-/
/--
info: Try this: simp only [e, c]
---
trace: α : Type
b : αα
x : α
c : αα := b
d : αα := c
e : αα := d
⊢ d x = x
---
warning: declaration uses 'sorry'
-/
#guard_msgs in
example {α : Type} (b : αα) (x : α) : b x = x := by
let c := b
let d := c
let e := d
change e x = x
simp? [e, c]
trace_state
sorry