Documentation

CslibFrontier.Foundations.Data.List.SortedAssoc.Scan

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 #

Main results #

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

Lookup by a scan: stop at the first key not below k and test it for equality with a second comparison.

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

Rank by a scan: the number of entries before the first key not below k.

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

Strict predecessor by a scan: the last entry before the first key not below k.

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

Strict successor by a scan: the first entry whose key is not at most k.

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

The entries before the first key that is not at most hi.

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

Restriction to the closed interval [lo, hi] by a scan: skip the keys below lo, then take the keys up to hi.

theorem Cslib.SortedAssoc.map_lookupM {α β : 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 (lookupM le k l) = lookupM (fun (a b : α) => f (le a b)) k l

A monad morphism commutes with the lookup scan.

theorem Cslib.SortedAssoc.map_rankM {α β : 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 (rankM le k l) = rankM (fun (a b : α) => f (le a b)) k l

A monad morphism commutes with the rank scan.

theorem Cslib.SortedAssoc.map_predM {α β : 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 (predM le k l) = predM (fun (a b : α) => f (le a b)) k l

A monad morphism commutes with the predecessor scan.

theorem Cslib.SortedAssoc.map_succM {α β : 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 (succM le k l) = succM (fun (a b : α) => f (le a b)) k l

A monad morphism commutes with the successor scan.

theorem Cslib.SortedAssoc.map_takeUpToM {α β : 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) (hi : α) (l : List (α × β)) :
f (takeUpToM le hi l) = takeUpToM (fun (a b : α) => f (le a b)) hi l

A monad morphism commutes with taking the keys up to a bound.

theorem Cslib.SortedAssoc.map_rangeM {α β : 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) (lo hi : α) (l : List (α × β)) :
f (rangeM le lo hi l) = rangeM (fun (a b : α) => f (le a b)) lo hi l

A monad morphism commutes with the range scan.

theorem Cslib.SortedAssoc.idRun_lookupM_cons {α β : Type} [LinearOrder α] {l : List (α × β)} {k k' : α} {v : β} :
(lookupM leI k ((k', v) :: l)).run = if k ≤ k' then if k' ≤ k then some v else none else (lookupM leI k l).run

The pure lookup scan at an entry: stop at a key not below k, and move on otherwise.

theorem Cslib.SortedAssoc.idRun_rankM_cons {α β : Type} [LinearOrder α] {l : List (α × β)} {k : α} {p : α × β} :
(rankM leI k (p :: l)).run = if k ≤ p.1 then 0 else (rankM leI k l).run + 1

The pure rank scan at an entry: stop at a key not below k, and count it otherwise.

theorem Cslib.SortedAssoc.idRun_predM_cons {α β : Type} [LinearOrder α] {l : List (α × β)} {k : α} {p : α × β} :
(predM leI k (p :: l)).run = if k ≤ p.1 then none else (predM leI k l).run.or (some p)

The pure predecessor scan at an entry: stop at a key not below k, and otherwise keep the entry as the fallback.

theorem Cslib.SortedAssoc.idRun_succM_cons {α β : Type} [LinearOrder α] {l : List (α × β)} {k : α} {p : α × β} :
(succM leI k (p :: l)).run = if p.1 ≤ k then (succM leI k l).run else some p

The pure successor scan at an entry: pass a key that is at most k, and stop otherwise.

theorem Cslib.SortedAssoc.idRun_takeUpToM {α β : Type} [LinearOrder α] (hi : α) (l : List (α × β)) :
(takeUpToM leI hi l).run = List.takeWhile (fun (p : α × β) => decide (p.1 ≤ hi)) l

The pure scan up to a bound is List.takeWhile, on any list.

theorem Cslib.SortedAssoc.idRun_rangeM_cons {α β : Type} [LinearOrder α] {l : List (α × β)} {lo hi : α} {p : α × β} :
(rangeM leI lo hi (p :: l)).run = if lo ≤ p.1 then (takeUpToM leI hi (p :: l)).run else (rangeM leI lo hi l).run

The pure range scan at an entry: at a key not below lo, take the keys up to hi; otherwise move on.

theorem Cslib.SortedAssoc.idRun_lookupM {α β : Type} [LinearOrder α] {l : List (α × β)} (h : l.IsSortedAssoc) (k : α) :

On a sorted list the pure lookup scan is List.lookup.

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

On a sorted list the pure rank scan is rank.

theorem Cslib.SortedAssoc.idRun_predM {α β : Type} [LinearOrder α] {l : List (α × β)} (h : l.IsSortedAssoc) (k : α) :
(predM leI k l).run = pred? k l

On a sorted list the pure predecessor scan is pred?.

theorem Cslib.SortedAssoc.idRun_succM {α β : Type} [LinearOrder α] (k : α) (l : List (α × β)) :
(succM leI k l).run = succ? k l

The pure successor scan is succ?, on any list.

theorem Cslib.SortedAssoc.range_eq_takeWhile {α β : Type} [LinearOrder α] {l : List (α × β)} {lo hi : α} (h : l.IsSortedAssoc) (hlo : ∀ p ∈ l, lo ≤ p.1) :
range lo hi l = List.takeWhile (fun (p : α × β) => decide (p.1 ≤ hi)) l

On a sorted list whose keys are all at least lo, restriction to [lo, hi] is the prefix of keys up to hi.

theorem Cslib.SortedAssoc.idRun_rangeM {α β : Type} [LinearOrder α] {l : List (α × β)} (h : l.IsSortedAssoc) (lo hi : α) :
(rangeM leI lo hi l).run = range lo hi l

On a sorted list the pure range scan is range.