(Imperfect) discrimination trees. We use a hybrid representation.
- A
PersistentHashMap
for the root node which usually contains many children. - A sorted array of key/node pairs for inner nodes.
The edges are labeled by keys:
- Constant names (and arity). Universe levels are ignored.
- Free variables (and arity). Thus, an entry in the discrimination tree may reference hypotheses from the local context.
- Literals
- Star/Wildcard. We use them to represent metavariables and terms we want to ignore. We ignore implicit arguments and proofs.
- Other. We use to represent other kinds of terms (e.g., nested lambda, forall, sort, etc).
We reduce terms using TransparencyMode.reducible
. Thus, all reducible
definitions in an expression e
are unfolded before we insert it into the
discrimination tree.
Recall that projections from classes are NOT reducible.
For example, the expressions Add.add α (ringAdd ?α ?s) ?x ?x
and Add.add Nat Nat.hasAdd a b
generates paths with the following keys
respectively
⟨Add.add, 4⟩, α, *, *, *
⟨Add.add, 4⟩, Nat, *, ⟨a,0⟩, ⟨b,0⟩
That is, we don't reduce Add.add Nat inst a b
into Nat.add a b
.
We say the Add.add
applications are the de-facto canonical forms in
the metaprogramming framework.
Moreover, it is the metaprogrammer's responsibility to re-pack applications such as
Nat.add a b
into Add.add Nat inst a b
.
Remark: we store the arity in the keys
1- To be able to implement the "skip" operation when retrieving "candidate"
unifiers.
2- Distinguish partial applications f a
, f a b
, and f a b c
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Lean.Meta.DiscrTree.instLTKey = { lt := fun (a b : Lean.Meta.DiscrTree.Key) => Lean.Meta.DiscrTree.Key.lt a b = true }
Equations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Lean.Meta.DiscrTree.instInhabitedTrie = { default := Lean.Meta.DiscrTree.Trie.node #[] #[] }
Equations
- Lean.Meta.DiscrTree.empty = { root := { root := Lean.PersistentHashMap.Node.entries Lean.PersistentHashMap.mkEmptyEntriesArray, size := 0 } }
Instances For
Equations
- Lean.Meta.DiscrTree.instToFormatTrie = { format := Lean.Meta.DiscrTree.Trie.format }
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Lean.Meta.DiscrTree.instToFormatDiscrTree = { format := Lean.Meta.DiscrTree.format }
Equations
- One or more equations did not get rendered due to their size.
Equations
- Lean.Meta.DiscrTree.mkNoindexAnnotation e = Lean.mkAnnotation `noindex e
Instances For
Equations
- Lean.Meta.DiscrTree.hasNoindexAnnotation e = Option.isSome (Lean.annotation? `noindex e)
Instances For
Reduction procedure for the discrimination tree indexing.
The parameter config
controls how aggressively the term is reduced.
The parameter at type DiscrTree
controls this value.
See comment at DiscrTree
.
whnf for the discrimination tree module
Equations
- Lean.Meta.DiscrTree.reduceDT e root config = if root = true then Lean.Meta.DiscrTree.reduceUntilBadKey e config else Lean.Meta.DiscrTree.reduce e config
Instances For
When noIndexAtArgs := true
, pushArgs
assumes function application arguments have a no_index
annotation.
That is, f a b
is indexed as it was f (no_index a) (no_index b)
.
This feature is used when indexing local proofs in the simplifier. This is useful in examples like the one described on issue #2670.
In this issue, we have a local hypotheses (h : ∀ p : α × β, f p p.2 = p.2)
, and users expect it to be applicable to
f (a, b) b = b
. This worked in Lean 3 since no indexing was used. We can retrieve Lean 3 behavior by writing
(h : ∀ p : α × β, f p (no_index p.2) = p.2)
, but this is very inconvenient when the hypotheses was not written by the user in first place.
For example, it was introduced by another tactic. Thus, when populating the discrimination tree explicit arguments provided to simp
(e.g., simp [h]
),
we use noIndexAtArgs := true
. See comment: https://github.com/leanprover/lean4/issues/2670#issuecomment-1758889365
When noIndexAtArgs := true
, pushArgs
assumes function application arguments have a no_index
annotation.
That is, f a b
is indexed as it was f (no_index a) (no_index b)
.
This feature is used when indexing local proofs in the simplifier. This is useful in examples like the one described on issue #2670.
In this issue, we have a local hypotheses (h : ∀ p : α × β, f p p.2 = p.2)
, and users expect it to be applicable to
f (a, b) b = b
. This worked in Lean 3 since no indexing was used. We can retrieve Lean 3 behavior by writing
(h : ∀ p : α × β, f p (no_index p.2) = p.2)
, but this is very inconvenient when the hypotheses was not written by the user in first place.
For example, it was introduced by another tactic. Thus, when populating the discrimination tree explicit arguments provided to simp
(e.g., simp [h]
),
we use noIndexAtArgs := true
. See comment: https://github.com/leanprover/lean4/issues/2670#issuecomment-1758889365
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Lean.Meta.DiscrTree.insert d e v config noIndexAtArgs = do let keys ← Lean.Meta.DiscrTree.mkPath e config noIndexAtArgs pure (Lean.Meta.DiscrTree.insertCore d keys v)
Instances For
Inserts a value into a discrimination tree,
but only if its key is not of the form #[*]
or #[=, *, *, *]
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Find values that match e
in d
.
Equations
- Lean.Meta.DiscrTree.getMatch d e config = do let __do_lift ← Lean.Meta.DiscrTree.getMatchCore d e config pure __do_lift.snd
Instances For
Similar to getMatch
, but returns solutions that are prefixes of e
.
We store the number of ignored arguments in the result.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Monadically fold the keys and values stored in a Trie
.
Fold the keys and values stored in a Trie
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Monadically fold the values stored in a Trie
.
Fold the values stored in a Trie
.
Equations
- Lean.Meta.DiscrTree.Trie.foldValues f init t = Id.run (Lean.Meta.DiscrTree.Trie.foldValuesM f init t)
Instances For
The number of values stored in a Trie
.
Monadically fold over the keys and values stored in a DiscrTree
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Fold over the keys and values stored in a DiscrTree
Equations
- Lean.Meta.DiscrTree.fold f init t = Id.run (Lean.Meta.DiscrTree.foldM (fun (s : σ) (keys : Array Lean.Meta.DiscrTree.Key) (a : α) => pure (f s keys a)) init t)
Instances For
Monadically fold over the values stored in a DiscrTree
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Fold over the values stored in a DiscrTree
.
Equations
- Lean.Meta.DiscrTree.foldValues f init t = Id.run (Lean.Meta.DiscrTree.foldValuesM f init t)
Instances For
Check for the presence of a value satisfying a predicate.
Equations
- Lean.Meta.DiscrTree.containsValueP t f = Lean.Meta.DiscrTree.foldValues (fun (r : Bool) (a : α) => r || f a) false t
Instances For
Extract the values stored in a DiscrTree
.
Equations
- Lean.Meta.DiscrTree.values t = Lean.Meta.DiscrTree.foldValues (fun (as : Array α) (a : α) => Array.push as a) #[] t
Instances For
Extract the keys and values stored in a DiscrTree
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Get the number of values stored in a DiscrTree
. O(n) in the size of the tree.
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 DiscrTree
.
Apply a monadic function to the array of values at each node in a DiscrTree
.
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 DiscrTree
.
Equations
- Lean.Meta.DiscrTree.mapArrays d f = Id.run (Lean.Meta.DiscrTree.mapArraysM d fun (A : Array α) => pure (f A))