RefinedDiscrTree discrimination tree with refined indexing #
We define discrimination trees for the purpose of unifying local expressions with library results.
This implementation is based on the DiscrTree in Lean.
I document here what is not in the original.
-
The constructor
Key.starnow takes aNatidentifier as an argument. For example, the library patterna+ais encoded as[⟨Hadd.hadd, 6⟩, *0, *0, *0, *1, *2, *2].*0corresponds to the type ofa,*1to theHaddinstance, and*2toa. This means that it will only match an expressionx+yifxis definitionally equal toy. -
The constructor
Key.opaquehas been introduced in order to index existential variables in lemmas likeNat.exists_prime_and_dvd {n : ℕ} (hn : n ≠ 1) : ∃ p, Prime p ∧ p ∣ n, where the partPrime pgets the pattern[⟨Nat.Prime, 1⟩, ◾]. (◾ representsKey.opaque) -
The constructors
Key.lam,Key.forallandKey.bvarhave been introduced in order to allow for patterns under binders. For example, this allows for more specific matching with the left hand side ofFinset.sum_range_id (n : ℕ) : ∑ i in range n, i = n * (n - 1) / 2, which is indexed by[⟨Finset.sum, 5⟩, ⟨Nat, 0⟩, ⟨Nat, 0⟩, *0, ⟨Finset.Range, 1⟩, *1, λ, ⟨#0, 0⟩] -
We keep track of the matching score of a unification. This score represent the number of keys that had to be the same for the unification to succeed. For example, matching
(1 + 2) + 3withadd_commgives a score of 3, since the pattern of commutativity is [⟨Hadd.hadd, 6⟩, *0, *0, *0, *1, *2, *3], so matching⟨Hadd.hadd, 6⟩gives 1 point, and matching*0two times after its first appearance gives another 2 points. Similarly, matching it withadd_assocgives a score of 7.TODO?: the third type parameter of
Hadd.haddis an outparam, so its matching should not be counted in the score. -
Patterns that have the potential to be η-reduced are put into the
RefinedDiscrTreeunder all possible reduced key sequences. This is for terms of the formfun x => f (?m x₁ .. xₙ), where?mis a metavariable, and for somei,xᵢ = x. For example, the patternContinuous fun y => Real.exp (f y)])is indexed by[⟨Continuous, 5⟩, *0, ⟨Real, 0⟩, *1, *2, λ, ⟨Real.exp⟩, *3]and by[⟨Continuous, 5⟩, *0, ⟨Real, 0⟩, *1, *2, ⟨Real.exp⟩]so that it also comes up if you search withContinuous Real.exp. Similarly,Continuous fun x => f x + g xis indexed by[⟨Continuous, 1⟩, λ, ⟨Hadd.hadd, 6⟩, *0, *0, *0, *1, *2, *3]and by[⟨Continuous, 1⟩, ⟨Hadd.hadd, 5⟩, *0, *0, *0, *1, *2].
Discrimination tree key.
- star: Nat → Mathlib.Meta.FunProp.RefinedDiscrTree.Key
A metavariable. This key matches with anything. It stores an index.
- opaque: Mathlib.Meta.FunProp.RefinedDiscrTree.Key
An opaque variable. This key only matches with itself or
Key.star. - const: Lean.Name → Nat → Mathlib.Meta.FunProp.RefinedDiscrTree.Key
A constant. It stores a
Nameand an arity. - fvar: Lean.FVarId → Nat → Mathlib.Meta.FunProp.RefinedDiscrTree.Key
A free variable. It stores a
FVarIdand an arity. - bvar: Nat → Nat → Mathlib.Meta.FunProp.RefinedDiscrTree.Key
- lit: Lean.Literal → Mathlib.Meta.FunProp.RefinedDiscrTree.Key
A literal.
- sort: Mathlib.Meta.FunProp.RefinedDiscrTree.Key
A sort. Universe levels are ignored.
- lam: Mathlib.Meta.FunProp.RefinedDiscrTree.Key
A lambda function.
- forall: Mathlib.Meta.FunProp.RefinedDiscrTree.Key
A dependent arrow.
- proj: Lean.Name → Nat → Nat → Mathlib.Meta.FunProp.RefinedDiscrTree.Key
A projection. It takes the constructor name, the projection index and the arity.
Instances For
Constructor index as a help for ordering Key.
Note that the index of the star pattern is 0, so that when looking up in a Trie,
we can look at the start of the sorted array for all .star patterns.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Return the number of arguments that the Key takes.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Discrimination tree trie. See RefinedDiscrTree.
- node: {α : Type} → Array (Mathlib.Meta.FunProp.RefinedDiscrTree.Key × Mathlib.Meta.FunProp.RefinedDiscrTree.Trie α) → Mathlib.Meta.FunProp.RefinedDiscrTree.Trie α
- path: {α : Type} →
Array Mathlib.Meta.FunProp.RefinedDiscrTree.Key →
Mathlib.Meta.FunProp.RefinedDiscrTree.Trie α → Mathlib.Meta.FunProp.RefinedDiscrTree.Trie α
Sequence of nodes with only one child.
keysis anArrayof size at least 1. - values: {α : Type} → Array α → Mathlib.Meta.FunProp.RefinedDiscrTree.Trie α
Instances For
Equations
- Mathlib.Meta.FunProp.RefinedDiscrTree.instInhabitedTrie = { default := Mathlib.Meta.FunProp.RefinedDiscrTree.Trie.node #[] }
Trie.path constructor that only inserts the path if it is non-empty.
Equations
- Mathlib.Meta.FunProp.RefinedDiscrTree.Trie.mkPath keys child = if Array.isEmpty keys = true then child else Mathlib.Meta.FunProp.RefinedDiscrTree.Trie.path keys child
Instances For
Trie constructor for a single value.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Trie.node constructor for combining two Key, Trie α pairs.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Return the values from a Trie α, assuming that it is a leaf
Equations
- One or more equations did not get rendered due to their size.
Instances For
Return the children of a Trie α, assuming that it is not a leaf.
The result is sorted by the Key's
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Mathlib.Meta.FunProp.RefinedDiscrTree.instToFormatTrie = { format := Mathlib.Meta.FunProp.RefinedDiscrTree.Trie.format }
Discrimination tree. It is an index from expressions to values of type α.
- root : Lean.PersistentHashMap Mathlib.Meta.FunProp.RefinedDiscrTree.Key (Mathlib.Meta.FunProp.RefinedDiscrTree.Trie α)
The underlying
PersistentHashMapof aRefinedDiscrTree.
Instances For
Equations
- Mathlib.Meta.FunProp.RefinedDiscrTree.instInhabitedRefinedDiscrTree = { default := { root := default } }
Equations
- Mathlib.Meta.FunProp.RefinedDiscrTree.instToFormatRefinedDiscrTree = { format := Mathlib.Meta.FunProp.RefinedDiscrTree.RefinedDiscrTree.format }
DTExpr is a simplified form of Expr.
It is intermediate step in converting from Expr to Array Key.
- star: Option Lean.MVarId → Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr
A metavariable.
- opaque: Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr
An opaque variable.
- const: Lean.Name → Array Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr → Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr
A constant.
- fvar: Lean.FVarId → Array Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr → Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr
A free variable.
- bvar: Nat → Array Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr → Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr
A bound variable.
- lit: Lean.Literal → Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr
A literal.
- sort: Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr
A sort.
- lam: Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr → Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr
A lambda function.
- forall: Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr →
Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr → Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr
A dependent arrow.
- proj: Lean.Name →
Nat →
Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr →
Array Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr → Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr
A projection.
Instances For
Return the size of the DTExpr. This is used for calculating the matching score when two
expressions are equal.
The score is not incremented at a lambda, which is so that the expressions
∀ x, p[x] and ∃ x, p[x] get the same size.
Given a DTExpr, return the linearized encoding in terms of Key,
which is used for RefinedDiscrTree indexing.
Equations
- Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr.flatten e initCapacity = StateT.run' (Mathlib.Meta.FunProp.RefinedDiscrTree.DTExpr.flattenAux (Array.mkEmpty initCapacity) e) { stars := #[] }
Instances For
Reduction procedure for the RefinedDiscrTree indexing.
Check whether the expression is represented by Key.star and has arg as an argument.
Equations
- One or more equations did not get rendered due to their size.
- Mathlib.Meta.FunProp.RefinedDiscrTree.isStarWithArg arg x = false
Instances For
If e is of the form (fun x₀ ... xₙ => b y₀ ... yₙ),
where each yᵢ has a metavariable head with xᵢ as an argument,
then return some b. Otherwise, return none.
Equations
- Mathlib.Meta.FunProp.RefinedDiscrTree.starEtaExpanded (Lean.Expr.lam binderName binderType b binderInfo) x = Mathlib.Meta.FunProp.RefinedDiscrTree.starEtaExpanded b (x + 1)
- Mathlib.Meta.FunProp.RefinedDiscrTree.starEtaExpanded x✝ x = Mathlib.Meta.FunProp.RefinedDiscrTree.starEtaExpandedBody x✝ x 0
Instances For
Return true if e contains a loose bound variable.
Equations
Instances For
Equations
- One or more equations did not get rendered due to their size.
If e is of the form (fun x₁ ... xₙ => b y₁ ... yₙ),
then introduce variables for x₁, ..., xₙ, instantiate these in b, and run k on b.
Return the encoding of e as a DTExpr.
If root = false, then e is a strict sub expression of the original expression.
Introduce a bound variable of type domain to the context, instantiate it in e,
and then return all encodings of e as a DTExpr
Return all encodings of e as a DTExpr, taking possible η-reductions into account.
If root = false, then e is a strict sub expression of the original expression.
Introduce a bound variable of type domain to the context, instantiate it in e,
and then return all encodings of e as a DTExpr
Return the encoding of e as a DTExpr.
The argument fvarInContext allows you to specify which free variables in e will still be
in the context when the RefinedDiscrTree is being used for lookup.
It should return true only if the RefinedDiscrTree is build and used locally.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Similar to mkDTExpr.
Return all encodings of e as a DTExpr, taking possible η-reductions into account.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Make discr tree path for expression e.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Insert the value v at index keys : Array Key in a RefinedDiscrTree.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Insert the value v at index e : DTExpr in a RefinedDiscrTree.
Equations
Instances For
Insert the value v at index e : Expr in a RefinedDiscrTree.
The argument fvarInContext allows you to specify which free variables in e will still be
in the context when the RefinedDiscrTree is being used for lookup.
It should return true only if the RefinedDiscrTree is build and used locally.
Equations
- One or more equations did not get rendered due to their size.
Instances For
If k is a key in children, return the corresponding Trie α. Otherwise return none.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Return the possible Trie α that match with n metavariable.
Return the possible Trie α that match with anything.
We add 1 to the matching score when the key equals .opaque,
since this pattern is "harder" to match with.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Return the possible Trie α that come from a Key.star,
while keeping track of the Key.star assignments.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Return the possible Trie α that match with e.
If the head of e is not a metavariable,
return the possible Trie α that exactly match with e.
Return the results from the RefinedDiscrTree that match the given expression,
together with their matching scores, in decreasing order of score.
Each entry of type Array α × Nat corresponds to one pattern.
If unify := false, then metavariables in e are treated as opaque variables.
This is for when you don't want the matched keys to instantiate metavariables in e.
If allowRootStar := false, then we don't allow e or the matched key in d
to be a star pattern.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Apply a monadic function to the array of values at each node in a RefinedDiscrTree.
Apply a monadic function to the array of values at each node in a RefinedDiscrTree.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Apply a function to the array of values at each node in a RefinedDiscrTree.