The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V01. The language of Mathlib

Mathlib.Lean.Meta.RefinedDiscrTree.Lookup

12 declarations. Mathlib docs · Source

Matching with a RefinedDiscrTree

This file defines the matching procedure for the RefinedDiscrTree.

The main definitions are

  • The structure MatchResult, which contains the match results, ordered by matching score.
  • The (private) function evalNode which evaluates a node of the RefinedDiscrTree
  • The (private) function getMatchLoop, which is the main function that computes the matches. It implements the non-deterministic computation by keeping a stack of PartialMatches, and repeatedly processing the most recent one.
  • The matching function getMatch that also returns an updated RefinedDiscrTree

To find the matches, we first encode the expression as a List Key. Then using this, we find all matches with the tree. When unify == true, we also allow metavariables in the target expression to be assigned.

We use a simple unification algorithm. For all star/metavariable patterns in the RefinedDiscrTree (and in the target if unify == true), we store the assignment, and when it is attempted to be assigned again, we check that it is the same assignment.

theorem Lean.Meta.RefinedDiscrTree.MatchResult.mk.inj E1 #

∀ {α : Type} {elts elts_1 : Std.TreeMap ℕ (Array (Array α)) compare},
  { elts := elts } = { elts := elts_1 } → elts = elts_1

def Lean.Meta.RefinedDiscrTree.instInhabitedPartialMatch.default E1 #

Lean.Meta.RefinedDiscrTree.PartialMatch✝

theorem Lean.Meta.RefinedDiscrTree.MatchResult.mk.injEq E0 #

∀ {α : Type} (elts elts_1 : Std.TreeMap ℕ (Array (Array α)) compare),
  ({ elts := elts } = { elts := elts_1 }) = (elts = elts_1)

def Lean.Meta.RefinedDiscrTree.instInhabitedMatchResult.default E1 #

{α : Type} → Lean.Meta.RefinedDiscrTree.MatchResult α

def Lean.Meta.RefinedDiscrTree.MatchResult.ctorIdx E1 #

{α : Type} → Lean.Meta.RefinedDiscrTree.MatchResult α → ℕ

structure Lean.Meta.RefinedDiscrTree.MatchResult E1 #

Type → Type

A match result contains the results from matching a term against patterns in the discrimination tree.

ctor Lean.Meta.RefinedDiscrTree.MatchResult.mk E0 #

{α : Type} → Std.TreeMap ℕ (Array (Array α)) compare → Lean.Meta.RefinedDiscrTree.MatchResult α

abbrev Lean.Meta.RefinedDiscrTree.MatchResult.elts E1 #

{α : Type} → Lean.Meta.RefinedDiscrTree.MatchResult α → Std.TreeMap ℕ (Array (Array α)) compare

The elements in the match result.

The Nat in the tree map represents the score of the results. The elements are arrays of arrays, where each sub-array corresponds to one discr tree pattern.

instance Lean.Meta.RefinedDiscrTree.instInhabitedMatchResult E0 #

{a : Type} → Inhabited (Lean.Meta.RefinedDiscrTree.MatchResult a)

def Lean.Meta.RefinedDiscrTree.MatchResult.toArray E1 #

{α : Type} → Lean.Meta.RefinedDiscrTree.MatchResult α → Array α

Convert a MatchResult into a Array, with better matches at the start of the array.

def Lean.Meta.RefinedDiscrTree.MatchResult.flatten E1 #

{α : Type} → Lean.Meta.RefinedDiscrTree.MatchResult α → Array (Array α)

Convert a MatchResult into an Array of Arrays. Each Array corresponds to one pattern. The better matching patterns are at the start of the outer array. For each inner array, the entries are ordered in the order they were inserted.

def Lean.Meta.RefinedDiscrTree.getMatch E1 #

{α : Type} →
  Lean.Meta.RefinedDiscrTree α →
    Lean.Expr → Bool → Bool → Lean.MetaM (Lean.Meta.RefinedDiscrTree.MatchResult α × Lean.Meta.RefinedDiscrTree α)

Find values that match e in d.

  • If unify == true then metavariables in e can be assigned.
  • If matchRootStar == true then we allow metavariables at the root to unify. Set this to false to avoid getting excessively many results.