Sorted association lists #
The specification model for ordered maps: a list of (key, value) pairs whose keys are strictly
increasing, so that keys are distinct. List.IsSortedAssoc is the predicate, stated as
List.Pairwise (· < ·) on the key list, which is the form that the splitting and concatenation
lemmas consume; Mathlib's List.SortedLT is the same predicate by List.sortedLT_iff_pairwise.
The model operations live in Cslib.SortedAssoc and are plain list functions. Lookup is
List.lookup; insertion replaces an existing key; erasure, strict predecessor and successor,
closed-interval restriction and rank are defined here. The minimum and maximum of a sorted list
are its head? and getLast?. Together these are the reference semantics against which
tree-shaped maps are verified through their entry lists. The operations are @[expose]d so
that decide and rfl evaluate them on concrete lists from other modules.
The lemmas come in two kinds. Those about the operations say what each does to a sorted list.
Those about ++ are the shapes used when a tree node's entry list is split as
first ++ (k, v) :: rest. Specifying a search tree by its sorted in-order list and verifying
its operations through such splitting lemmas is the method of [Nip16], used for B-trees in
[Mun21].
Main definitions #
List.IsSortedAssoc: the keys of an association list are strictly increasing.Cslib.SortedAssoc.insert,Cslib.SortedAssoc.erase: insert-or-replace and erasure.Cslib.SortedAssoc.leI: the decidable comparator of a linear order in the identity monad, shared by the comparator-polymorphic operations of the maps specified by this model.Cslib.SortedAssoc.pred?,Cslib.SortedAssoc.succ?: strict predecessor and successor.Cslib.SortedAssoc.range,Cslib.SortedAssoc.rank: closed-interval restriction and the number of keys strictly below a key.
Main results #
List.isSortedAssoc_append_iff,List.isSortedAssoc_append_cons_iff: sortedness of a list split at an entry.List.IsSortedAssoc.lookup_eq_some_iff,List.IsSortedAssoc.eq_of_forall_mem_iff: on sorted lists lookup finds exactly the entries present, and membership determines the list.List.IsSortedAssoc.head?_eq_some_iff,List.IsSortedAssoc.getLast?_eq_some_iff: the head and last entry of a sorted list have the least and greatest key.Cslib.SortedAssoc.lookup_insert_self,Cslib.SortedAssoc.lookup_erase_self,Cslib.SortedAssoc.length_insert,Cslib.SortedAssoc.length_erase: lookup and length after insertion and erasure.Cslib.SortedAssoc.pred?_eq_some_iff,Cslib.SortedAssoc.succ?_eq_some_iff: on a sorted list the predecessor (successor) is the greatest (least) entry with key below (above) the query.Cslib.SortedAssoc.rank_eq_length_takeWhile: on a sorted list the rank is the length of the prefix of smaller keys.
References #
- T. Nipkow, Automatic Functional Correctness Proofs for Functional Search Trees
- N. Mündler, A Verified Imperative Implementation of B-Trees
Ported from cslib-community/EpsilonEridani at commit 1966ab476:
EpsilonEridani/Foundations/Data/List/SortedAssoc.lean.
An association list whose keys are strictly increasing, hence distinct.
Instances For
IsSortedAssoc restated: the key list is strictly increasing.
IsSortedAssoc restated through Mathlib's SortedLT.
The entries of a sorted association list are pairwise increasing in their keys.
The keys of a sorted association list are distinct.
The entries of a sorted association list are distinct.
A sorted association list is an entry whose key is below every later key, then a sorted list.
A concatenation is sorted iff both parts are and every key of the first part is below every key of the second.
Splitting a sorted list at a key: the shape of the lemma used at every tree node.
A sublist of a sorted association list is sorted.
Filtering a sorted association list keeps it sorted.
Keys are distinct: two entries of a sorted list with the same key are the same entry.
Two sorted association lists with the same entries are equal: the model is determined by its membership.
Erasure #
Erasure and its unconditional laws need only decidable equality of keys.
Remove every entry with key k; on a sorted list there is at most one.
Erasing a key that no entry carries is the identity.
Lookup of the erased key fails, on any list.
Erasure does not change lookup at other keys, on any list.
Erasing an absent key is the identity.
When keys are distinct, erasure removes one entry if the key is present and none otherwise.
The decidable comparator of a linear order, in the identity monad: the comparator at which a comparator-polymorphic operation on an ordered map is its pure form.
Insert (k, v) before the first entry whose key is at least k, replacing that entry if
its key is k. On a sorted list this is insert-or-replace at the sorted position.
The last entry with key strictly below k. On a sorted list this is the greatest such
entry, the strict predecessor of k.
The first entry with key strictly above k. On a sorted list this is the least such entry,
the strict successor of k.
Restriction to the entries with keys in the closed interval [lo, hi].
The rank of k: the number of entries with key strictly below it.
Insertion #
Insertion preserves sortedness.
On a sorted list, the entries after insertion are the new entry and the old entries with other keys.
Lookup of the inserted key finds the new value, on any list.
Insertion does not change lookup at other keys, on any list.
On a sorted list, insertion keeps the length if the key was present and adds one otherwise.
Insertion skips a prefix of smaller keys followed by an entry with a smaller key.
Erasure on sorted lists #
Erasing a key that sits between a prefix of smaller keys and a suffix of larger ones removes exactly that entry.
Erasure preserves sortedness.
On a sorted list, erasure removes one entry if the key was present and none otherwise.
Predecessor and successor #
Range and rank #
Range restriction keeps the first entry exactly when its key is in the interval.
Range restriction preserves sortedness.
On a sorted list, the rank is the length of the prefix of keys below k.