Documentation

CslibFrontier.DataStructure.BTree.Range

Closed-interval queries in classic B-trees #

rangeM traverses a B-tree in order, pruning children whose separator bounds exclude the closed interval. It takes a monadic comparator and a node-visit action, with no order instance; range runs the same algorithm in Id with the comparator of a linear order. Each node entered is visited once, before its triples are scanned. Scanning triples with rangeRestM adds no visits, and empty leaves are never visited. Neither occupancy nor balance is needed for the model equation, only sorted entries. The constructor equations support evaluation without unfolding the well-founded recursion.

Main definitions #

Main results #

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

@[irreducible]
def Cslib.DataStructure.BTree.rangeM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (lo hi : α) :
BTree α β → m (List (α × β))

Entries in the closed interval, in key order when the comparator decides the key order and toList is sorted. Visit each node on entry; enter a child only when its adjacent separators permit an entry in the interval.

@[irreducible]
def Cslib.DataStructure.BTree.rangeRestM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (lo hi : α) (first : BTree α β) :
List (Triple α β) → m (List (α × β))

Scan a node's remaining triples without additional visits. first precedes the triples; any separator before it has already passed the upper-bound comparison.

theorem Cslib.DataStructure.BTree.rangeM_leaf {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (lo hi : α) :
rangeM le visit lo hi leaf = pure []

The empty leaf returns no entries and is not visited.

theorem Cslib.DataStructure.BTree.rangeM_node {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (lo hi : α) (first : BTree α β) (rest : List (Triple α β)) :
rangeM le visit lo hi (first.node rest) = do visit rangeRestM le visit lo hi first rest

A node is visited once before scanning its triples.

theorem Cslib.DataStructure.BTree.rangeRestM_nil {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (lo hi : α) (first : BTree α β) :
rangeRestM le visit lo hi first [] = rangeM le visit lo hi first

After the last separator, search the last child.

theorem Cslib.DataStructure.BTree.rangeRestM_cons {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (lo hi k : α) (v : β) (first c : BTree α β) (rest : List (Triple α β)) :
rangeRestM le visit lo hi first ((k, v, c) :: rest) = do let loLeK ← le lo k have __do_jp : List (α × β) → m (List (α × β)) := fun (below : List (α × β)) => do let __do_lift ← le k hi if __do_lift = true then do let after ← rangeRestM le visit lo hi c rest pure ((below ++ if loLeK = true then [(k, v)] else []) ++ after) else pure below if loLeK = true then do let below ← rangeM le visit lo hi first __do_jp below else do let below ← pure [] __do_jp below

A separator bounds the child before it from above and all remaining entries from below. The scan stops when the separator exceeds hi.

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

A monad morphism commutes with closed-interval queries.

theorem Cslib.IsMonadHom.map_rangeRestM {α β : 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) (lo hi : α) (first : DataStructure.BTree α β) (rest : List (DataStructure.BTree.Triple α β)) :
f (DataStructure.BTree.rangeRestM le visit lo hi first rest) = DataStructure.BTree.rangeRestM (fun (a b : α) => f (le a b)) (f visit) lo hi first rest

A monad morphism commutes with the scan of a node's triples in a closed-interval query.

def Cslib.DataStructure.BTree.range {α β : Type} [LinearOrder α] (lo hi : α) (tr : BTree α β) :
List (α × β)

Pure closed-interval query: the Id instantiation of rangeM with a trivial visit.

theorem Cslib.DataStructure.BTree.range_def {α β : Type} [LinearOrder α] {tr : BTree α β} (lo hi : α) :
range lo hi tr = (rangeM SortedAssoc.leI (pure ()) lo hi tr).run

range is the Id instantiation of rangeM.

@[simp]
theorem Cslib.DataStructure.BTree.range_leaf {α β : Type} [LinearOrder α] (lo hi : α) :
range lo hi leaf = []

The empty tree has no entries in any interval.

@[simp]
theorem Cslib.DataStructure.BTree.range_node_nil {α β : Type} [LinearOrder α] (lo hi : α) (first : BTree α β) :
range lo hi (first.node []) = range lo hi first

A node without keys delegates the query to its only child.

@[simp]
theorem Cslib.DataStructure.BTree.range_node_cons {α β : Type} [LinearOrder α] (lo hi k : α) (v : β) (first c : BTree α β) (rest : List (Triple α β)) :
range lo hi (first.node ((k, v, c) :: rest)) = (if lo ≤ k then range lo hi first else []) ++ if k ≤ hi then (if lo ≤ k then [(k, v)] else []) ++ range lo hi (c.node rest) else []

At the first separator, prune the preceding child below lo, and stop the remaining scan above hi; include the separator itself exactly when it lies in the closed interval.

theorem Cslib.DataStructure.BTree.length_range_node_cons {α β : Type} [LinearOrder α] (lo hi k : α) (v : β) (first c : BTree α β) (rest : List (Triple α β)) :
(range lo hi (first.node ((k, v, c) :: rest))).length = (if lo ≤ k then (range lo hi first).length else 0) + if k ≤ hi then (range lo hi (c.node rest)).length + if lo ≤ k then 1 else 0 else 0

The output length at the first separator: the preceding child's contribution when the separator is at least lo, and, when it is at most hi, the separator itself if it is at least lo and the contribution of the node without its first child and separator.

theorem Cslib.DataStructure.BTree.range_eq_of_isSortedAssoc {α β : Type} [LinearOrder α] {tr : BTree α β} (h : tr.toList.IsSortedAssoc) (lo hi : α) :
range lo hi tr = SortedAssoc.range lo hi tr.toList

Model equation: on a tree with sorted entries, the query returns the model's restriction of toList to the closed interval.

theorem Cslib.DataStructure.BTree.range_eq {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (lo hi : α) :
range lo hi tr = SortedAssoc.range lo hi tr.toList

Model equation for closed-interval queries on well-formed trees.

@[simp]
theorem Cslib.DataStructure.BTree.mem_range_iff_of_isSortedAssoc {α β : Type} [LinearOrder α] {tr : BTree α β} (h : tr.toList.IsSortedAssoc) (lo hi : α) (p : α × β) :
p ∈ range lo hi tr ↔ p ∈ tr ∧ lo ≤ p.1 ∧ p.1 ≤ hi

On a tree with sorted entries, the output contains exactly the tree's entries whose keys lie in the closed interval.

theorem Cslib.DataStructure.BTree.mem_range_iff {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (lo hi : α) (p : α × β) :
p ∈ range lo hi tr ↔ p ∈ tr ∧ lo ≤ p.1 ∧ p.1 ≤ hi

In a well-formed tree, range membership is tree membership with the closed-interval condition on the key.