mathlib3 documentation

data.list.alist

Association Lists #

THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.

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.

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 β} :
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} :
has_mem α (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) :
@[simp]
theorem alist.keys_singleton {α : Type u} {β : α Type v} (a : α) (b : β 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
@[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 β} :
theorem alist.mem_lookup_iff {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {b : β a} {s : alist β} :
theorem alist.perm_lookup {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {s₁ s₂ : alist β} (p : s₁.entries ~ s₂.entries) :
@[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 β) :
@[simp]
theorem alist.mem_replace {α : Type u} {β : α Type v} [decidable_eq α] {a a' : α} {b : β a} {s : alist β} :
a' alist.replace a b s a' s
theorem alist.perm_replace {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {b : β a} {s₁ s₂ : alist β} :
s₁.entries ~ s₂.entries (alist.replace a b s₁).entries ~ (alist.replace a 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 β) :
@[simp]
theorem alist.mem_erase {α : Type u} {β : α Type v} [decidable_eq α] {a a' : α} {s : alist β} :
a' alist.erase a s a' a a' s
theorem alist.perm_erase {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {s₁ s₂ : alist β} :
@[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') :
theorem alist.erase_erase {α : Type u} {β : α Type v} [decidable_eq α] (a a' : α) (s : alist β) :

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 β} :
theorem alist.insert_entries_of_neg {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {b : β a} {s : alist β} (h : a s) :
(alist.insert a b s).entries = a, b⟩ :: s.entries
theorem alist.insert_of_neg {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {b : β a} {s : alist β} (h : a s) :
alist.insert a b s = {entries := a, b⟩ :: s.entries, nodupkeys := _}
@[simp]
theorem alist.insert_empty {α : Type u} {β : α Type v} [decidable_eq α] (a : α) (b : β a) :
@[simp]
theorem alist.mem_insert {α : Type u} {β : α Type v} [decidable_eq α] {a a' : α} {b' : β a'} (s : alist β) :
a alist.insert a' b' s a = a' a s
@[simp]
theorem alist.keys_insert {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {b : β a} (s : alist β) :
(alist.insert a 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) :
(alist.insert a b s₁).entries ~ (alist.insert a b s₂).entries
@[simp]
theorem alist.lookup_insert {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {b : β a} (s : alist β) :
@[simp]
theorem alist.lookup_insert_ne {α : Type u} {β : α Type v} [decidable_eq α] {a a' : α} {b' : β a'} {s : alist β} (h : a a') :
@[simp]
theorem alist.lookup_to_alist {α : Type u} {β : α Type v} [decidable_eq α] {a : α} (s : list (sigma β)) :
@[simp]
theorem alist.insert_insert {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {b b' : β a} (s : alist β) :
theorem alist.insert_insert_of_ne {α : Type u} {β : α Type v} [decidable_eq α] {a a' : α} {b : β a} {b' : β a'} (s : alist β) (h : a a') :
@[simp]
theorem alist.insert_singleton_eq {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {b b' : β a} :
@[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 = alist.insert a b xs.to_alist
theorem alist.mk_cons_eq_insert {α : Type u} {β : α Type v} [decidable_eq α] (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.insert_rec {α : Type u} {β : α Type v} [decidable_eq α] {C : alist β Sort u_1} (H0 : C ) (IH : Π (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 using alist.insert_rec.

Equations
@[simp]
theorem alist.insert_rec_empty {α : Type u} {β : α Type v} [decidable_eq α] {C : alist β Sort u_1} (H0 : C ) (IH : Π (a : α) (b : β a) (l : alist β), a l C l C (alist.insert a b l)) :
theorem alist.insert_rec_insert {α : Type u} {β : α Type v} [decidable_eq α] {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.insert_rec H0 IH (alist.insert c.fst c.snd l) = IH c.fst c.snd l h (alist.insert_rec H0 IH l)
theorem alist.recursion_insert_mk {α : Type u} {β : α Type v} [decidable_eq α] {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.insert_rec H0 IH (alist.insert a b l) = IH a b l h (alist.insert_rec H0 IH l)

extract #

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

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 β) :

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 β) :
alist.erase a (s₁ s₂) = alist.erase a s₁ alist.erase a s₂
@[simp]
theorem alist.lookup_union_left {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {s₁ s₂ : alist β} :
a s₁ alist.lookup a (s₁ s₂) = alist.lookup a s₁
@[simp]
theorem alist.lookup_union_right {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {s₁ s₂ : alist β} :
a s₁ alist.lookup a (s₁ s₂) = alist.lookup a s₂
@[simp]
theorem alist.mem_lookup_union {α : Type u} {β : α Type v} [decidable_eq α] {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.mem_lookup_union_middle {α : Type u} {β : α Type v} [decidable_eq α] {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 {α : Type u} {β : α Type v} [decidable_eq α] {a : α} {b : β a} {s₁ s₂ : alist β} :
alist.insert a b (s₁ s₂) = alist.insert a 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