Documentation

CslibFrontier.Foundations.Data.List.SortedAssoc.Update

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 #

Main results #

def Cslib.SortedAssoc.insertM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (k : α) (v : β) :
List (α × β) → m (List (α × β))

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.

def Cslib.SortedAssoc.eraseM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (k : α) :
List (α × β) → m (List (α × β))

Erasure by a scan: stop at the first key not below k and test it for equality with a second comparison; drop that entry if the keys are equal, and leave the list unchanged otherwise.

theorem Cslib.SortedAssoc.map_insertM {α β : Type} {m : Type → Type u_1} {n : Type → Type u_2} [Monad m] [Monad n] {f : {γ : Type} → m γ → n γ} (hf : IsMonadHom m n fun {α : Type} => f) (le : α → α → m Bool) (k : α) (v : β) (l : List (α × β)) :
f (insertM le k v l) = insertM (fun (a b : α) => f (le a b)) k v l

A monad morphism commutes with the insert scan.

theorem Cslib.SortedAssoc.map_eraseM {α β : Type} {m : Type → Type u_1} {n : Type → Type u_2} [Monad m] [Monad n] {f : {γ : Type} → m γ → n γ} (hf : IsMonadHom m n fun {α : Type} => f) (le : α → α → m Bool) (k : α) (l : List (α × β)) :
f (eraseM le k l) = eraseM (fun (a b : α) => f (le a b)) k l

A monad morphism commutes with the erase scan.

theorem Cslib.SortedAssoc.idRun_insertM {α β : Type} [LinearOrder α] (k : α) (v : β) (l : List (α × β)) :
(insertM leI k v l).run = insert k v l

The pure insert scan is insert, on any list.

theorem Cslib.SortedAssoc.idRun_eraseM {α β : Type} [LinearOrder α] {l : List (α × β)} (h : l.IsSortedAssoc) (k : α) :
(eraseM leI k l).run = erase k l

On a sorted list the pure erase scan is erase.