@@ -58,29 +58,39 @@ set_option linter.missingDocs false in
58
58
/-- A rule pattern index. Maps expressions `e` to rule patterns that likely
59
59
unify with `e`. -/
60
60
structure RulePatternIndex where
61
+ /-- The index. -/
61
62
tree : DiscrTree RulePatternIndex.Entry
63
+ /-- `true` iff the index contains no patterns. -/
64
+ isEmpty : Bool
62
65
deriving Inhabited
63
66
64
67
namespace RulePatternIndex
65
68
66
69
instance : EmptyCollection RulePatternIndex :=
67
- ⟨⟨{}⟩⟩
70
+ ⟨⟨{}, true ⟩⟩
68
71
69
72
/-- Add a rule pattern to the index. -/
70
73
def add (name : RuleName) (pattern : RulePattern) (idx : RulePatternIndex) :
71
74
RulePatternIndex :=
72
- ⟨idx.tree.insertCore pattern.discrTreeKeys { name, pattern }⟩
75
+ ⟨idx.tree.insertCore pattern.discrTreeKeys { name, pattern }, false ⟩
73
76
74
77
/-- Merge two rule pattern indices. Patterns that appear in both indices appear
75
78
twice in the result. -/
76
79
def merge (idx₁ idx₂ : RulePatternIndex) : RulePatternIndex :=
77
- ⟨idx₁.tree.mergePreservingDuplicates idx₂.tree⟩
80
+ if idx₁.isEmpty then
81
+ idx₂
82
+ else if idx₂.isEmpty then
83
+ idx₁
84
+ else
85
+ ⟨idx₁.tree.mergePreservingDuplicates idx₂.tree, false ⟩
78
86
79
87
section Get
80
88
81
89
/-- Get rule pattern substitutions for the patterns that match `e`. -/
82
90
def getSingle (e : Expr) (idx : RulePatternIndex) :
83
91
BaseM (Array (RuleName × Substitution)) := do
92
+ if idx.isEmpty then
93
+ return #[]
84
94
let ms ← getUnify idx.tree e
85
95
ms.filterMapM λ { name := r, pattern } => do
86
96
let some subst ← pattern.match e
@@ -92,6 +102,8 @@ def getSingle (e : Expr) (idx : RulePatternIndex) :
92
102
array may contain duplicates. -/
93
103
def getCore (e : Expr) (idx : RulePatternIndex) :
94
104
BaseM (Array (RuleName × Substitution)) := do
105
+ if idx.isEmpty then
106
+ return #[]
95
107
let e ← instantiateMVars e
96
108
checkCache (β := RulePatternCache.Entry) e λ _ => do
97
109
let (_, ms) ← e.forEach getSubexpr |>.run #[]
@@ -118,6 +130,8 @@ the given local declaration. Subexpressions containing bound variables are not
118
130
considered. -/
119
131
def getInLocalDeclCore (acc : RulePatternSubstMap) (ldecl : LocalDecl)
120
132
(idx : RulePatternIndex) : BaseM RulePatternSubstMap := do
133
+ if idx.isEmpty then
134
+ return acc
121
135
let mut result := acc
122
136
result := result.insertArray $ ← idx.getCore ldecl.toExpr
123
137
result := result.insertArray $ ← idx.getCore ldecl.type
@@ -136,6 +150,8 @@ considered. -/
136
150
def getInGoal (goal : MVarId) (idx : RulePatternIndex) :
137
151
BaseM RulePatternSubstMap :=
138
152
goal.withContext do
153
+ if idx.isEmpty then
154
+ return ∅
139
155
let mut result := ∅
140
156
for ldecl in (← goal.getDecl).lctx do
141
157
unless ldecl.isImplementationDetail do
0 commit comments