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 #
slotM,slot: within-node linear search, the position of the first key not below the query.lookupM,lookup: search for the value at a key.containsM,contains: membership test.
Main results #
Cslib.IsMonadHom.map_slotM,Cslib.IsMonadHom.map_lookupM,Cslib.IsMonadHom.map_containsM: a monad morphism commutes with the operations, so that instantiations in other monads keep the pure value.slot_eq_length_takeWhile: the pure within-node search counts the keys below the query.lookup_eq_of_isSortedAssoc,lookup_eq: on a tree with sorted entries, and in particular on a well-formed tree,lookupisList.lookupontoList.contains_iff_mem_keys: a key is in a well-formed tree exactly when it is among its keys.
References #
Ported from cslib-community/EpsilonEridani at commit 1966ab476:
EpsilonEridani/Algorithms/Lean/BTree/Lookup.lean.
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.
A monad morphism commutes with the within-node search.
A monad morphism commutes with lookup.
A monad morphism commutes with the membership test.
Pure lookup: the Id instantiation of lookupM with visit := pure ().
The within-node search in a node without keys stops at position zero.
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.
Membership is success of lookup.
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.
The pure within-node search counts the keys below k before the first key not below it.
The position found by the within-node search is at most the number of keys.
Lookup in the empty tree fails.
Model equation: on a tree with sorted entries, lookup is lookup in toList.
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.
Membership on well-formed trees is success of lookup in toList.