AssocList α β
is "the same as" List (α × β)
, but flattening the structure
leads to one fewer pointer indirection (in the current code generator).
It is mainly intended as a component of HashMap
, but it can also be used as a plain
key-value map.
- nil: {α : Type u} → {β : Type v} → Batteries.AssocList α β
An empty list
- cons: {α : Type u} → {β : Type v} → α → β → Batteries.AssocList α β → Batteries.AssocList α β
Add a
key, value
pair to the list
Instances For
Equations
- Batteries.instInhabitedAssocList = { default := Batteries.AssocList.nil }
O(n)
. Convert an AssocList α β
into the equivalent List (α × β)
.
This is used to give specifications for all the AssocList
functions
in terms of corresponding list functions.
Equations
- Batteries.AssocList.nil.toList = []
- (Batteries.AssocList.cons a b es).toList = (a, b) :: es.toList
Instances For
Equations
- Batteries.AssocList.instEmptyCollection = { emptyCollection := Batteries.AssocList.nil }
O(1)
. Is the list empty?
Instances For
The number of entries in an AssocList
.
Equations
- Batteries.AssocList.nil.length = 0
- (Batteries.AssocList.cons a b es).length = es.length + 1
Instances For
O(n)
. Fold a monadic function over the list, from head to tail.
Equations
- Batteries.AssocList.foldlM f x Batteries.AssocList.nil = pure x
- Batteries.AssocList.foldlM f x (Batteries.AssocList.cons a b es) = do let __do_lift ← f x a b Batteries.AssocList.foldlM f __do_lift es
Instances For
O(n)
. Fold a function over the list, from head to tail.
Equations
- Batteries.AssocList.foldl f init as = (Batteries.AssocList.foldlM f init as).run
Instances For
Optimized version of toList
.
Equations
- as.toListTR = (Batteries.AssocList.foldl (fun (r : Array (α × β)) (a : α) (b : β) => r.push (a, b)) #[] as).toList
Instances For
O(n)
. Run monadic function f
on all elements in the list, from head to tail.
Equations
- Batteries.AssocList.forM f Batteries.AssocList.nil = pure PUnit.unit
- Batteries.AssocList.forM f (Batteries.AssocList.cons a b es) = do f a b Batteries.AssocList.forM f es
Instances For
O(n)
. Map a function f
over the keys of the list.
Equations
- Batteries.AssocList.mapKey f Batteries.AssocList.nil = Batteries.AssocList.nil
- Batteries.AssocList.mapKey f (Batteries.AssocList.cons a b es) = Batteries.AssocList.cons (f a) b (Batteries.AssocList.mapKey f es)
Instances For
O(n)
. Map a function f
over the values of the list.
Equations
- Batteries.AssocList.mapVal f Batteries.AssocList.nil = Batteries.AssocList.nil
- Batteries.AssocList.mapVal f (Batteries.AssocList.cons a b es) = Batteries.AssocList.cons a (f a b) (Batteries.AssocList.mapVal f es)
Instances For
O(n)
. Returns the first entry in the list whose entry satisfies p
.
Equations
- Batteries.AssocList.findEntryP? p Batteries.AssocList.nil = none
- Batteries.AssocList.findEntryP? p (Batteries.AssocList.cons a b es) = bif p a b then some (a, b) else Batteries.AssocList.findEntryP? p es
Instances For
O(n)
. Returns the first entry in the list whose key is equal to a
.
Equations
- Batteries.AssocList.findEntry? a l = Batteries.AssocList.findEntryP? (fun (k : α) (x : β) => k == a) l
Instances For
O(n)
. Returns the first value in the list whose key is equal to a
.
Equations
- Batteries.AssocList.find? a Batteries.AssocList.nil = none
- Batteries.AssocList.find? a (Batteries.AssocList.cons a_1 b es) = match a_1 == a with | true => some b | false => Batteries.AssocList.find? a es
Instances For
O(n)
. Returns true if any entry in the list satisfies p
.
Equations
- Batteries.AssocList.any p Batteries.AssocList.nil = false
- Batteries.AssocList.any p (Batteries.AssocList.cons a b es) = (p a b || Batteries.AssocList.any p es)
Instances For
O(n)
. Returns true if every entry in the list satisfies p
.
Equations
- Batteries.AssocList.all p Batteries.AssocList.nil = true
- Batteries.AssocList.all p (Batteries.AssocList.cons a b es) = (p a b && Batteries.AssocList.all p es)
Instances For
Returns true if every entry in the list satisfies p
.
Equations
- Batteries.AssocList.All p l = ∀ (a : α × β), a ∈ l.toList → p a.fst a.snd
Instances For
O(n)
. Returns true if there is an element in the list whose key is equal to a
.
Equations
- Batteries.AssocList.contains a l = Batteries.AssocList.any (fun (k : α) (x : β) => k == a) l
Instances For
O(n)
. Replace the first entry in the list
with key equal to a
to have key a
and value b
.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.AssocList.replace a b Batteries.AssocList.nil = Batteries.AssocList.nil
Instances For
O(n)
. Remove the first entry in the list with key equal to a
.
Equations
- Batteries.AssocList.eraseP p Batteries.AssocList.nil = Batteries.AssocList.nil
- Batteries.AssocList.eraseP p (Batteries.AssocList.cons a b es) = bif p a b then es else Batteries.AssocList.cons a b (Batteries.AssocList.eraseP p es)
Instances For
O(n)
. Remove the first entry in the list with key equal to a
.
Equations
- Batteries.AssocList.erase a l = Batteries.AssocList.eraseP (fun (k : α) (x : β) => k == a) l
Instances For
O(n)
. Replace the first entry a', b
in the list
with key equal to a
to have key a
and value f a' b
.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.AssocList.modify a f Batteries.AssocList.nil = Batteries.AssocList.nil
Instances For
The implementation of ForIn
, which enables for (k, v) in aList do ...
notation.
Equations
- Batteries.AssocList.nil.forIn init f = pure init
- (Batteries.AssocList.cons a b es).forIn init f = do let __do_lift ← f (a, b) init match __do_lift with | ForInStep.done d => pure d | ForInStep.yield d => es.forIn d f
Instances For
Split the list into head and tail, if possible.
Equations
- x.pop? = match x with | Batteries.AssocList.nil => none | Batteries.AssocList.cons a b l => some ((a, b), l)
Instances For
Equations
- Batteries.AssocList.instToStream = { toStream := fun (x : Batteries.AssocList α β) => x }
Equations
- Batteries.AssocList.instStreamProd = { next? := Batteries.AssocList.pop? }
Converts a list into an AssocList
. This is the inverse function to AssocList.toList
.
Equations
- [].toAssocList = Batteries.AssocList.nil
- ((a, b) :: es).toAssocList = Batteries.AssocList.cons a b es.toAssocList
Instances For
Implementation of ==
on AssocList
.
Equations
- Batteries.AssocList.nil.beq Batteries.AssocList.nil = true
- (Batteries.AssocList.cons key value tail).beq Batteries.AssocList.nil = false
- Batteries.AssocList.nil.beq (Batteries.AssocList.cons key value tail) = false
- (Batteries.AssocList.cons a b t).beq (Batteries.AssocList.cons a' b' t') = (a == a' && b == b' && t.beq t')
Instances For
Boolean equality for AssocList
.
(This relation cares about the ordering of the key-value pairs.)
Equations
- Batteries.AssocList.instBEq = { beq := Batteries.AssocList.beq }