Documentation

CslibFrontier.DataStructure.BTree.Lookup

Search in classic B-trees #

Search in the B-trees of Cslib.DataStructure.BTree, following [CLRS09], Chapter 18: scan a node's keys in order, stop at the query, or descend into the child before the first key above it.

Each operation is defined once, over an arbitrary monad m, taking a comparator le : α → α → m Bool and a node-visit action visit : m Unit, with no order instance in scope, so that every comparison goes through le. The pure operations are the Id instantiations with the decidable comparator Cslib.SortedAssoc.leI of a linear order and visit := pure (), in the pattern of CSLib's List.mergeSortM and List.idRun_mergeSortM. Costs are not stated here.

lookupM recurses on .node c rest, which is not a structural subterm, so it is defined by well-founded recursion on sizeOf; decide and rfl cannot evaluate lookup, and concrete lookups use the equation lemmas lookup_node_nil and lookup_node_cons through simp.

Main definitions #

Main results #

References #

Ported from cslib-community/EpsilonEridani at commit 1966ab476: EpsilonEridani/Algorithms/Lean/BTree/Lookup.lean.

def Cslib.DataStructure.BTree.slotM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (k : α) :
List (Triple α β) → m ℕ

Within-node linear search: the position of the first key not below k. At most one comparison per key.

@[irreducible]
def Cslib.DataStructure.BTree.lookupM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) :
BTree α β → m (Option β)

Lookup. Walking along a node's triples is free; visit fires when the search commits to a child or stops at a key, so each node is visited once. Equality is two le queries.

def Cslib.DataStructure.BTree.containsM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) (tr : BTree α β) :

Membership test.

theorem Cslib.IsMonadHom.map_slotM {α β : Type} {m : Type → Type u_1} {n : Type → Type u_2} [Monad m] [Monad n] {f : {γ : Type} → m γ → n γ} (hf : IsMonadHom m n fun {α : Type} => f) (le : α → α → m Bool) (k : α) (rest : List (DataStructure.BTree.Triple α β)) :
f (DataStructure.BTree.slotM le k rest) = DataStructure.BTree.slotM (fun (a b : α) => f (le a b)) k rest

A monad morphism commutes with the within-node search.

theorem Cslib.IsMonadHom.map_lookupM {α β : Type} {m : Type → Type u_1} {n : Type → Type u_2} [Monad m] [Monad n] {f : {γ : Type} → m γ → n γ} (hf : IsMonadHom m n fun {α : Type} => f) (le : α → α → m Bool) (visit : m Unit) (k : α) (tr : DataStructure.BTree α β) :
f (DataStructure.BTree.lookupM le visit k tr) = DataStructure.BTree.lookupM (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with lookup.

theorem Cslib.IsMonadHom.map_containsM {α β : Type} {m : Type → Type u_1} {n : Type → Type u_2} [Monad m] [Monad n] {f : {γ : Type} → m γ → n γ} (hf : IsMonadHom m n fun {α : Type} => f) (le : α → α → m Bool) (visit : m Unit) (k : α) (tr : DataStructure.BTree α β) :
f (DataStructure.BTree.containsM le visit k tr) = DataStructure.BTree.containsM (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with the membership test.

def Cslib.DataStructure.BTree.slot {α β : Type} [LinearOrder α] (k : α) (rest : List (Triple α β)) :

Pure within-node search: the Id instantiation of slotM.

def Cslib.DataStructure.BTree.lookup {α β : Type} [LinearOrder α] (k : α) (tr : BTree α β) :

Pure lookup: the Id instantiation of lookupM with visit := pure ().

def Cslib.DataStructure.BTree.contains {α β : Type} [LinearOrder α] (k : α) (tr : BTree α β) :

Pure membership test: the Id instantiation of containsM with visit := pure ().

theorem Cslib.DataStructure.BTree.slot_def {α β : Type} [LinearOrder α] (k : α) (rest : List (Triple α β)) :
slot k rest = (slotM SortedAssoc.leI k rest).run

slot is the Id instantiation of slotM.

theorem Cslib.DataStructure.BTree.lookup_def {α β : Type} [LinearOrder α] {tr : BTree α β} (k : α) :

lookup is the Id instantiation of lookupM.

theorem Cslib.DataStructure.BTree.contains_def {α β : Type} [LinearOrder α] {tr : BTree α β} (k : α) :

contains is the Id instantiation of containsM.

@[simp]
theorem Cslib.DataStructure.BTree.slot_nil {α β : Type} [LinearOrder α] (k : α) :
slot k [] = 0

The within-node search in a node without keys stops at position zero.

@[simp]
theorem Cslib.DataStructure.BTree.slot_cons {α β : Type} [LinearOrder α] (k k' : α) (v : β) (c : BTree α β) (rest : List (Triple α β)) :
slot k ((k', v, c) :: rest) = if k ≤ k' then 0 else slot k rest + 1

The within-node search stops at the first key not below k, and otherwise moves on.

@[simp]
theorem Cslib.DataStructure.BTree.lookup_node_nil {α β : Type} [LinearOrder α] (k : α) (first : BTree α β) :
lookup k (first.node []) = lookup k first

Lookup in a node without keys descends into its only child.

@[simp]
theorem Cslib.DataStructure.BTree.lookup_node_cons {α β : Type} [LinearOrder α] (k k' : α) (v : β) (first c : BTree α β) (rest : List (Triple α β)) :
lookup k (first.node ((k', v, c) :: rest)) = if k ≤ k' then if k' ≤ k then some v else lookup k first else lookup k (c.node rest)

Lookup at a node's first key: stop on equality, descend into the child before the key when the query is below it, and otherwise move on to the remaining keys.

@[simp]
theorem Cslib.DataStructure.BTree.contains_eq_isSome_lookup {α β : Type} [LinearOrder α] {tr : BTree α β} (k : α) :
contains k tr = (lookup k tr).isSome

Membership is success of lookup.

theorem Cslib.DataStructure.BTree.slot_eq_findIdx {α β : Type} [LinearOrder α] (k : α) (rest : List (Triple α β)) :
slot k rest = List.findIdx (fun (e : Triple α β) => decide (k ≤ e.1)) rest

The pure within-node search is the index of the first key not below k; this is the form that insertion and rank will use.

theorem Cslib.DataStructure.BTree.slot_eq_length_takeWhile {α β : Type} [LinearOrder α] (k : α) (rest : List (Triple α β)) :
slot k rest = (List.takeWhile (fun (e : Triple α β) => decide (e.1 < k)) rest).length

The pure within-node search counts the keys below k before the first key not below it.

theorem Cslib.DataStructure.BTree.slot_le_length {α β : Type} [LinearOrder α] (k : α) (rest : List (Triple α β)) :
slot k rest ≤ rest.length

The position found by the within-node search is at most the number of keys.

@[simp]

Lookup in the empty tree fails.

Model equation: on a tree with sorted entries, lookup is lookup in toList.

theorem Cslib.DataStructure.BTree.lookup_eq {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :

Model equation on well-formed trees.

On a tree with sorted entries, membership is success of lookup in toList.

On a tree with sorted entries, a key is in the tree exactly when it is among its keys.

theorem Cslib.DataStructure.BTree.contains_eq {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :

Membership on well-formed trees is success of lookup in toList.

theorem Cslib.DataStructure.BTree.contains_iff_mem_keys {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :

A key is in a well-formed tree exactly when it is among the tree's keys.