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 #
slotM,slot: the within-node separator search.lookupM,containsM: lookup and the membership test, polymorphic.lookup,contains: the pure operations.SearchInvariant,SearchInvariant.induction: the premises and induction shared by the ordered-list model proofs.
Main results #
map_slotM,map_lookupM,map_containsM: a monad morphism commutes with the operations.slot_eq_length_takeWhile: the pure separator search counts the leading separators that are at most the query.Occupied.size_pos: an occupied tree has an entry when2 ≤ t.lookup_eq: on a well-formed tree,lookupisList.lookupontoList.contains_iff: on a well-formed tree,containsdecides membership of the key.
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.
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.
A monad morphism commutes with the within-node separator search.
A monad morphism commutes with lookup.
A monad morphism commutes with the membership test.
Pure within-node separator search: the Id instantiation of slotM.
Pure lookup: the Id instantiation of lookupM with visit := pure ().
Pure membership test: the Id instantiation of containsM with visit := pure ().
The separator search in a node without separators stops at position zero.
The pure separator search counts the leading separators that are at most k.
Lookup in a leaf is the scan of its entries.
Lookup at a node's first separator: pass it when it is at most the query, and otherwise descend into the child before it.
Membership is success of lookup.
The separator convention along a node #
The entries of a node are those of its first child followed by those of the node without its first child and separator.
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.
All immediate children are occupied.
A well-formed tree satisfies the search invariant.
The search invariant passes to any child.
Passing the first separator preserves the search invariant on the remaining node.
Induction under the search invariant splits a node with separators at its first separator and supplies the key bounds for both parts.
Model equation of lookup on well-formed trees.