Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This module defines a way to prove that a key exists in a map, so that the
key can be used to index into the map without using a Maybe
, or manually
handling the "impossible" case with error
or other partial functions.
To do this,
has a type parameter HashMap
s k vs
that identifies its
set of keys, so that if another map has the same type parameter, you know
that map has the same set of keys. There is
, a type of keys that
have been validated to belong to the set identified by Key
s ks
, and for which the
operation of indexing into a
(only for the same HashMap
s k vs
) can
proceed without failure (see !
). The type s
itself has no internal
structure, rather it is merely a skolem type variable (rank-2 polymorphism
runST
trick) introduced by Data.Reflection.
Like Data.HashMap.Lazy, functions in this module are strict in the keys but
lazy in the values. The Data.HashMap.Strict.Refined module reuses the same
HashMap
type but provides functions that operate strictly on the values.
Warning
This module together with Data.HashMap.Lazy rely on Eq
and Hashable
instances being lawful: that ==
is an equivalence relation, and that
hashWithSalt
is defined on the quotient by this equivalence
relation; at least for the subset of values that are actually encountered at
runtime. If this assumption is violated, this module may not be able to
uphold its invariants and may throw errors. In particular beware of NaN in
Float
and Double
, and, if using hashable < 1.3
, beware of 0
and -0
.
Synopsis
- data HashMap s k a
- type Key s = Refined (InSet 'Hashed s)
- data SomeHashMap k a where
- SomeHashMap :: forall s k a. !(HashMap s k a) -> SomeHashMap k a
- withHashMap :: forall k a r. SomeHashMap k a -> (forall s. HashMap s k a -> r) -> r
- data SomeHashMapWith p k a where
- SomeHashMapWith :: forall s k a p. !(HashMap s k a) -> !(p s) -> SomeHashMapWith p k a
- withHashMapWith :: forall k a r p. SomeHashMapWith p k a -> (forall s. HashMap s k a -> p s -> r) -> r
- data Some2HashMapWith p k a b where
- Some2HashMapWith :: forall s t k a b p. !(HashMap s k a) -> !(HashMap t k b) -> !(p s t) -> Some2HashMapWith p k a b
- with2HashMapWith :: forall k a b r p. Some2HashMapWith p k a b -> (forall s t. HashMap s k a -> HashMap t k b -> p s t -> r) -> r
- newtype SupersetProof f s r = SupersetProof (InSet f r :-> InSet f s)
- newtype EmptyProof f r = EmptyProof (forall s. InSet f r :-> InSet f s)
- empty :: forall k a. SomeHashMapWith (EmptyProof 'Hashed) k a
- singleton :: forall k a. Hashable k => k -> a -> SomeHashMapWith (SingletonProof 'Hashed k) k a
- newtype SingletonProof f a r = SingletonProof (Refined (InSet f r) a)
- fromSet :: forall s k a. KnownHashSet s k => (Key s k -> a) -> HashMap s k a
- fromHashMap :: forall k a. HashMap k a -> SomeHashMap k a
- fromTraversableWithKey :: forall t k a. (Traversable t, Hashable k) => (k -> a -> a -> a) -> t (k, a) -> SomeHashMapWith (FromTraversableProof 'Hashed t k) k a
- newtype FromTraversableProof f (t :: Type -> Type) a r = FromTraversableProof (t (Refined (InSet f r) a))
- insert :: forall s k a. Hashable k => k -> a -> HashMap s k a -> SomeHashMapWith (InsertProof 'Hashed k s) k a
- data InsertProof f a s r = InsertProof (Refined (InSet f r) a) (InSet f s :-> InSet f r)
- reinsert :: forall s k a. Hashable k => Key s k -> a -> HashMap s k a -> HashMap s k a
- insertLookupWithKey :: forall s k a. Hashable k => (Key s k -> a -> a -> a) -> k -> a -> HashMap s k a -> (Maybe (Key s k, a), SomeHashMapWith (InsertProof 'Hashed k s) k a)
- delete :: forall s k a. Hashable k => k -> HashMap s k a -> SomeHashMapWith (SupersetProof 'Hashed s) k a
- adjust :: forall s k a. Hashable k => (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
- adjustWithKey :: forall s k a. Hashable k => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a
- update :: forall s k a. Hashable k => (a -> Maybe a) -> Key s k -> HashMap s k a -> SomeHashMapWith (SupersetProof 'Hashed s) k a
- updateLookupWithKey :: forall s k a. Hashable k => (Key s k -> a -> Maybe a) -> k -> HashMap s k a -> (Maybe (Key s k, a), SomeHashMapWith (SupersetProof 'Hashed s) k a)
- lookup :: forall s k a. Hashable k => k -> HashMap s k a -> Maybe (Key s k, a)
- (!) :: forall s k a. Hashable k => HashMap s k a -> Key s k -> a
- member :: forall s k a. Hashable k => k -> HashMap s k a -> Maybe (Key s k)
- null :: forall s k a. HashMap s k a -> Maybe (EmptyProof 'Hashed s)
- isSubmapOfBy :: forall s t k a b. Hashable k => (a -> b -> Bool) -> HashMap s k a -> HashMap t k b -> Maybe (SubsetProof 'Hashed s t)
- newtype SubsetProof f s r = SubsetProof (InSet f s :-> InSet f r)
- disjoint :: forall s t k a b. Hashable k => HashMap s k a -> HashMap t k b -> Maybe (DisjointProof 'Hashed s t)
- newtype DisjointProof f s r = DisjointProof (forall t. (InSet f t :-> InSet f s) -> (InSet f t :-> InSet f r) -> forall u. InSet f t :-> InSet f u)
- zipWithKey :: forall s k a b c. Hashable k => (Key s k -> a -> b -> c) -> HashMap s k a -> HashMap s k b -> HashMap s k c
- bind :: forall s k a b. Hashable k => HashMap s k a -> (a -> HashMap s k b) -> HashMap s k b
- unionWithKey :: forall s t k a. Hashable k => (Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> a -> a) -> HashMap s k a -> HashMap t k a -> SomeHashMapWith (UnionProof 'Hashed s t) k a
- data UnionProof f s t r = UnionProof ((InSet f s || InSet f t) :-> InSet f r) (forall u. (InSet f s :-> InSet f u) -> (InSet f t :-> InSet f u) -> InSet f r :-> InSet f u)
- difference :: forall s t k a b. Hashable k => HashMap s k a -> HashMap t k b -> SomeHashMapWith (DifferenceProof 'Hashed s t) k a
- data DifferenceProof f s t r = DifferenceProof (InSet f r :-> InSet f s) (forall u. (InSet f u :-> InSet f r) -> (InSet f u :-> InSet f t) -> forall v. InSet f u :-> InSet f v) (InSet f s :-> (InSet f t || InSet f r))
- differenceWithKey :: forall s t k a b. Hashable k => (Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> b -> Maybe a) -> HashMap s k a -> HashMap t k b -> SomeHashMapWith (PartialDifferenceProof 'Hashed s t) k a
- data PartialDifferenceProof f s t r = PartialDifferenceProof (InSet f r :-> InSet f s) (InSet f s :-> (InSet f t || InSet f r))
- intersectionWithKey :: forall s t k a b c. Hashable k => (Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> b -> c) -> HashMap s k a -> HashMap t k b -> SomeHashMapWith (IntersectionProof 'Hashed s t) k c
- data IntersectionProof f s t r = IntersectionProof (InSet f r :-> (InSet f s && InSet f t)) (forall u. (InSet f u :-> InSet f s) -> (InSet f u :-> InSet f t) -> InSet f u :-> InSet f r)
- mapWithKey :: forall s k a b. (Key s k -> a -> b) -> HashMap s k a -> HashMap s k b
- traverseWithKey :: forall s f k a b. Applicative f => (Key s k -> a -> f b) -> HashMap s k a -> f (HashMap s k b)
- mapAccumLWithKey :: forall s k a b c. (a -> Key s k -> b -> (a, c)) -> a -> HashMap s k b -> (a, HashMap s k c)
- mapAccumRWithKey :: forall s k a b c. (a -> Key s k -> b -> (a, c)) -> a -> HashMap s k b -> (a, HashMap s k c)
- mapKeysWith :: forall s k1 k2 a. Hashable k2 => (a -> a -> a) -> (Key s k1 -> k2) -> HashMap s k1 a -> SomeHashMapWith (MapProof 'Hashed s k1 k2) k2 a
- data MapProof f s a b r = MapProof (Refined (InSet f s) a -> Refined (InSet f r) b) (Refined (InSet f r) b -> Refined (InSet f s) a)
- backpermuteKeys :: forall s1 s2 k1 k2 a. (Hashable k1, KnownHashSet s2 k2) => (Key s2 k2 -> Key s1 k1) -> HashMap s1 k1 a -> HashMap s2 k2 a
- foldMapWithKey :: forall s k a m. Monoid m => (Key s k -> a -> m) -> HashMap s k a -> m
- foldrWithKey :: forall s k a b. (Key s k -> a -> b -> b) -> b -> HashMap s k a -> b
- foldlWithKey :: forall s k a b. (b -> Key s k -> a -> b) -> b -> HashMap s k a -> b
- foldrWithKey' :: forall s k a b. (Key s k -> a -> b -> b) -> b -> HashMap s k a -> b
- foldlWithKey' :: forall s k a b. (b -> Key s k -> a -> b) -> b -> HashMap s k a -> b
- toMap :: forall s k a. HashMap s k a -> HashMap k a
- keysSet :: forall s k a. HashMap s k a -> HashSet s k
- toList :: forall s k a. HashMap s k a -> [(Key s k, a)]
- restrictKeys :: forall s t k a. (Hashable k, KnownHashSet t k) => HashMap s k a -> SomeHashMapWith (IntersectionProof 'Hashed s t) k a
- withoutKeys :: forall s t k a. (Hashable k, KnownHashSet t k) => HashMap s k a -> SomeHashMapWith (DifferenceProof 'Hashed s t) k a
- filterWithKey :: forall s k a. (Key s k -> a -> Bool) -> HashMap s k a -> SomeHashMapWith (SupersetProof 'Hashed s) k a
- partitionWithKey :: forall s k a. Hashable k => (Key s k -> a -> Bool) -> HashMap s k a -> Some2HashMapWith (PartitionProof 'Hashed s k) k a a
- data PartitionProof f s a r q = PartitionProof (Refined (InSet f s) a -> Either (Refined (InSet f r) a) (Refined (InSet f q) a)) ((InSet f r || InSet f q) :-> InSet f s) (forall t. (InSet f r :-> InSet f t) -> (InSet f q :-> InSet f t) -> InSet f s :-> InSet f t) (forall t. (InSet f t :-> InSet f r) -> (InSet f t :-> InSet f q) -> forall u. InSet f t :-> InSet f u)
- mapMaybeWithKey :: forall s k a b. (Key s k -> a -> Maybe b) -> HashMap s k a -> SomeHashMapWith (SupersetProof 'Hashed s) k b
- mapEitherWithKey :: forall s k a b c. Hashable k => (Key s k -> a -> Either b c) -> HashMap s k a -> Some2HashMapWith (PartitionProof 'Hashed s k) k b c
- castKey :: forall s t k. (forall x. Key s x -> Key t x) -> (forall x. Key t x -> Key s x) -> Coercion (Key s k) (Key t k)
- cast :: forall s t k. (forall x. Coercion (Key s x) (Key t x)) -> Coercion (HashMap s k) (HashMap t k)
- castFlavor :: forall (f :: Flavor) (g :: Flavor) s a. Coercion (Refined (InSet f s) a) (Refined (InSet g s) a)
Map type
A wrapper around a regular HashMap
with a type parameter s
identifying the set of keys present in the map.
A key of type k
may not be present in the map, but a
is
guaranteed to be present (if the Key
s ks
parameters match). Thus the map is
isomorphic to a (total) function
, which motivates many of
the instances below.Key
s k -> a
A HashMap
always knows its set of keys, so given
we can
always derive HashMap
s k a
by pattern matching on the KnownHashSet
s kDict
returned
by keysSet
.
Instances
(Hashable k, KnownHashSet s k) => Representable (HashMap s k) Source # | Witness isomorphism with functions from |
Foldable (HashMap s k) Source # | |
Defined in Data.HashMap.Common.Refined fold :: Monoid m => HashMap s k m -> m # foldMap :: Monoid m => (a -> m) -> HashMap s k a -> m # foldMap' :: Monoid m => (a -> m) -> HashMap s k a -> m # foldr :: (a -> b -> b) -> b -> HashMap s k a -> b # foldr' :: (a -> b -> b) -> b -> HashMap s k a -> b # foldl :: (b -> a -> b) -> b -> HashMap s k a -> b # foldl' :: (b -> a -> b) -> b -> HashMap s k a -> b # foldr1 :: (a -> a -> a) -> HashMap s k a -> a # foldl1 :: (a -> a -> a) -> HashMap s k a -> a # toList :: HashMap s k a -> [a] # null :: HashMap s k a -> Bool # length :: HashMap s k a -> Int # elem :: Eq a => a -> HashMap s k a -> Bool # maximum :: Ord a => HashMap s k a -> a # minimum :: Ord a => HashMap s k a -> a # | |
Traversable (HashMap s k) Source # | |
Defined in Data.HashMap.Common.Refined | |
(Hashable k, KnownHashSet s k) => Applicative (HashMap s k) Source # | Similar to the instance for functions -- zip corresponding keys. To use
|
Defined in Data.HashMap.Common.Refined | |
Functor (HashMap s k) Source # | |
(Hashable k, KnownHashSet s k) => Monad (HashMap s k) Source # | Similar to the instance for functions. To use |
(Hashable k, KnownHashSet s k) => Distributive (HashMap s k) Source # | Similar to the instance for functions |
Defined in Data.HashMap.Common.Refined | |
FoldableWithIndex (Key s k) (HashMap s k) Source # | |
Defined in Data.HashMap.Common.Refined ifoldMap :: Monoid m => (Key s k -> a -> m) -> HashMap s k a -> m # ifoldMap' :: Monoid m => (Key s k -> a -> m) -> HashMap s k a -> m # ifoldr :: (Key s k -> a -> b -> b) -> b -> HashMap s k a -> b # ifoldl :: (Key s k -> b -> a -> b) -> b -> HashMap s k a -> b # ifoldr' :: (Key s k -> a -> b -> b) -> b -> HashMap s k a -> b # ifoldl' :: (Key s k -> b -> a -> b) -> b -> HashMap s k a -> b # | |
FunctorWithIndex (Key s k) (HashMap s k) Source # | |
TraversableWithIndex (Key s k) (HashMap s k) Source # | |
Defined in Data.HashMap.Common.Refined | |
(Hashable k, KnownHashSet s k) => MonadReader (Key s k) (HashMap s k) Source # | Similar to the instance for functions. See also
|
(Hashable k, KnownHashSet s k, Monoid a) => Monoid (HashMap s k a) Source # | |
(Hashable k, Semigroup a) => Semigroup (HashMap s k a) Source # | Append the values at the corresponding keys |
(Show k, Show a) => Show (HashMap s k a) Source # | |
(NFData k, NFData a) => NFData (HashMap s k a) Source # | |
Defined in Data.HashMap.Common.Refined | |
(Eq k, Eq a) => Eq (HashMap s k a) Source # | |
(Ord k, Ord a) => Ord (HashMap s k a) Source # | |
Defined in Data.HashMap.Common.Refined compare :: HashMap s k a -> HashMap s k a -> Ordering # (<) :: HashMap s k a -> HashMap s k a -> Bool # (<=) :: HashMap s k a -> HashMap s k a -> Bool # (>) :: HashMap s k a -> HashMap s k a -> Bool # (>=) :: HashMap s k a -> HashMap s k a -> Bool # | |
(Hashable k, Hashable a) => Hashable (HashMap s k a) Source # | |
Defined in Data.HashMap.Common.Refined | |
type Rep (HashMap s k) Source # | |
Defined in Data.HashMap.Common.Refined |
type Key s = Refined (InSet 'Hashed s) Source #
is a key of type Key
s kk
that has been verified to be an element
of the set s
, and thus verified to be present in all
maps.HashMap
s k
Thus,
is a "refinement" type of Key
s kk
, and this library
integrates with an implementation of refimenement types in Refined, so
the machinery from there can be used to manipulate Key
s (however see
revealPredicate
).
The underlying k
value can be obtained with unrefine
. A k
can be
validated into an
with Key
s kmember
.
Existentials and common proofs
data SomeHashMap k a where Source #
An existential wrapper for a HashMap
with an as-yet-unknown set of keys.
Pattern maching on it gives you a way to refer to the set (the parameter
s
), e.g.
casefromHashMap
... ofSomeHashMap
@s m -> doSomethingWith @s casefromHashMap
... ofSomeHashMap
(m ::HashMap
s k a) -> doSomethingWith @s
SomeHashMap :: forall s k a. !(HashMap s k a) -> SomeHashMap k a |
withHashMap :: forall k a r. SomeHashMap k a -> (forall s. HashMap s k a -> r) -> r Source #
Apply a map with an unknown set of keys to a continuation that can accept
a map with any set of keys. This gives you a way to refer to the set (the
parameter s
), e.g.:
withHashMap
(fromHashMap
... $ (m ::HashMap
s k a) -> doSomethingWith @s
data SomeHashMapWith p k a where Source #
An existential wrapper for a HashMap
with an as-yet-unknown set of keys,
together with a proof of some fact p
about the set. Pattern matching on it
gives you a way to refer to the set (the parameter s
). Functions that
change the set of keys in a map will return the map in this way, together
with a proof that somehow relates the keys set to the function's inputs.
SomeHashMapWith :: forall s k a p. !(HashMap s k a) -> !(p s) -> SomeHashMapWith p k a |
withHashMapWith :: forall k a r p. SomeHashMapWith p k a -> (forall s. HashMap s k a -> p s -> r) -> r Source #
Apply a map with proof for an unknown set of keys to a continuation that
can accept a map with any set of keys satisfying the proof. This gives you a
way to refer to the set (the parameter s
).
data Some2HashMapWith p k a b where Source #
An existential wrapper for a pair of maps with as-yet-unknown sets of keys,
together with a proof of some fact p
relating them.
Some2HashMapWith :: forall s t k a b p. !(HashMap s k a) -> !(HashMap t k b) -> !(p s t) -> Some2HashMapWith p k a b |
with2HashMapWith :: forall k a b r p. Some2HashMapWith p k a b -> (forall s t. HashMap s k a -> HashMap t k b -> p s t -> r) -> r Source #
Apply a pair of maps with proof for unknown sets of keys to a continuation
that can accept any pair of maps with any sets of keys satisfying the proof.
This gives you a way to refer to the sets (the parameters s
and t
).
newtype SupersetProof f s r Source #
Proof that s
is a superset of the set r
.
SupersetProof (InSet f r :-> InSet f s) | \(r \subseteq s\) |
newtype EmptyProof f r Source #
Proof that the set r
is empty.
EmptyProof (forall s. InSet f r :-> InSet f s) | \(\forall s, r \subseteq s\), which is equivalent to \(r \subseteq \varnothing\) |
Construction
empty :: forall k a. SomeHashMapWith (EmptyProof 'Hashed) k a Source #
An empty map.
singleton :: forall k a. Hashable k => k -> a -> SomeHashMapWith (SingletonProof 'Hashed k) k a Source #
Create a map with a single key-value pair, and return a proof that the key is in the resulting map.
newtype SingletonProof f a r Source #
Proof that r
contains an element of type a
.
SingletonProof (Refined (InSet f r) a) | The element that is guaranteed to be in |
fromSet :: forall s k a. KnownHashSet s k => (Key s k -> a) -> HashMap s k a Source #
Create a map from a set of keys, and a function that for each key computes the corresponding value.
fromHashMap :: forall k a. HashMap k a -> SomeHashMap k a Source #
Construct a map from a regular HashMap
.
fromTraversableWithKey :: forall t k a. (Traversable t, Hashable k) => (k -> a -> a -> a) -> t (k, a) -> SomeHashMapWith (FromTraversableProof 'Hashed t k) k a Source #
Create a map from an arbitrary traversable of key-value pairs.
newtype FromTraversableProof f (t :: Type -> Type) a r Source #
Proof that elements of t a
are contained in r
.
FromTraversableProof (t (Refined (InSet f r) a)) | The original traversable, with all elements refined with a guarantee of
being in |
Insertion
insert :: forall s k a. Hashable k => k -> a -> HashMap s k a -> SomeHashMapWith (InsertProof 'Hashed k s) k a Source #
Insert a key-value pair into the map to obtain a potentially larger map, guaranteed to contain the given key. If the key was already present, the associated value is replaced with the supplied value.
data InsertProof f a s r Source #
Proof that r
is s
with a
inserted.
reinsert :: forall s k a. Hashable k => Key s k -> a -> HashMap s k a -> HashMap s k a Source #
Overwrite a key-value pair that is known to already be in the map. The set of keys remains the same.
insertLookupWithKey :: forall s k a. Hashable k => (Key s k -> a -> a -> a) -> k -> a -> HashMap s k a -> (Maybe (Key s k, a), SomeHashMapWith (InsertProof 'Hashed k s) k a) Source #
Insert a key-value pair into the map using a combining function, and if the key was already present, the old value is returned along with the proof that the key was present.
Deletion/Update
delete :: forall s k a. Hashable k => k -> HashMap s k a -> SomeHashMapWith (SupersetProof 'Hashed s) k a Source #
Delete a key and its value from the map if present, returning a potentially smaller map.
adjust :: forall s k a. Hashable k => (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a Source #
Update the value at a specific key known the be in the map using the given function. The set of keys remains the same.
adjustWithKey :: forall s k a. Hashable k => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a Source #
If the given key is in the map, update the associated value using the given function with a proof that the key was in the map; otherwise return the map unchanged. In any case the set of keys remains the same.
update :: forall s k a. Hashable k => (a -> Maybe a) -> Key s k -> HashMap s k a -> SomeHashMapWith (SupersetProof 'Hashed s) k a Source #
Update or delete a key known to be in the map using the given function, returning a potentially smaller map.
updateLookupWithKey :: forall s k a. Hashable k => (Key s k -> a -> Maybe a) -> k -> HashMap s k a -> (Maybe (Key s k, a), SomeHashMapWith (SupersetProof 'Hashed s) k a) Source #
If the given key is in the map, update or delete it using the given function with a proof that the key was in the map; otherwise the map is unchanged. Alongside return the new value if it was updated, or the old value if it was deleted, and a proof that the key was in the map.
Query
lookup :: forall s k a. Hashable k => k -> HashMap s k a -> Maybe (Key s k, a) Source #
If the key is in the map, return the proof of this, and the associated
value; otherwise return Nothing
.
(!) :: forall s k a. Hashable k => HashMap s k a -> Key s k -> a Source #
Given a key that is proven to be in the map, return the associated value.
Unlike !
from Data.HashMap.Lazy, this function is total, as
it is impossible to obtain a
for a key that is not in the map
Key
s k
.HashMap
s k a
member :: forall s k a. Hashable k => k -> HashMap s k a -> Maybe (Key s k) Source #
If a key is in the map, return the proof that it is.
null :: forall s k a. HashMap s k a -> Maybe (EmptyProof 'Hashed s) Source #
If a map is empty, return a proof that it is.
isSubmapOfBy :: forall s t k a b. Hashable k => (a -> b -> Bool) -> HashMap s k a -> HashMap t k b -> Maybe (SubsetProof 'Hashed s t) Source #
If all keys of the first map are also present in the second map, and the
given function returns True
for their associated values, return a proof
that the keys form a subset.
newtype SubsetProof f s r Source #
Proof that s
is a subset of the set r
.
SubsetProof (InSet f s :-> InSet f r) | \(s \subseteq r\) |
disjoint :: forall s t k a b. Hashable k => HashMap s k a -> HashMap t k b -> Maybe (DisjointProof 'Hashed s t) Source #
If two maps are disjoint (i.e. their intersection is empty), return a proof of that.
newtype DisjointProof f s r Source #
Proof that s
and r
are disjoint.
Combine
zipWithKey :: forall s k a b c. Hashable k => (Key s k -> a -> b -> c) -> HashMap s k a -> HashMap s k b -> HashMap s k c Source #
Given two maps proven to have the same keys, for each key apply the function to the associated values, to obtain a new map with the same keys.
bind :: forall s k a b. Hashable k => HashMap s k a -> (a -> HashMap s k b) -> HashMap s k b Source #
unionWithKey :: forall s t k a. Hashable k => (Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> a -> a) -> HashMap s k a -> HashMap t k a -> SomeHashMapWith (UnionProof 'Hashed s t) k a Source #
data UnionProof f s t r Source #
Proof that unioning s
and t
gives r
.
difference :: forall s t k a b. Hashable k => HashMap s k a -> HashMap t k b -> SomeHashMapWith (DifferenceProof 'Hashed s t) k a Source #
Remove the keys that appear in the second map from the first map.
data DifferenceProof f s t r Source #
Proof that if from s
you subtract t
, then you get r
.
DifferenceProof | |
|
differenceWithKey :: forall s t k a b. Hashable k => (Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> b -> Maybe a) -> HashMap s k a -> HashMap t k b -> SomeHashMapWith (PartialDifferenceProof 'Hashed s t) k a Source #
data PartialDifferenceProof f s t r Source #
Proof that r
is obtained by removing some of t
's elements from s
.
intersectionWithKey :: forall s t k a b c. Hashable k => (Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> b -> c) -> HashMap s k a -> HashMap t k b -> SomeHashMapWith (IntersectionProof 'Hashed s t) k c Source #
data IntersectionProof f s t r Source #
Proof that intersecting s
and t
gives r
.
Traversal
mapWithKey :: forall s k a b. (Key s k -> a -> b) -> HashMap s k a -> HashMap s k b Source #
Apply a function to all values in a map, together with their corresponding keys, that are proven to be in the map. The set of keys remains the same.
traverseWithKey :: forall s f k a b. Applicative f => (Key s k -> a -> f b) -> HashMap s k a -> f (HashMap s k b) Source #
Map an Applicative
transformation with access to each value's
corresponding key and a proof that it is in the map. The set of keys remains
unchanged.
mapAccumLWithKey :: forall s k a b c. (a -> Key s k -> b -> (a, c)) -> a -> HashMap s k b -> (a, HashMap s k c) Source #
Thread an accumularing argument through the map in ascending order of hashes.
mapAccumRWithKey :: forall s k a b c. (a -> Key s k -> b -> (a, c)) -> a -> HashMap s k b -> (a, HashMap s k c) Source #
Thread an accumularing argument through the map in descending order of hashes.
mapKeysWith :: forall s k1 k2 a. Hashable k2 => (a -> a -> a) -> (Key s k1 -> k2) -> HashMap s k1 a -> SomeHashMapWith (MapProof 'Hashed s k1 k2) k2 a Source #
applies mapKeysWith
c f mf
to each key of m
and collects the
results into a new map. For keys that were mapped to the same new key, c
acts as the combining function for corresponding values.
data MapProof f s a b r Source #
Proof that r
is the direct image of s
under some mapping f :: a -> b
.
backpermuteKeys :: forall s1 s2 k1 k2 a. (Hashable k1, KnownHashSet s2 k2) => (Key s2 k2 -> Key s1 k1) -> HashMap s1 k1 a -> HashMap s2 k2 a Source #
Apply the inverse image of the given function to the keys of the given map,
so that for all k ::
,c
Key
s2 k2
.backpermuteKeys
f m !
k = m !
f k
If maps are identified with functions, this computes the composition.
Folds
foldMapWithKey :: forall s k a m. Monoid m => (Key s k -> a -> m) -> HashMap s k a -> m Source #
Map each key-value pair of a map into a monoid (with proof that the key was
in the map), and combine the results using <>
.
foldrWithKey :: forall s k a b. (Key s k -> a -> b -> b) -> b -> HashMap s k a -> b Source #
Right associative fold with a lazy accumulator.
foldlWithKey :: forall s k a b. (b -> Key s k -> a -> b) -> b -> HashMap s k a -> b Source #
Left associative fold with a lazy accumulator.
foldrWithKey' :: forall s k a b. (Key s k -> a -> b -> b) -> b -> HashMap s k a -> b Source #
Right associative fold with a strict accumulator.
foldlWithKey' :: forall s k a b. (b -> Key s k -> a -> b) -> b -> HashMap s k a -> b Source #
Left associative fold with a strict accumulator.
Conversion
toMap :: forall s k a. HashMap s k a -> HashMap k a Source #
Convert to a regular HashMap
, forgetting its set of keys.
keysSet :: forall s k a. HashMap s k a -> HashSet s k Source #
Return the set of keys in the map, with the contents of the set still
tracked by the s
parameter. See Data.HashSet.Refined.
toList :: forall s k a. HashMap s k a -> [(Key s k, a)] Source #
Convert to a list of key-value pairs.
Filter
restrictKeys :: forall s t k a. (Hashable k, KnownHashSet t k) => HashMap s k a -> SomeHashMapWith (IntersectionProof 'Hashed s t) k a Source #
Restrict a map to only those keys that are elements of t
.
withoutKeys :: forall s t k a. (Hashable k, KnownHashSet t k) => HashMap s k a -> SomeHashMapWith (DifferenceProof 'Hashed s t) k a Source #
Remove all keys that are elements of t
from the map.
filterWithKey :: forall s k a. (Key s k -> a -> Bool) -> HashMap s k a -> SomeHashMapWith (SupersetProof 'Hashed s) k a Source #
Retain only the key-value pairs that satisfy the predicate, returning a potentially smaller map.
partitionWithKey :: forall s k a. Hashable k => (Key s k -> a -> Bool) -> HashMap s k a -> Some2HashMapWith (PartitionProof 'Hashed s k) k a a Source #
Partition a map into two disjoint submaps: those whose key-value pairs satisfy the predicate, and those whose don't.
data PartitionProof f s a r q Source #
Proof that s
is the union of disjoint subsets r
and q
, together with
a procedure that decides which of the two an element belongs to.
PartitionProof | |
|
mapMaybeWithKey :: forall s k a b. (Key s k -> a -> Maybe b) -> HashMap s k a -> SomeHashMapWith (SupersetProof 'Hashed s) k b Source #
Apply a function to all values in a map, together with their corresponding
keys, and collect only the Just
results, returning a potentially smaller
map.
mapEitherWithKey :: forall s k a b c. Hashable k => (Key s k -> a -> Either b c) -> HashMap s k a -> Some2HashMapWith (PartitionProof 'Hashed s k) k b c Source #
Casts
castKey :: forall s t k. (forall x. Key s x -> Key t x) -> (forall x. Key t x -> Key s x) -> Coercion (Key s k) (Key t k) Source #
If elements of s
can be weakened to elements of t
and vice versa, then
s
and t
actually stand for the same set and
can be safely
interconverted with Key
s
.Key
t
The requirement that the weakenings are natural transformations ensures that
they don't actually alter the keys. To build these you can compose :->
's
from proofs returned by functions in this module, or Refined functions like
andLeft
or leftOr
.
cast :: forall s t k. (forall x. Coercion (Key s x) (Key t x)) -> Coercion (HashMap s k) (HashMap t k) Source #
If keys can be interconverted (e.g. as proved by castKey
), then the maps
can be interconverted too. For example, zipWithKey
can be implemented via
intersectionWithKey
by proving that the set of keys
remains unchanged:
zipWithKey
:: forall s k a b c.Hashable
k => (Key
s k -> a -> b -> c) ->HashMap
s k a ->HashMap
s k b ->HashMap
s k czipWithKey
f m1 m2 |SomeHashMapWith
@r m proof <-intersectionWithKey
(f .andLeft
) m1 m2 ,IntersectionProof
p1 p2 <- proof , (Coercion
::Coercion
(HashMap
r k c) (HashMap
s k c)) <- app $cast
$castKey
(andLeft
. p1) (p2id
id
) =coerce
m where app ::Coercion
f g ->Coercion
(f x) (g x) appCoercion
=Coercion