Skip to content

Commit

Permalink
fix: do not include internal match equational theorems at simp trace (
Browse files Browse the repository at this point in the history
#4274)

closes #4251
  • Loading branch information
leodemoura authored May 25, 2024
1 parent fe17b82 commit 8eee5ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Lean/Elab/Tactic/Simp.lean
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ def mkSimpOnly (stx : Syntax) (usedSimps : Simp.UsedSimps) : MetaM Syntax := do
for (thm, _) in usedSimps.toArray.qsort (·.2 < ·.2) do
match thm with
| .decl declName post inv => -- global definitions in the environment
if env.contains declName && (inv || !simpOnlyBuiltins.contains declName) then
if env.contains declName
&& (inv || !simpOnlyBuiltins.contains declName)
&& !Match.isMatchEqnTheorem env declName then
let decl : Term ← `($(mkIdent (← unresolveNameGlobal declName)):ident)
let arg ← match post, inv with
| true, true => `(Parser.Tactic.simpLemma| ← $decl:term)
Expand Down
14 changes: 12 additions & 2 deletions src/Lean/Meta/Match/MatchEqsExt.lean
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ def MatchEqns.size (e : MatchEqns) : Nat :=

structure MatchEqnsExtState where
map : PHashMap Name MatchEqns := {}
eqns : PHashSet Name := {}
deriving Inhabited

/- We generate the equations and splitter on demand, and do not save them on .olean files. -/
builtin_initialize matchEqnsExt : EnvExtension MatchEqnsExtState ←
registerEnvExtension (pure {})

def registerMatchEqns (matchDeclName : Name) (matchEqns : MatchEqns) : CoreM Unit :=
modifyEnv fun env => matchEqnsExt.modifyState env fun s => { s with map := s.map.insert matchDeclName matchEqns }
def registerMatchEqns (matchDeclName : Name) (matchEqns : MatchEqns) : CoreM Unit := do
modifyEnv fun env => matchEqnsExt.modifyState env fun { map, eqns } => {
eqns := matchEqns.eqnNames.foldl (init := eqns) fun eqns eqn => eqns.insert eqn
map := map.insert matchDeclName matchEqns
}

/-
Forward definition. We want to use `getEquationsFor` in the simplifier,
`getEquationsFor` depends on `mkEquationsfor` which uses the simplifier. -/
@[extern "lean_get_match_equations_for"]
opaque getEquationsFor (matchDeclName : Name) : MetaM MatchEqns

/--
Returns `true` if `declName` is the name of a `match` equational theorem.
-/
def isMatchEqnTheorem (env : Environment) (declName : Name) : Bool :=
matchEqnsExt.getState env |>.eqns.contains declName

end Lean.Meta.Match
13 changes: 13 additions & 0 deletions tests/lean/run/4251.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/--
info: Try this: simp only [ha, Nat.reduceEqDiff, imp_self]
-/
#guard_msgs in
theorem foo₁ (a : Nat) (ha : a = 37) :
(match h : a with | 42 => by simp_all | n => n) = 37 := by
simp? [ha]

theorem foo₂ (a : Nat) (ha : a = 37) (hb : b = 37) :
(match h : a with | 42 => by simp_all | n => n) = b := by
simp only [ha, Nat.reduceEqDiff, imp_self]
guard_target =ₛ 37 = b
rw [hb]

0 comments on commit 8eee5ff

Please sign in to comment.