data.list.alist

# 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.keys returns a list of keys of the alist.
• alist.has_mem returns membership in the set of keys.
• alist.erase removes a certain key.
• alist.insert adds a key-value mapping to the list.
• alist.union combines two association lists.

## References #

structure alist {α : 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.

Instances for alist
def list.to_alist {α : Type u} [decidable_eq α] {β : α → Type v} (l : list (sigma β)) :

Given l : list (sigma β), create a term of type alist β by removing entries with duplicate keys.

Equations
@[ext]
theorem alist.ext {α : Type u} {β : α → Type v} {s t : alist β} :
s.entries = t.entriess = t
theorem alist.ext_iff {α : Type u} {β : α → Type v} {s t : alist β} :
@[protected, instance]
def alist.decidable_eq {α : Type u} {β : α → Type v} [decidable_eq α] [Π (a : α), decidable_eq (β a)] :
Equations

### keys #

def alist.keys {α : Type u} {β : α → Type v} (s : alist β) :
list α

The list of keys of an association list.

Equations
theorem alist.keys_nodup {α : Type u} {β : α → Type v} (s : alist β) :

### mem #

@[protected, instance]
def alist.has_mem {α : Type u} {β : α → Type v} :
(alist β)

The predicate a ∈ s means that s has a value associated to the key a.

Equations
theorem alist.mem_keys {α : Type u} {β : α → Type v} {a : α} {s : alist β} :
a s a s.keys
theorem alist.mem_of_perm {α : Type u} {β : α → Type v} {a : α} {s₁ s₂ : alist β} (p : s₁.entries ~ s₂.entries) :
a s₁ a s₂

### empty #

@[protected, instance]
def alist.has_emptyc {α : Type u} {β : α → Type v} :

The empty association list.

Equations
@[protected, instance]
def alist.inhabited {α : Type u} {β : α → Type v} :
Equations
@[simp]
theorem alist.not_mem_empty {α : Type u} {β : α → Type v} (a : α) :
@[simp]
theorem alist.empty_entries {α : Type u} {β : α → Type v} :
@[simp]
theorem alist.keys_empty {α : Type u} {β : α → Type v} :

### singleton #

def alist.singleton {α : Type u} {β : α → Type v} (a : α) (b : β a) :

The singleton association list.

Equations
@[simp]
theorem alist.singleton_entries {α : Type u} {β : α → Type v} (a : α) (b : β a) :
b).entries = [a, b⟩]
@[simp]
theorem alist.keys_singleton {α : Type u} {β : α → Type v} (a : α) (b : β a) :
b).keys = [a]

### lookup #

def alist.lookup {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (s : alist β) :
option (β a)

Look up the value associated to a key in an association list.

Equations
• s =
@[simp]
theorem alist.lookup_empty {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) :
theorem alist.lookup_is_some {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {s : alist β} :
theorem alist.lookup_eq_none {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {s : alist β} :
a s
theorem alist.mem_lookup_iff {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b : β a} {s : alist β} :
b s a, b⟩ s.entries
theorem alist.perm_lookup {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {s₁ s₂ : alist β} (p : s₁.entries ~ s₂.entries) :
s₁ = s₂
@[protected, instance]
def alist.has_mem.mem.decidable {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (s : alist β) :
Equations

### replace #

def alist.replace {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (b : β a) (s : alist β) :

Replace a key with a given value in an association list. If the key is not present it does nothing.

Equations
@[simp]
theorem alist.keys_replace {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (b : β a) (s : alist β) :
b s).keys = s.keys
@[simp]
theorem alist.mem_replace {α : Type u} {β : α → Type v} [decidable_eq α] {a a' : α} {b : β a} {s : alist β} :
a' s a' s
theorem alist.perm_replace {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b : β a} {s₁ s₂ : alist β} :
s₁.entries ~ s₂.entries b s₁).entries ~ b s₂).entries
def alist.foldl {α : Type u} {β : α → Type v} {δ : Type w} (f : δ → Π (a : α), β a → δ) (d : δ) (m : alist β) :
δ

Fold a function over the key-value pairs in the map.

Equations

### erase #

def alist.erase {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (s : alist β) :

Erase a key from the map. If the key is not present, do nothing.

Equations
@[simp]
theorem alist.keys_erase {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (s : alist β) :
s).keys = s.keys.erase a
@[simp]
theorem alist.mem_erase {α : Type u} {β : α → Type v} [decidable_eq α] {a a' : α} {s : alist β} :
a' s a' a a' s
theorem alist.perm_erase {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {s₁ s₂ : alist β} :
s₁.entries ~ s₂.entries s₁).entries ~ s₂).entries
@[simp]
theorem alist.lookup_erase {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (s : alist β) :
@[simp]
theorem alist.lookup_erase_ne {α : Type u} {β : α → Type v} [decidable_eq α] {a a' : α} {s : alist β} (h : a a') :
(alist.erase a' s) = s
theorem alist.erase_erase {α : Type u} {β : α → Type v} [decidable_eq α] (a a' : α) (s : alist β) :
s) = (alist.erase a' s)

### insert #

def alist.insert {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (b : β a) (s : alist β) :

Insert a key-value pair into an association list and erase any existing pair with the same key.

Equations
@[simp]
theorem alist.insert_entries {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b : β a} {s : alist β} :
b s).entries = a, b⟩ ::
theorem alist.insert_entries_of_neg {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b : β a} {s : alist β} (h : a s) :
b s).entries = a, b⟩ :: s.entries
@[simp]
theorem alist.insert_empty {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (b : β a) :
b =
@[simp]
theorem alist.mem_insert {α : Type u} {β : α → Type v} [decidable_eq α] {a a' : α} {b' : β a'} (s : alist β) :
a b' s a = a' a s
@[simp]
theorem alist.keys_insert {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b : β a} (s : alist β) :
b s).keys = a :: s.keys.erase a
theorem alist.perm_insert {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b : β a} {s₁ s₂ : alist β} (p : s₁.entries ~ s₂.entries) :
b s₁).entries ~ b s₂).entries
@[simp]
theorem alist.lookup_insert {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b : β a} (s : alist β) :
b s) =
@[simp]
theorem alist.lookup_insert_ne {α : Type u} {β : α → Type v} [decidable_eq α] {a a' : α} {b' : β a'} {s : alist β} (h : a a') :
(alist.insert a' b' s) = s
@[simp]
theorem alist.lookup_to_alist {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} (s : list (sigma β)) :
= s
@[simp]
theorem alist.insert_insert {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b b' : β a} (s : alist β) :
b' b s) = b' s
theorem alist.insert_insert_of_ne {α : Type u} {β : α → Type v} [decidable_eq α] {a a' : α} {b : β a} {b' : β a'} (s : alist β) (h : a a') :
(alist.insert a' b' b s)).entries ~ b (alist.insert a' b' s)).entries
@[simp]
theorem alist.insert_singleton_eq {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b b' : β a} :
b b') =
@[simp]
theorem alist.entries_to_alist {α : Type u} {β : α → Type v} [decidable_eq α] (xs : list (sigma β)) :
theorem alist.to_alist_cons {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (b : β a) (xs : list (sigma β)) :
(a, b⟩ :: xs).to_alist = b xs.to_alist

### extract #

def alist.extract {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (s : alist β) :
option (β a) ×

Erase a key from the map, and return the corresponding value, if found.

Equations
@[simp]
theorem alist.extract_eq_lookup_erase {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (s : alist β) :
= s, s)

### union #

def alist.union {α : Type u} {β : α → Type v} [decidable_eq α] (s₁ s₂ : 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₁.

Equations
@[protected, instance]
def alist.has_union {α : Type u} {β : α → Type v} [decidable_eq α] :
Equations
@[simp]
theorem alist.union_entries {α : Type u} {β : α → Type v} [decidable_eq α] {s₁ s₂ : alist β} :
(s₁ s₂).entries = s₁.entries.kunion s₂.entries
@[simp]
theorem alist.empty_union {α : Type u} {β : α → Type v} [decidable_eq α] {s : alist β} :
s = s
@[simp]
theorem alist.union_empty {α : Type u} {β : α → Type v} [decidable_eq α] {s : alist β} :
s = s
@[simp]
theorem alist.mem_union {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {s₁ s₂ : alist β} :
a s₁ s₂ a s₁ a s₂
theorem alist.perm_union {α : Type u} {β : α → Type v} [decidable_eq α] {s₁ s₂ s₃ s₄ : alist β} (p₁₂ : s₁.entries ~ s₂.entries) (p₃₄ : s₃.entries ~ s₄.entries) :
(s₁ s₃).entries ~ (s₂ s₄).entries
theorem alist.union_erase {α : Type u} {β : α → Type v} [decidable_eq α] (a : α) (s₁ s₂ : alist β) :
(s₁ s₂) = s₁ s₂
@[simp]
theorem alist.lookup_union_left {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {s₁ s₂ : alist β} :
a s₁ (s₁ s₂) = s₁
@[simp]
theorem alist.lookup_union_right {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {s₁ s₂ : alist β} :
a s₁ (s₁ s₂) = s₂
@[simp]
theorem alist.mem_lookup_union {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b : β a} {s₁ s₂ : alist β} :
b (s₁ s₂) b s₁ a s₁ b s₂
theorem alist.mem_lookup_union_middle {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b : β a} {s₁ s₂ s₃ : alist β} :
b (s₁ s₃)a s₂b (s₁ s₂ s₃)
theorem alist.insert_union {α : Type u} {β : α → Type v} [decidable_eq α] {a : α} {b : β a} {s₁ s₂ : alist β} :
b (s₁ s₂) = b s₁ s₂
theorem alist.union_assoc {α : Type u} {β : α → Type v} [decidable_eq α] {s₁ s₂ s₃ : alist β} :
(s₁ s₂ s₃).entries ~ (s₁ (s₂ s₃)).entries

### disjoint #

def alist.disjoint {α : Type u} {β : α → Type v} (s₁ s₂ : alist β) :
Prop

Two associative lists are disjoint if they have no common keys.

Equations
theorem alist.union_comm_of_disjoint {α : Type u} {β : α → Type v} [decidable_eq α] {s₁ s₂ : alist β} (h : s₁.disjoint s₂) :
(s₁ s₂).entries ~ (s₂ s₁).entries