Comparator-polymorphic updates of sorted association lists #
Insertion and erasure 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. They are the updates that go with the queries of
Cslib.SortedAssoc.lookupM and its siblings, and they compare in the same pattern as lookupM:
one question per entry up to and including the first key that is not below the query, and one
more at that entry to tell equality from a strictly larger key.
insertM replaces the entry where it stops if the keys are equal and inserts before it
otherwise, appending when it reaches the end of the list. eraseM drops the entry where it stops
if the keys are equal and otherwise leaves the list as it is.
With the comparator Cslib.SortedAssoc.leI of a linear order, the identity-monad instance of
insertM is Cslib.SortedAssoc.insert on every list: in a linear order the two recursions agree
branch by branch.
The instance of eraseM is Cslib.SortedAssoc.erase on a sorted list. Sortedness is needed
there: the scan removes at most one entry and stops early, whereas the model function, a filter,
removes every entry with the key.
A leaf of a B+-tree is a sorted association list, and these scans are its updates.
Main definitions #
Cslib.SortedAssoc.insertM: insert-or-replace by a scan.Cslib.SortedAssoc.eraseM: erasure by a scan.
Main results #
Cslib.SortedAssoc.idRun_insertM: the pure instance ofinsertMisinsert.Cslib.SortedAssoc.idRun_eraseM: on a sorted list the pure instance oferaseMiserase.Cslib.SortedAssoc.map_insertM,map_eraseM: a monad morphism commutes with each scan.
Insert-or-replace by a scan: stop at the first key not below k and test it for equality
with a second comparison; replace that entry if the keys are equal, and insert before it
otherwise. A scan that reaches the end of the list appends.
On a sorted list the pure erase scan is erase.