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 AList
s.
AList.keys
returns a list of keys of the alist.AList.membership
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 #
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.
- nodupKeys : List.NodupKeys self.entries
There are no duplicate keys in
entries
Instances For
Given l : List (Sigma β)
, create a term of type AList β
by removing
entries with duplicate keys.
Equations
- List.toAList l = { entries := List.dedupKeys l, nodupKeys := (_ : List.NodupKeys (List.dedupKeys l)) }
Instances For
keys #
mem #
The predicate a ∈ s
means that s
has a value associated to the key a
.
Equations
- AList.instMembershipAList = { mem := fun (a : α) (s : AList β) => a ∈ AList.keys s }
empty #
The empty association list.
Equations
- AList.instEmptyCollectionAList = { emptyCollection := { entries := [], nodupKeys := (_ : List.NodupKeys []) } }
singleton #
The singleton association list.
Equations
- AList.singleton a b = { entries := [{ fst := a, snd := b }], nodupKeys := (_ : List.NodupKeys [{ fst := a, snd := b }]) }
Instances For
lookup #
Look up the value associated to a key in an association list.
Equations
- AList.lookup a s = List.dlookup a s.entries
Instances For
Equations
- AList.instDecidableMemAListInstMembershipAList a s = decidable_of_iff (Option.isSome (AList.lookup a s) = true) (_ : Option.isSome (AList.lookup a s) = true ↔ a ∈ s)
replace #
Replace a key with a given value in an association list. If the key is not present it does nothing.
Equations
- AList.replace a b s = { entries := List.kreplace a b s.entries, nodupKeys := (_ : List.NodupKeys (List.kreplace a b s.entries)) }
Instances For
Fold a function over the key-value pairs in the map.
Equations
- AList.foldl f d m = List.foldl (fun (r : δ) (a : Sigma β) => f r a.fst a.snd) d m.entries
Instances For
erase #
Erase a key from the map. If the key is not present, do nothing.
Equations
- AList.erase a s = { entries := List.kerase a s.entries, nodupKeys := (_ : List.NodupKeys (List.kerase a s.entries)) }
Instances For
insert #
Insert a key-value pair into an association list and erase any existing pair with the same key.
Equations
- AList.insert a b s = { entries := List.kinsert a b s.entries, nodupKeys := (_ : List.NodupKeys (List.kinsert a b s.entries)) }
Instances For
Recursion on an AList
, using insert
. Use as induction l using AList.insertRec
.
Equations
- One or more equations did not get rendered due to their size.
- AList.insertRec H0 IH { entries := [], nodupKeys := nodupKeys } = H0
Instances For
extract #
Erase a key from the map, and return the corresponding value, if found.
Equations
- AList.extract a s = let_fun this := (_ : List.NodupKeys (List.kextract a s.entries).2); match List.kextract a s.entries, this with | (b, l), h => (b, { entries := l, nodupKeys := h })
Instances For
union #
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
- AList.union s₁ s₂ = { entries := List.kunion s₁.entries s₂.entries, nodupKeys := (_ : List.NodupKeys (List.kunion s₁.entries s₂.entries)) }
Instances For
Equations
- AList.instUnionAList = { union := AList.union }
disjoint #
Two associative lists are disjoint if they have no common keys.
Equations
- AList.Disjoint s₁ s₂ = ∀ k ∈ AList.keys s₁, k ∉ AList.keys s₂