Documentation

CslibFrontier.DataStructure.BPlusTree.Lookup

Search in B+-trees #

Lookup in the B+-trees of Cslib.DataStructure.BPlusTree. Each operation is defined once over an arbitrary monad m, with a comparator le : α → α → m Bool and a node-visit action visit : m Unit, and no order instance in scope, so that every comparison of keys goes through le. The pure operations are the Id instantiations with the comparator Cslib.SortedAssoc.leI of a linear order and visit := pure (). Costs are not stated here.

A search descends through the interior nodes by the separators and ends in a leaf. At an interior node it passes the separators that are at most the query and enters the child introduced by the last of them, or the first child when there is none; one comparison per separator passed, and one more where it stops. In the leaf it runs the scan Cslib.SortedAssoc.lookupM of the entry list. visit fires once at each node on the path, the leaf included. A separator equal to the query does not end the search: separators are not entries.

slotM is the within-node separator search on its own: the number of separators that the search passes, which is the position of the child it enters.

The model equation lookup_eq holds on well-formed trees. Sorted entries and the separator convention are not enough for it: the search trusts a separator to bound every later child from below, and that follows from the convention only through a key of the child the separator introduces, so the children must not be empty. Occupancy at 2 ≤ t provides that.

Main definitions #

Main results #

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

Within-node separator search: the number of leading separators that are at most k, which is the position of the child a search for k enters. One comparison per separator passed, and one more where it stops.

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

Lookup. At an interior node, pass the separators that are at most k and descend into the child introduced by the last of them; in a leaf, scan the entries. visit fires once at each node on the path.

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

Membership test: whether lookup succeeds.

theorem Cslib.DataStructure.BPlusTree.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 (Branch α β)) :
f (slotM le k rest) = slotM (fun (a b : α) => f (le a b)) k rest

A monad morphism commutes with the within-node separator search.

theorem Cslib.DataStructure.BPlusTree.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 : BPlusTree α β) :
f (lookupM le visit k tr) = lookupM (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with lookup.

theorem Cslib.DataStructure.BPlusTree.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 : BPlusTree α β) :
f (containsM le visit k tr) = containsM (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with the membership test.

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

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

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

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

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

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

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

slot is the Id instantiation of slotM.

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

lookup is the Id instantiation of lookupM.

contains is the Id instantiation of containsM.

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

The separator search in a node without separators stops at position zero.

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

The separator search passes a separator that is at most k, and stops otherwise.

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

The pure separator search counts the leading separators that are at most k.

theorem Cslib.DataStructure.BPlusTree.lookup_leaf {α β : Type} [LinearOrder α] (k : α) (entries : List (α × β)) :

Lookup in a leaf is the scan of its entries.

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

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

@[simp]
theorem Cslib.DataStructure.BPlusTree.lookup_node_cons {α β : Type} [LinearOrder α] (k s : α) (first c : BPlusTree α β) (rest : List (Branch α β)) :
lookup k (first.node ((s, c) :: rest)) = if s ≤ k then lookup k (c.node rest) else lookup k first

Lookup at a node's first separator: pass it when it is at most the query, and otherwise descend into the child before it.

@[simp]

Membership is success of lookup.

The separator convention along a node #

theorem Cslib.DataStructure.BPlusTree.toList_node_cons {α β : Type} {first c : BPlusTree α β} {s : α} {rest : List (Branch α β)} :
(first.node ((s, c) :: rest)).toList = first.toList ++ (c.node rest).toList

The entries of a node are those of its first child followed by those of the node without its first child and separator.

theorem Cslib.DataStructure.BPlusTree.Separated.tail {α β : Type} [LinearOrder α] {first c : BPlusTree α β} {s : α} {rest : List (Branch α β)} (h : (first.node ((s, c) :: rest)).Separated) :
(c.node rest).Separated

A separated node without its first child and separator is separated.

theorem Cslib.DataStructure.BPlusTree.Occupied.size_pos {α β : Type} {t : ℕ} {c : BPlusTree α β} (ht : 2 ≤ t) (ho : Occupied t c) :
0 < c.size

An occupied tree has an entry when 2 ≤ t: every leaf has at least t - 1 entries.

theorem Cslib.DataStructure.BPlusTree.Separated.le_of_mem_tail {α β : Type} [LinearOrder α] {first c : BPlusTree α β} {s : α} {rest : List (Branch α β)} (h : (first.node ((s, c) :: rest)).Separated) (hs : (first.node ((s, c) :: rest)).toList.IsSortedAssoc) (hc : 0 < c.size) {q : α × β} (hq : q ∈ (c.node rest).toList) :
s ≤ q.1

In a separated node with sorted entries whose second child is not empty, the first separator is at most every key after the first child: the keys of the second child are at least it by the convention, and the later keys are above those.

Conditions needed to route a search in a B+-tree against its ordered list of entries. They also hold for a suffix of an interior node, where the remaining root may have no separator.

  • two_le : 2 ≤ t

    The degree makes occupied children nonempty.

  • separated : tr.Separated

    Separators divide the keys at each node.

  • sorted : tr.toList.IsSortedAssoc

    The flattened entries are sorted by key.

  • occupied (c : BPlusTree α β) : c ∈ tr.children → Occupied t c

    All immediate children are occupied.

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

A well-formed tree satisfies the search invariant.

theorem Cslib.DataStructure.BPlusTree.SearchInvariant.child {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : SearchInvariant t tr) {c : BPlusTree α β} (hc : c ∈ tr.children) :

The search invariant passes to any child.

theorem Cslib.DataStructure.BPlusTree.SearchInvariant.tail {α β : Type} [LinearOrder α] {t : ℕ} {first c : BPlusTree α β} {s : α} {rest : List (Branch α β)} (h : SearchInvariant t (first.node ((s, c) :: rest))) :

Passing the first separator preserves the search invariant on the remaining node.

theorem Cslib.DataStructure.BPlusTree.SearchInvariant.induction {α β : Type} [LinearOrder α] {t : ℕ} {motive : (tr : BPlusTree α β) → SearchInvariant t tr → Prop} (leaf : ∀ (entries : List (α × β)) (h : SearchInvariant t (leaf entries)), motive (leaf entries) h) (node_nil : ∀ (first : BPlusTree α β) (h : SearchInvariant t (first.node [])), motive first ⋯ → motive (first.node []) h) (node_cons : ∀ (first : BPlusTree α β) (s : α) (c : BPlusTree α β) (rest : List (α × BPlusTree α β)) (h : SearchInvariant t (first.node ((s, c) :: rest))), (∀ p ∈ first.toList, p.1 < s) → (∀ p ∈ (c.node rest).toList, s ≤ p.1) → motive first ⋯ → motive (c.node rest) ⋯ → motive (first.node ((s, c) :: rest)) h) {tr : BPlusTree α β} (h : SearchInvariant t tr) :
motive tr h

Induction under the search invariant splits a node with separators at its first separator and supplies the key bounds for both parts.

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

Model equation of lookup on well-formed trees.

theorem Cslib.DataStructure.BPlusTree.lookup_eq_some_iff {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} {k : α} (h : IsWF t tr) {v : β} :
lookup k tr = some v ↔ (k, v) ∈ tr

On a well-formed tree, lookup returns a value exactly when the entry belongs to the tree.

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

On a well-formed tree, contains decides whether the key occurs.