Utilities for lists of sigmas #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
This file includes several ways of interacting with list (sigma β)
, treated as a key-value store.
If α : Type*
and β : α → Type*
, then we regard s : sigma β
as having key s.1 : α
and value
s.2 : β s.1
. Hence, list (sigma β)
behaves like a key-value store.
Main Definitions #
list.keys
extracts the list of keys.list.nodupkeys
determines if the store has duplicate keys.list.lookup
/lookup_all
accesses the value(s) of a particular key.list.kreplace
replaces the first value with a given key by a given value.list.kerase
removes a value.list.kinsert
inserts a value.list.kunion
computes the union of two stores.list.kextract
returns a value with a given key and the rest of the values.
keys
#
nodupkeys
#
lookup
#
lookup a l
is the first value in l
corresponding to the key a
,
or none
if no such element exists.
Equations
- list.lookup a (⟨a', b⟩ :: l) = dite (a' = a) (λ (h : a' = a), option.some (h.rec_on b)) (λ (h : ¬a' = a), list.lookup a l)
- list.lookup a list.nil = option.none
lookup_all
#
lookup_all a l
is the list of all values in l
corresponding to the key a
.
Equations
- list.lookup_all a (⟨a', b⟩ :: l) = dite (a' = a) (λ (h : a' = a), h.rec_on b :: list.lookup_all a l) (λ (h : ¬a' = a), list.lookup_all a l)
- list.lookup_all a list.nil = list.nil
kreplace
#
Replaces the first value with key a
by b
.
Equations
- list.kreplace a b = list.lookmap (λ (s : sigma β), ite (a = s.fst) (option.some ⟨a, b⟩) option.none)
kerase
#
Remove the first pair with the key a
.
Equations
- list.kerase a = list.erasep (λ (s : sigma β), a = s.fst)
kinsert
#
Insert the pair ⟨a, b⟩
and erase the first pair with the key a
.
Equations
- list.kinsert a b l = ⟨a, b⟩ :: list.kerase a l
kextract
#
Finds the first entry with a given key a
and returns its value (as an option
because there
might be no entry with key a
) alongside with the rest of the entries.
Equations
- list.kextract a (s :: l) = dite (s.fst = a) (λ (h : s.fst = a), (option.some (h.rec_on s.snd), l)) (λ (h : ¬s.fst = a), list.kextract._match_1 a s (list.kextract a l))
- list.kextract a list.nil = (option.none (β a), list.nil (sigma β))
- list.kextract._match_1 a s (b', l') = (b', s :: l')
dedupkeys
#
Remove entries with duplicate keys from l : list (sigma β)
.
Equations
- list.dedupkeys = list.foldr (λ (x : sigma β), list.kinsert x.fst x.snd) list.nil
kunion
#
kunion l₁ l₂
is the append to l₁ of l₂ after, for each key in l₁, the
first matching pair in l₂ is erased.