The areas / V33. Data structures and computation
Mathlib.Data.List.AList
92 declarations. Mathlib docs · Source
Association Lists
This file defines association lists. An association list is a list where every element consists of a key and a value, and no two entries have the same key. The type of the value is allowed to be dependent on the type of the key.
This type dependence is implemented using Sigma: The elements of the list are of type Sigma β,
for some type index β.
Main definitions
Association lists are represented by the AList structure. This file defines this structure and
provides ways to access, modify, and combine ALists.
AList.keysreturns a list of keys of the alist.AList.membershipreturns membership in the set of keys.AList.eraseremoves a certain key.AList.insertadds a key-value mapping to the list.AList.unioncombines two association lists.
References
keys
mem
empty
singleton
lookup
replace
erase
insert
extract
union
disjoint
theorem AList.extract.match_1.congr_eq_1 E1 #
∀ {α : Type u_2} {β : α → Type u_1} (a : α) (motive : (x : Option (β a) × List (Sigma β)) → x.2.NodupKeys → Sort u_3)
(x : Option (β a) × List (Sigma β)) (this : x.2.NodupKeys)
(h_1 : (b : Option (β a)) → (l : List (Sigma β)) → (h : (b, l).2.NodupKeys) → motive (b, l) h) (b : Option (β a))
(l : List (Sigma β)) (h : (b, l).2.NodupKeys),
x = (b, l) →
this ≍ h →
(match x, this with
| (b, l), h => h_1 b l h) ≍
h_1 b l h
theorem AList.mk.injEq E0 #
∀ {α : Type u} {β : α → Type v} (entries : List (Sigma β)) (nodupKeys : entries.NodupKeys) (entries_1 : List (Sigma β))
(nodupKeys_1 : entries_1.NodupKeys),
({ entries := entries, nodupKeys := nodupKeys } = { entries := entries_1, nodupKeys := nodupKeys_1 }) =
(entries = entries_1)
theorem AList.lookup.congr_simp E1 #
∀ {α : Type u} {β : α → Type v} {inst : DecidableEq α} [inst_1 : DecidableEq α] (a : α) (s s_1 : AList β),
s = s_1 → AList.lookup a s = AList.lookup a s_1
theorem AList.mk.congr_simp E1 #
∀ {α : Type u} {β : α → Type v} (entries entries_1 : List (Sigma β)) (e_entries : entries = entries_1)
(nodupKeys : entries.NodupKeys),
{ entries := entries, nodupKeys := nodupKeys } = { entries := entries_1, nodupKeys := ⋯ }
theorem AList.insertRec.match_1.congr_eq_2 E1 #
∀ {α : Type u_1} {β : α → Type u_2} (motive : AList β → Sort u_3) (x : AList β)
(h_1 : (nodupKeys : [].NodupKeys) → motive { entries := [], nodupKeys := nodupKeys })
(h_2 : (c : Sigma β) → (l : List (Sigma β)) → (h : (c :: l).NodupKeys) → motive { entries := c :: l, nodupKeys := h })
(c : Sigma β) (l : List (Sigma β)) (h : (c :: l).NodupKeys),
x = { entries := c :: l, nodupKeys := h } →
(match x with
| { entries := [], nodupKeys := nodupKeys } => h_1 nodupKeys
| { entries := c :: l, nodupKeys := h } => h_2 c l h) ≍
h_2 c l h
def AList.ctorIdx E1 #
{α : Type u} → {β : α → Type v} → AList β → ℕ
theorem AList.mk.inj E1 #
∀ {α : Type u} {β : α → Type v} {entries : List (Sigma β)} {nodupKeys : entries.NodupKeys} {entries_1 : List (Sigma β)}
{nodupKeys_1 : entries_1.NodupKeys},
{ entries := entries, nodupKeys := nodupKeys } = { entries := entries_1, nodupKeys := nodupKeys_1 } →
entries = entries_1
theorem AList.insertRec.match_1.congr_eq_1 E1 #
∀ {α : Type u_1} {β : α → Type u_2} (motive : AList β → Sort u_3) (x : AList β)
(h_1 : (nodupKeys : [].NodupKeys) → motive { entries := [], nodupKeys := nodupKeys })
(h_2 : (c : Sigma β) → (l : List (Sigma β)) → (h : (c :: l).NodupKeys) → motive { entries := c :: l, nodupKeys := h })
(nodupKeys : [].NodupKeys),
x = { entries := [], nodupKeys := nodupKeys } →
(match x with
| { entries := [], nodupKeys := nodupKeys } => h_1 nodupKeys
| { entries := c :: l, nodupKeys := h } => h_2 c l h) ≍
h_1 nodupKeys
structure AList E3 #
{α : Type u} → (α → Type v) → Type (max u v)
AList β is a key-value map stored as a List (i.e. a linked list).
It is a wrapper around certain List functions with the added constraint
that the list have unique keys.
ctor AList.mk E0 #
{α : Type u} → {β : α → Type v} → (entries : List (Sigma β)) → entries.NodupKeys → AList β
abbrev AList.entries E1 #
{α : Type u} → {β : α → Type v} → AList β → List (Sigma β)
The underlying List of an AList
theorem AList.nodupKeys E1 #
∀ {α : Type u} {β : α → Type v} (self : AList β), self.entries.NodupKeys
There are no duplicate keys in entries
def List.toAList E1 #
{α : Type u} → [DecidableEq α] → {β : α → Type v} → List (Sigma β) → AList β
Given l : List (Sigma β), create a term of type AList β by removing
entries with duplicate keys.
theorem AList.ext_iff E0 #
∀ {α : Type u} {β : α → Type v} {s t : AList β}, s = t ↔ s.entries = t.entries
theorem AList.ext E1 #
∀ {α : Type u} {β : α → Type v} {s t : AList β}, s.entries = t.entries → s = t
instance AList.instDecidableEq E0 #
{α : Type u} → {β : α → Type v} → [DecidableEq α] → [(a : α) → DecidableEq (β a)] → DecidableEq (AList β)
def AList.keys E3 #
{α : Type u} → {β : α → Type v} → AList β → List α
The list of keys of an association list.
theorem AList.keys_nodup E1 #
∀ {α : Type u} {β : α → Type v} (s : AList β), s.keys.Nodup
theorem AList.keys_mk E0 #
∀ {α : Type u} {β : α → Type v} (l : List (Sigma β)) (h : l.NodupKeys), { entries := l, nodupKeys := h }.keys = l.keys
instance AList.instMembership E0 #
{α : Type u} → {β : α → Type v} → Membership α (AList β)
The predicate a ∈ s means that s has a value associated to the key a.
theorem AList.mem_keys E0 #
∀ {α : Type u} {β : α → Type v} {a : α} {s : AList β}, a ∈ s ↔ a ∈ s.keys
theorem AList.mem_of_perm E0 #
∀ {α : Type u} {β : α → Type v} {a : α} {s₁ s₂ : AList β}, s₁.entries.Perm s₂.entries → (a ∈ s₁ ↔ a ∈ s₂)
theorem AList.mem_mk E0 #
∀ {α : Type u} {β : α → Type v} {l : List (Sigma β)} {h : l.NodupKeys} {x : α},
x ∈ { entries := l, nodupKeys := h } ↔ x ∈ l.keys
instance AList.instEmptyCollection E0 #
{α : Type u} → {β : α → Type v} → EmptyCollection (AList β)
The empty association list.
instance AList.instInhabited E0 #
{α : Type u} → {β : α → Type v} → Inhabited (AList β)
theorem AList.notMem_empty E0 #
∀ {α : Type u} {β : α → Type v} (a : α), a ∉ ∅
theorem AList.empty_entries E0 #
∀ {α : Type u} {β : α → Type v}, ∅.entries = []
theorem AList.keys_empty E0 #
∀ {α : Type u} {β : α → Type v}, ∅.keys = []
def AList.singleton E1 #
{α : Type u} → {β : α → Type v} → (a : α) → β a → AList β
The singleton association list.
theorem AList.singleton_entries E0 #
∀ {α : Type u} {β : α → Type v} (a : α) (b : β a), (AList.singleton a b).entries = [⟨a, b⟩]
theorem AList.keys_singleton E0 #
∀ {α : Type u} {β : α → Type v} (a : α) (b : β a), (AList.singleton a b).keys = [a]
def AList.lookup E1 #
{α : Type u} → {β : α → Type v} → [DecidableEq α] → (a : α) → AList β → Option (β a)
Look up the value associated to a key in an association list.
theorem AList.lookup_empty E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (a : α), AList.lookup a ∅ = none
theorem AList.lookup_isSome E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {s : AList β}, (AList.lookup a s).isSome = true ↔ a ∈ s
theorem AList.lookup_eq_none E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {s : AList β}, AList.lookup a s = none ↔ a ∉ s
theorem AList.mem_lookup_iff E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} {s : AList β},
b ∈ AList.lookup a s ↔ ⟨a, b⟩ ∈ s.entries
theorem AList.perm_lookup E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {s₁ s₂ : AList β},
s₁.entries.Perm s₂.entries → AList.lookup a s₁ = AList.lookup a s₂
instance AList.instDecidableMem E0 #
{α : Type u} → {β : α → Type v} → [DecidableEq α] → (a : α) → (s : AList β) → Decidable (a ∈ s)
theorem AList.keys_subset_keys_of_entries_subset_entries E1 #
∀ {α : Type u} {β : α → Type v} {s₁ s₂ : AList β}, s₁.entries ⊆ s₂.entries → s₁.keys ⊆ s₂.keys
def AList.replace E1 #
{α : Type u} → {β : α → Type v} → [DecidableEq α] → (a : α) → β a → AList β → AList β
Replace a key with a given value in an association list. If the key is not present it does nothing.
theorem AList.keys_replace E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (a : α) (b : β a) (s : AList β),
(AList.replace a b s).keys = s.keys
theorem AList.mem_replace E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a a' : α} {b : β a} {s : AList β},
a' ∈ AList.replace a b s ↔ a' ∈ s
theorem AList.perm_replace E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} {s₁ s₂ : AList β},
s₁.entries.Perm s₂.entries → (AList.replace a b s₁).entries.Perm (AList.replace a b s₂).entries
def AList.foldl E1 #
{α : Type u} → {β : α → Type v} → {δ : Type w} → (δ → (a : α) → β a → δ) → δ → AList β → δ
Fold a function over the key-value pairs in the map.
def AList.erase E3 #
{α : Type u} → {β : α → Type v} → [DecidableEq α] → α → AList β → AList β
Erase a key from the map. If the key is not present, do nothing.
theorem AList.keys_erase E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (a : α) (s : AList β), (AList.erase a s).keys = s.keys.erase a
theorem AList.mem_erase E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a a' : α} {s : AList β}, a' ∈ AList.erase a s ↔ a' ≠ a ∧ a' ∈ s
theorem AList.perm_erase E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {s₁ s₂ : AList β},
s₁.entries.Perm s₂.entries → (AList.erase a s₁).entries.Perm (AList.erase a s₂).entries
theorem AList.lookup_erase E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (a : α) (s : AList β), AList.lookup a (AList.erase a s) = none
theorem AList.lookup_erase_ne E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a a' : α} {s : AList β},
a ≠ a' → AList.lookup a (AList.erase a' s) = AList.lookup a s
theorem AList.erase_erase E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (a a' : α) (s : AList β),
AList.erase a' (AList.erase a s) = AList.erase a (AList.erase a' s)
def AList.insert E3 #
{α : Type u} → {β : α → Type v} → [DecidableEq α] → (a : α) → β a → AList β → AList β
Insert a key-value pair into an association list and erase any existing pair with the same key.
theorem AList.entries_insert E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} {s : AList β},
(AList.insert a b s).entries = ⟨a, b⟩ :: List.kerase a s.entries
theorem AList.entries_insert_of_notMem E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} {s : AList β},
a ∉ s → (AList.insert a b s).entries = ⟨a, b⟩ :: s.entries
theorem AList.insert_of_notMem E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} {s : AList β} (h : a ∉ s),
AList.insert a b s = { entries := ⟨a, b⟩ :: s.entries, nodupKeys := ⋯ }
theorem AList.insert_empty E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (a : α) (b : β a), AList.insert a b ∅ = AList.singleton a b
theorem AList.mem_insert E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a a' : α} {b' : β a'} (s : AList β),
a ∈ AList.insert a' b' s ↔ a = a' ∨ a ∈ s
theorem AList.keys_insert E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} (s : AList β),
(AList.insert a b s).keys = a :: s.keys.erase a
theorem AList.perm_insert E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} {s₁ s₂ : AList β},
s₁.entries.Perm s₂.entries → (AList.insert a b s₁).entries.Perm (AList.insert a b s₂).entries
theorem AList.lookup_insert E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} (s : AList β),
AList.lookup a (AList.insert a b s) = some b
theorem AList.lookup_insert_ne E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a a' : α} {b' : β a'} {s : AList β},
a ≠ a' → AList.lookup a (AList.insert a' b' s) = AList.lookup a s
theorem AList.lookup_insert_eq_none E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {l : AList β} {k k' : α} {v : β k},
AList.lookup k' (AList.insert k v l) = none ↔ k' ≠ k ∧ AList.lookup k' l = none
theorem AList.lookup_to_alist E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} (s : List (Sigma β)),
AList.lookup a s.toAList = List.dlookup a s
theorem AList.insert_insert E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b b' : β a} (s : AList β),
AList.insert a b' (AList.insert a b s) = AList.insert a b' s
theorem AList.insert_insert_of_ne E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a a' : α} {b : β a} {b' : β a'} (s : AList β),
a ≠ a' → (AList.insert a' b' (AList.insert a b s)).entries.Perm (AList.insert a b (AList.insert a' b' s)).entries
theorem AList.insert_singleton_eq E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b b' : β a},
AList.insert a b (AList.singleton a b') = AList.singleton a b
theorem AList.entries_toAList E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (xs : List (Sigma β)), xs.toAList.entries = xs.dedupKeys
theorem AList.toAList_cons E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (a : α) (b : β a) (xs : List (Sigma β)),
(⟨a, b⟩ :: xs).toAList = AList.insert a b xs.toAList
theorem AList.mk_cons_eq_insert E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (c : Sigma β) (l : List (Sigma β)) (h : (c :: l).NodupKeys),
{ entries := c :: l, nodupKeys := h } = AList.insert c.fst c.snd { entries := l, nodupKeys := ⋯ }
def AList.insertRec E1 #
{α : Type u} →
{β : α → Type v} →
[inst : DecidableEq α] →
{C : AList β → Sort u_1} →
C ∅ → ((a : α) → (b : β a) → (l : AList β) → a ∉ l → C l → C (AList.insert a b l)) → (l : AList β) → C l
Recursion on an AList, using insert. Use as induction l.
theorem AList.insertRec_empty E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {C : AList β → Sort u_1} (H0 : C ∅)
(IH : (a : α) → (b : β a) → (l : AList β) → a ∉ l → C l → C (AList.insert a b l)), AList.insertRec H0 IH ∅ = H0
theorem AList.insertRec_insert E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {C : AList β → Sort u_1} (H0 : C ∅)
(IH : (a : α) → (b : β a) → (l : AList β) → a ∉ l → C l → C (AList.insert a b l)) {c : Sigma β} {l : AList β}
(h : c.fst ∉ l), AList.insertRec H0 IH (AList.insert c.fst c.snd l) = IH c.fst c.snd l h (AList.insertRec H0 IH l)
theorem AList.insertRec_insert_mk E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {C : AList β → Sort u_1} (H0 : C ∅)
(IH : (a : α) → (b : β a) → (l : AList β) → a ∉ l → C l → C (AList.insert a b l)) {a : α} (b : β a) {l : AList β}
(h : a ∉ l), AList.insertRec H0 IH (AList.insert a b l) = IH a b l h (AList.insertRec H0 IH l)
def AList.extract E1 #
{α : Type u} → {β : α → Type v} → [DecidableEq α] → (a : α) → AList β → Option (β a) × AList β
Erase a key from the map, and return the corresponding value, if found.
theorem AList.extract_eq_lookup_erase E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (a : α) (s : AList β),
AList.extract a s = (AList.lookup a s, AList.erase a s)
def AList.union E3 #
{α : Type u} → {β : α → Type v} → [DecidableEq α] → AList β → AList β → AList β
s₁ ∪ s₂ is the key-based union of two association lists. It is
left-biased: if there exists an a ∈ s₁, lookup a (s₁ ∪ s₂) = lookup a s₁.
instance AList.instUnion E0 #
{α : Type u} → {β : α → Type v} → [DecidableEq α] → Union (AList β)
theorem AList.union_entries E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {s₁ s₂ : AList β},
(s₁ ∪ s₂).entries = s₁.entries.kunion s₂.entries
theorem AList.empty_union E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {s : AList β}, ∅ ∪ s = s
theorem AList.union_empty E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {s : AList β}, s ∪ ∅ = s
theorem AList.mem_union E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {s₁ s₂ : AList β}, a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂
theorem AList.perm_union E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {s₁ s₂ s₃ s₄ : AList β},
s₁.entries.Perm s₂.entries → s₃.entries.Perm s₄.entries → (s₁ ∪ s₃).entries.Perm (s₂ ∪ s₄).entries
theorem AList.union_erase E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] (a : α) (s₁ s₂ : AList β),
AList.erase a (s₁ ∪ s₂) = AList.erase a s₁ ∪ AList.erase a s₂
theorem AList.lookup_union_left E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {s₁ s₂ : AList β},
a ∈ s₁ → AList.lookup a (s₁ ∪ s₂) = AList.lookup a s₁
theorem AList.lookup_union_right E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {s₁ s₂ : AList β},
a ∉ s₁ → AList.lookup a (s₁ ∪ s₂) = AList.lookup a s₂
theorem AList.mem_lookup_union E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} {s₁ s₂ : AList β},
b ∈ AList.lookup a (s₁ ∪ s₂) ↔ b ∈ AList.lookup a s₁ ∨ a ∉ s₁ ∧ b ∈ AList.lookup a s₂
theorem AList.lookup_union_eq_some E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} {s₁ s₂ : AList β},
AList.lookup a (s₁ ∪ s₂) = some b ↔ AList.lookup a s₁ = some b ∨ a ∉ s₁ ∧ AList.lookup a s₂ = some b
theorem AList.mem_lookup_union_middle E0 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} {s₁ s₂ s₃ : AList β},
b ∈ AList.lookup a (s₁ ∪ s₃) → a ∉ s₂ → b ∈ AList.lookup a (s₁ ∪ s₂ ∪ s₃)
theorem AList.insert_union E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {a : α} {b : β a} {s₁ s₂ : AList β},
AList.insert a b (s₁ ∪ s₂) = AList.insert a b s₁ ∪ s₂
theorem AList.union_assoc E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {s₁ s₂ s₃ : AList β},
(s₁ ∪ s₂ ∪ s₃).entries.Perm (s₁ ∪ (s₂ ∪ s₃)).entries
def AList.Disjoint E1 #
{α : Type u} → {β : α → Type v} → AList β → AList β → Prop
Two associative lists are disjoint if they have no common keys.
theorem AList.union_comm_of_disjoint E1 #
∀ {α : Type u} {β : α → Type v} [inst : DecidableEq α] {s₁ s₂ : AList β},
s₁.Disjoint s₂ → (s₁ ∪ s₂).entries.Perm (s₂ ∪ s₁).entries