Skip to content

Commit

Permalink
refactor: rename TheoremPattern to EMatchTheorem
Browse files Browse the repository at this point in the history
  • Loading branch information
leodemoura committed Dec 30, 2024
1 parent c6c4938 commit d2df7e1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Lean/Elab/Tactic/Grind.lean
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def elabGrindPattern : CommandElab := fun stx => do
let pattern ← instantiateMVars (← elabTerm term none)
let pattern ← Grind.unfoldReducible pattern
return pattern.abstract xs
Grind.addTheoremPattern declName xs.size patterns.toList
Grind.addEMatchTheorem declName xs.size patterns.toList
| _ => throwUnsupportedSyntax

def grind (mvarId : MVarId) (mainDeclName : Name) : MetaM Unit := do
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Meta/Tactic/Grind.lean
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Lean.Meta.Tactic.Grind.PP
import Lean.Meta.Tactic.Grind.Simp
import Lean.Meta.Tactic.Grind.Ctor
import Lean.Meta.Tactic.Grind.Parser
import Lean.Meta.Tactic.Grind.TheoremPatterns
import Lean.Meta.Tactic.Grind.EMatchTheorem

namespace Lean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def Origin.key : Origin → Name
| .stx id _ => id
| .other => `other

-- TODO: find better name
structure TheoremPattern where
/-- A theorem for heuristic instantiation based on E-matching. -/
structure EMatchTheorem where
proof : Expr
numParams : Nat
patterns : List Expr
Expand All @@ -42,21 +42,21 @@ structure TheoremPattern where
origin : Origin
deriving Inhabited

-- TODO: find better name
abbrev TheoremPatterns := PHashMap Name (List TheoremPattern)
/-- The key is a symbol from `EMatchTheorem.symbols`. -/
abbrev EMatchTheorems := PHashMap Name (List EMatchTheorem)

def TheoremPatterns.insertTheorem (s : TheoremPatterns) (thm : TheoremPattern) : TheoremPatterns := Id.run do
def EMatchTheorems.insert (s : EMatchTheorems) (thm : EMatchTheorem) : EMatchTheorems := Id.run do
let .const declName :: syms := thm.symbols
| unreachable!
let thm := { thm with symbols := syms }
if let some thms := s.find? declName then
return s.insert declName (thm::thms)
return PersistentHashMap.insert s declName (thm::thms)
else
return s.insert declName [thm]
return PersistentHashMap.insert s declName [thm]

builtin_initialize theoremPatternsExt : SimpleScopedEnvExtension TheoremPattern TheoremPatterns
private builtin_initialize ematchTheoremsExt : SimpleScopedEnvExtension EMatchTheorem EMatchTheorems
registerSimpleScopedEnvExtension {
addEntry := TheoremPatterns.insertTheorem
addEntry := EMatchTheorems.insert
initial := .empty
}

Expand Down Expand Up @@ -295,7 +295,7 @@ private def ppParamsAt (proof : Expr) (numParms : Nat) (paramPos : List Nat) : M
msg := msg ++ m!"{x} : {← inferType x}"
addMessageContextFull msg

def addTheoremPattern (declName : Name) (numParams : Nat) (patterns : List Expr) : MetaM Unit := do
def addEMatchTheorem (declName : Name) (numParams : Nat) (patterns : List Expr) : MetaM Unit := do
let .thmInfo info ← getConstInfo declName
| throwError "`{declName}` is not a theorem, you cannot assign patterns to non-theorems for the `grind` tactic"
let us := info.levelParams.map mkLevelParam
Expand All @@ -306,12 +306,12 @@ def addTheoremPattern (declName : Name) (numParams : Nat) (patterns : List Expr)
if let .missing pos ← checkCoverage proof numParams bvarFound then
let pats : MessageData := m!"{patterns.map ppPattern}"
throwError "invalid pattern(s) for `{declName}`{indentD pats}\nthe following theorem parameters cannot be instantiated:{indentD (← ppParamsAt proof numParams pos)}"
theoremPatternsExt.add {
ematchTheoremsExt.add {
proof, patterns, numParams, symbols
origin := .decl declName
}

def getTheoremPatterns : CoreM TheoremPatterns :=
return theoremPatternsExt.getState (← getEnv)
def getEMatchTheorems : CoreM EMatchTheorems :=
return ematchTheoremsExt.getState (← getEnv)

end Lean.Meta.Grind
2 changes: 1 addition & 1 deletion src/Lean/Meta/Tactic/Grind/Internalize.lean
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private def activateTheoremPatterns (fName : Name) : GoalM Unit := do
modify fun s => { s with newThms := s.newThms.push thm }
| _ =>
trace[grind.pattern] "reinsert `{thm.origin.key}`"
modify fun s => { s with thmMap := s.thmMap.insertTheorem thm }
modify fun s => { s with thmMap := s.thmMap.insert thm }

partial def internalize (e : Expr) (generation : Nat) : GoalM Unit := do
if (← alreadyInternalized e) then return ()
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Meta/Tactic/Grind/Run.lean
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def GrindM.run (x : GrindM α) (mainDeclName : Name) : MetaM α := do
def mkGoal (mvarId : MVarId) : GrindM Goal := do
let trueExpr ← getTrueExpr
let falseExpr ← getFalseExpr
let thmMap ← getTheoremPatterns
let thmMap ← getEMatchTheorems
GoalM.run' { mvarId, thmMap } do
mkENodeCore falseExpr (interpreted := true) (ctor := false) (generation := 0)
mkENodeCore trueExpr (interpreted := true) (ctor := false) (generation := 0)
Expand Down
8 changes: 4 additions & 4 deletions src/Lean/Meta/Tactic/Grind/Types.lean
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Lean.Meta.Tactic.Simp.Types
import Lean.Meta.Tactic.Util
import Lean.Meta.Tactic.Grind.Canon
import Lean.Meta.Tactic.Grind.Attr
import Lean.Meta.Tactic.Grind.TheoremPatterns
import Lean.Meta.Tactic.Grind.EMatchTheorem

namespace Lean.Meta.Grind

Expand Down Expand Up @@ -275,14 +275,14 @@ structure Goal where
/-- Next unique index for creating ENodes -/
nextIdx : Nat := 0
/-- Active theorems that we have performed ematching at least once. -/
thms : PArray TheoremPattern := {}
thms : PArray EMatchTheorem := {}
/-- Active theorems that we have not performed any round of ematching yet. -/
newThms : PArray TheoremPattern := {}
newThms : PArray EMatchTheorem := {}
/--
Inactive global theorems. As we internalize terms, we activate theorems as we find their symbols.
Local theorem provided by users are added directly into `newThms`.
-/
thmMap : TheoremPatterns
thmMap : EMatchTheorems
deriving Inhabited

def Goal.admit (goal : Goal) : MetaM Unit :=
Expand Down

0 comments on commit d2df7e1

Please sign in to comment.