Comparator-polymorphic scans of sorted association lists #
The queries of Cslib.SortedAssoc written as left-to-right scans over an arbitrary monad m,
with a comparator le : α → α → m Bool and no order instance in scope, so that every comparison
of keys goes through le. Instantiating m then decides what a comparison costs: the identity
monad gives the pure query, and a query monad counts comparisons. This is the shape of CSLib's
List.mergeSortM and List.idRun_mergeSortM.
Each scan stops as early as a sorted list allows. lookupM, rankM and predM stop at the
first key that is not below the query; succM stops at the first key above it; rangeM skips
the keys below the lower bound and then takes keys up to the upper bound with takeUpToM.
rankM, predM and succM ask one question per entry up to and including the one where they
stop. lookupM asks one more where it stops, to tell equality from a strictly larger key.
rangeM runs two phases: one question against the lower bound per entry up to and including the
first key that is not below it, then one question against the upper bound per entry from that
same key up to and including the first key above the upper bound. The first entry that is not
below the lower bound is therefore compared twice, once in each phase.
With the comparator Cslib.SortedAssoc.leI of a linear order, the identity-monad instance of
each scan is the corresponding function of Cslib.SortedAssoc on a sorted list (idRun_lookupM
and its siblings). The scan for the successor needs no sortedness. The others do, because they
stop early: on an unsorted list they may miss entries that the model function, a filter, finds.
map_lookupM and its siblings say that a monad morphism commutes with each scan, which is how
an instrumented instance is related to the pure one.
A leaf of a B+-tree is a sorted association list, and these scans are its queries.
Main definitions #
Cslib.SortedAssoc.lookupM,rankM,predM,succM: lookup, rank, strict predecessor and strict successor.Cslib.SortedAssoc.takeUpToM,rangeM: the prefix of keys up to a bound, and restriction to a closed interval.
Main results #
Cslib.SortedAssoc.idRun_lookupM,idRun_rankM,idRun_predM,idRun_succM,idRun_rangeM: the pure instances areList.lookup,rank,pred?,succ?andrange.Cslib.SortedAssoc.map_lookupM,map_rankM,map_predM,map_succM,map_takeUpToM,map_rangeM: a monad morphism commutes with each scan.
A monad morphism commutes with taking the keys up to a bound.
The pure lookup scan at an entry: stop at a key not below k, and move on otherwise.
The pure predecessor scan at an entry: stop at a key not below k, and otherwise keep the
entry as the fallback.
The pure scan up to a bound is List.takeWhile, on any list.
The pure range scan at an entry: at a key not below lo, take the keys up to hi; otherwise
move on.
On a sorted list the pure lookup scan is List.lookup.
On a sorted list the pure rank scan is rank.
On a sorted list the pure predecessor scan is pred?.
On a sorted list whose keys are all at least lo, restriction to [lo, hi] is the prefix of
keys up to hi.
On a sorted list the pure range scan is range.