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 #
rangeM,rangeRestM,range: the entries with keys in a closed interval, in order.
Main results #
Cslib.IsMonadHom.map_rangeM: a monad morphism commutes with the query.range_eq_of_isSortedAssoc,range_eq: on a tree with sorted entries, and in particular on a well-formed tree,rangeisSortedAssoc.rangeontoList.mem_range_iff: range membership is tree membership with the key in the interval.
Ported from cslib-community/EpsilonEridani at commit 1966ab476:
EpsilonEridani/Algorithms/Lean/BTree/Range.lean.
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.
Scan a node's remaining triples without additional visits. first precedes the triples;
any separator before it has already passed the upper-bound comparison.
A separator bounds the child before it from above and all remaining entries from below.
The scan stops when the separator exceeds hi.
A monad morphism commutes with closed-interval queries.
A monad morphism commutes with the scan of a node's triples in a closed-interval query.
Pure closed-interval query: the Id instantiation of rangeM with a trivial visit.
The empty tree has no entries in any interval.
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.
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.
Model equation: on a tree with sorted entries, the query returns the model's restriction
of toList to the closed interval.
Model equation for closed-interval queries on well-formed trees.
On a tree with sorted entries, the output contains exactly the tree's entries whose keys lie in the closed interval.
In a well-formed tree, range membership is tree membership with the closed-interval condition on the key.