Documentation

CslibFrontier.DataStructure.BTree.Rank

Rank queries in classic B-trees #

rankM scans a node's separators and descends into one child, following the search in lookupM. Each separator passed contributes one plus the size of its preceding child. At the first separator at least the query, the search descends into the preceding child; a key equal to the query is not counted. The constructor equations support evaluation and monadic instantiations without unfolding the well-founded recursion.

Main definitions #

Main results #

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

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

Rank by a single descent. Scan past separators k' for which le k k' fails, adding the pure BTree.size of the child before each separator plus one. At the first separator with le k k', descend into the child before it; otherwise descend into the last child. When le decides the key order and toList is sorted, the result counts keys strictly below k, excluding equality. Scanning triples and computing pure subtree sizes are uncharged client computation in the node-visit and comparison cost models. visit fires once when the scan chooses a child, including at zero-key nodes, and never at an empty leaf.

theorem Cslib.DataStructure.BTree.rankM_leaf {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) :
rankM le visit k leaf = pure 0

The empty tree has rank zero and is not visited.

theorem Cslib.DataStructure.BTree.rankM_node_nil {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) (first : BTree α β) :
rankM le visit k (first.node []) = do visit rankM le visit k first

A node without keys is visited before searching its only child.

theorem Cslib.DataStructure.BTree.rankM_node_cons {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k k' : α) (v : β) (first c : BTree α β) (rest : List (Triple α β)) :
rankM le visit k (first.node ((k', v, c) :: rest)) = do let __do_lift ← le k k' if __do_lift = true then do visit rankM le visit k first else do let __do_lift ← rankM le visit k (c.node rest) pure (first.size + 1 + __do_lift)

Descend before the first separator at least the query; otherwise count the preceding child and separator and continue scanning, without an additional visit.

theorem Cslib.IsMonadHom.map_rankM {α β : 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.rankM le visit k tr) = DataStructure.BTree.rankM (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with the rank search.

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

Pure rank: the Id instantiation of rankM with a trivial visit action.

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

rank is the Id instantiation of rankM.

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

No keys are below the query in the empty tree.

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

A node without keys delegates rank to its only child.

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

A separator at least the query selects the preceding child; a smaller separator contributes its preceding child's size plus one before the scan continues.

Model equation: on a tree with sorted entries, rank counts exactly the entries of toList whose keys are strictly below the query.

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

Model equation for rank on well-formed trees.