module Data.HashMap.Refined
(
Common.HashMap
, Common.Key
, Common.SomeHashMap(..)
, Common.withHashMap
, Common.SomeHashMapWith(..)
, Common.withHashMapWith
, Common.Some2HashMapWith(..)
, Common.with2HashMapWith
, SupersetProof(..)
, EmptyProof(..)
, Common.empty
, singleton
, SingletonProof(..)
, fromSet
, Common.fromHashMap
, fromTraversableWithKey
, FromTraversableProof(..)
, insert
, InsertProof(..)
, reinsert
, insertLookupWithKey
, Common.delete
, adjust
, adjustWithKey
, update
, updateLookupWithKey
, Common.lookup
, (Common.!)
, Common.member
, Common.null
, Common.isSubmapOfBy
, SubsetProof(..)
, Common.disjoint
, DisjointProof(..)
, zipWithKey
, bind
, unionWithKey
, UnionProof(..)
, Common.difference
, DifferenceProof(..)
, differenceWithKey
, PartialDifferenceProof(..)
, intersectionWithKey
, IntersectionProof(..)
, mapWithKey
, traverseWithKey
, mapAccumLWithKey
, mapAccumRWithKey
, mapKeysWith
, MapProof(..)
, backpermuteKeys
, Common.foldMapWithKey
, Common.foldrWithKey
, Common.foldlWithKey
, Common.foldrWithKey'
, Common.foldlWithKey'
, Common.toMap
, Common.keysSet
, Common.toList
, Common.restrictKeys
, Common.withoutKeys
, Common.filterWithKey
, Common.partitionWithKey
, PartitionProof(..)
, mapMaybeWithKey
, mapEitherWithKey
, Common.castKey
, Common.cast
, castFlavor
) where
import Data.Coerce
import Data.Container.Refined.Hashable
import Data.Container.Refined.Proofs
import Data.Container.Refined.Unsafe
import Data.Functor
import Data.HashMap.Common.Refined
( HashMap(..), Key, unsafeCastKey, unsafeKey, SomeHashMapWith(..)
, Some2HashMapWith(..), fromSet, (!), zipWithKey, mapWithKey, traverseWithKey
, bind
)
import qualified Data.HashMap.Common.Refined as Common
import qualified Data.HashMap.Lazy as HashMap
import Data.Traversable
import Data.Traversable.WithIndex
import Data.Type.Coercion
import Prelude hiding (lookup, null)
import Refined
import Refined.Unsafe
singleton
:: forall k a. Hashable k
=> k -> a -> SomeHashMapWith (SingletonProof 'Hashed k) k a
singleton :: forall k a.
Hashable k =>
k -> a -> SomeHashMapWith (SingletonProof 'Hashed k) k a
singleton k
k a
v = forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith (forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k v. Hashable k => k -> v -> HashMap k v
HashMap.singleton k
k a
v)
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) a r.
Refined (InSet f r) a -> SingletonProof f a r
SingletonProof (forall k s. k -> Key s k
unsafeKey k
k)
fromTraversableWithKey
:: forall t k a. (Traversable t, Hashable k)
=> (k -> a -> a -> a)
-> t (k, a)
-> SomeHashMapWith (FromTraversableProof 'Hashed t k) 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
fromTraversableWithKey k -> a -> a -> a
f t (k, a)
xs
= forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith (forall s k a. HashMap k a -> HashMap s k a
HashMap HashMap k a
m) forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) (t :: * -> *) a r.
t (Refined (InSet f r) a) -> FromTraversableProof f t a r
FromTraversableProof t (Key Any k)
proof
where
(HashMap k a
m, t (Key Any k)
proof) = forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumL
(\HashMap k a
s (k
k, a
v)
-> let !s' :: HashMap k a
s' = forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
HashMap.insertWith (k -> a -> a -> a
f k
k) k
k a
v HashMap k a
s in (HashMap k a
s', forall k s. k -> Key s k
unsafeKey k
k))
forall k v. HashMap k v
HashMap.empty
t (k, a)
xs
insert
:: forall s k a. Hashable k
=> k -> a -> HashMap s k a -> SomeHashMapWith (InsertProof 'Hashed k s) k a
insert :: forall s k a.
Hashable k =>
k
-> a
-> HashMap s k a
-> SomeHashMapWith (InsertProof 'Hashed k s) k a
insert k
k a
v (HashMap HashMap k a
m) = forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith (forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HashMap.insert k
k a
v HashMap k a
m)
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) a s r.
Refined (InSet f r) a
-> (InSet f s :-> InSet f r) -> InsertProof f a s r
InsertProof (forall k s. k -> Key s k
unsafeKey k
k) forall p q. p :-> q
unsafeSubset
reinsert
:: forall s k a. Hashable k
=> Key s k -> a -> HashMap s k a -> HashMap s k a
reinsert :: forall s k a.
Hashable k =>
Key s k -> a -> HashMap s k a -> HashMap s k a
reinsert = forall {k} (a :: k) (b :: k) r.
Coercion a b -> (Coercible a b => r) -> r
gcoerceWith (forall s k. Coercion k (Key s k)
unsafeCastKey @s @k) forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce forall a b. (a -> b) -> a -> b
$ forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HashMap.insert @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)
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)
insertLookupWithKey Key s k -> a -> a -> a
f k
k a
v (HashMap HashMap k a
m) =
( (forall k s. k -> Key s k
unsafeKey k
k,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup k
k HashMap k a
m
, forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith (forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
HashMap.insertWith (Key s k -> a -> a -> a
f forall a b. (a -> b) -> a -> b
$ forall k s. k -> Key s k
unsafeKey k
k) k
k a
v HashMap k a
m)
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) a s r.
Refined (InSet f r) a
-> (InSet f s :-> InSet f r) -> InsertProof f a s r
InsertProof (forall k s. k -> Key s k
unsafeKey k
k) forall p q. p :-> q
unsafeSubset
)
adjust
:: forall s k a. Hashable k
=> (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
adjust :: forall s k a.
Hashable k =>
(a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
adjust = forall {k} (a :: k) (b :: k) r.
Coercion a b -> (Coercible a b => r) -> r
gcoerceWith (forall s k. Coercion k (Key s k)
unsafeCastKey @s @k) forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce forall a b. (a -> b) -> a -> b
$ forall k v.
(Eq k, Hashable k) =>
(v -> v) -> k -> HashMap k v -> HashMap k v
HashMap.adjust @k @a
adjustWithKey
:: forall s k a. Hashable k
=> (Key s k -> a -> a) -> 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
adjustWithKey Key s k -> a -> a
f k
k (HashMap HashMap k a
m) = forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k v.
(Eq k, Hashable k) =>
(v -> v) -> k -> HashMap k v -> HashMap k v
HashMap.adjust (Key s k -> a -> a
f forall a b. (a -> b) -> a -> b
$ forall k s. k -> Key s k
unsafeKey k
k) k
k HashMap k a
m
update
:: forall s k a. Hashable k
=> (a -> Maybe a)
-> Key s k
-> HashMap s k a
-> SomeHashMapWith (SupersetProof 'Hashed 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
update a -> Maybe a
f Key s k
k (HashMap HashMap k a
m)
= forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith (forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k a.
(Eq k, Hashable k) =>
(a -> Maybe a) -> k -> HashMap k a -> HashMap k a
HashMap.update a -> Maybe a
f (forall {k} (p :: k) x. Refined p x -> x
unrefine Key s k
k) HashMap k a
m)
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) s r.
(InSet f r :-> InSet f s) -> SupersetProof f s r
SupersetProof forall p q. p :-> q
unsafeSubset
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)
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)
updateLookupWithKey Key s k -> a -> Maybe a
f k
k (HashMap HashMap k a
m) =
( (forall k s. k -> Key s k
unsafeKey k
k,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup k
k HashMap k a
m
, forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith (forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k a.
(Eq k, Hashable k) =>
(a -> Maybe a) -> k -> HashMap k a -> HashMap k a
HashMap.update (Key s k -> a -> Maybe a
f forall a b. (a -> b) -> a -> b
$ forall k s. k -> Key s k
unsafeKey k
k) k
k HashMap k a
m)
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) s r.
(InSet f r :-> InSet f s) -> SupersetProof f s r
SupersetProof forall p q. p :-> q
unsafeSubset
)
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
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
unionWithKey Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> a -> a
f (HashMap HashMap k a
m1) (HashMap HashMap k a
m2) = forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith
(forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k v.
(Eq k, Hashable k) =>
(k -> v -> v -> v) -> HashMap k v -> HashMap k v -> HashMap k v
HashMap.unionWithKey (Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> a -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} x (p :: k). x -> Refined p x
reallyUnsafeRefine) HashMap k a
m1 HashMap k a
m2)
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) s t r.
((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)
-> UnionProof f s t r
UnionProof forall p q. p :-> q
unsafeSubset forall p' q' p'' q'' p q. (p' :-> q') -> (p'' :-> q'') -> p :-> q
unsafeSubsetWith2
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
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
differenceWithKey Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> b -> Maybe a
f (HashMap HashMap k a
m1) (HashMap HashMap k b
m2) = forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith
(forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k v w.
(Eq k, Hashable k) =>
(v -> w -> Maybe v) -> HashMap k v -> HashMap k w -> HashMap k v
HashMap.differenceWith
(\a
x (k
k, b
y) -> Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> b -> Maybe a
f (forall {k} x (p :: k). x -> Refined p x
reallyUnsafeRefine k
k) a
x b
y)
HashMap k a
m1
(forall k v1 v2. (k -> v1 -> v2) -> HashMap k v1 -> HashMap k v2
HashMap.mapWithKey (,) HashMap k b
m2))
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) s t r.
(InSet f r :-> InSet f s)
-> (InSet f s :-> (InSet f t || InSet f r))
-> PartialDifferenceProof f s t r
PartialDifferenceProof forall p q. p :-> q
unsafeSubset forall p q. p :-> q
unsafeSubset
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
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
intersectionWithKey Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> b -> c
f (HashMap HashMap k a
m1) (HashMap HashMap k b
m2) = forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith
(forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k v1 v2 v3.
(Eq k, Hashable k) =>
(k -> v1 -> v2 -> v3)
-> HashMap k v1 -> HashMap k v2 -> HashMap k v3
HashMap.intersectionWithKey (Refined (InSet 'Hashed s && InSet 'Hashed t) k -> a -> b -> c
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} x (p :: k). x -> Refined p x
reallyUnsafeRefine) HashMap k a
m1 HashMap k b
m2)
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) s t r.
(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)
-> IntersectionProof f s t r
IntersectionProof forall p q. p :-> q
unsafeSubset forall p' q' p'' q'' p q. (p' :-> q') -> (p'' :-> q'') -> p :-> q
unsafeSubsetWith2
mapAccumLWithKey
:: forall s k a b c. (a -> Key s k -> b -> (a, c))
-> a
-> HashMap s k b
-> (a, HashMap s k c)
mapAccumLWithKey :: forall s k a b c.
(a -> Key s k -> b -> (a, c))
-> a -> HashMap s k b -> (a, HashMap s k c)
mapAccumLWithKey a -> Key s k -> b -> (a, c)
f = forall i (t :: * -> *) s a b.
TraversableWithIndex i t =>
(i -> s -> a -> (s, b)) -> s -> t a -> (s, t b)
imapAccumL (forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> Key s k -> b -> (a, c)
f)
mapAccumRWithKey
:: 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)
mapAccumRWithKey a -> Key s k -> b -> (a, c)
f = forall i (t :: * -> *) s a b.
TraversableWithIndex i t =>
(i -> s -> a -> (s, b)) -> s -> t a -> (s, t b)
imapAccumR (forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> Key s k -> b -> (a, c)
f)
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
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
mapKeysWith a -> a -> a
f Key s k1 -> k2
g (HashMap HashMap k1 a
m) = forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith
(forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> [(k, v)] -> HashMap k v
HashMap.fromListWith a -> a -> a
f
forall a b. (a -> b) -> a -> b
$ forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
HashMap.foldrWithKey (\k1
k a
x [(k2, a)]
xs -> (Key s k1 -> k2
g forall a b. (a -> b) -> a -> b
$ forall k s. k -> Key s k
unsafeKey k1
k, a
x) forall a. a -> [a] -> [a]
: [(k2, a)]
xs) [] HashMap k1 a
m)
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) s a b r.
(Refined (InSet f s) a -> Refined (InSet f r) b)
-> (Refined (InSet f r) b -> Refined (InSet f s) a)
-> MapProof f s a b r
MapProof (forall k s. k -> Key s k
unsafeKey forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key s k1 -> k2
g) \Refined (InSet 'Hashed Any) k2
k2 ->
case forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (forall {k} (p :: k) x. Refined p x -> x
unrefine Refined (InSet 'Hashed Any) k2
k2) HashMap k2 (Key s k1)
backMap of
Maybe (Key s k1)
Nothing -> forall a. HasCallStack => [Char] -> a
error
[Char]
"mapKeysWith: bug: Data.HashMap.Refined has been subverted"
Just Key s k1
k1 -> Key s k1
k1
where
~HashMap k2 (Key s k1)
backMap = forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ (k2
k2, forall k s. k -> Key s k
unsafeKey k1
k1)
| k1
k1 <- forall k v. HashMap k v -> [k]
HashMap.keys HashMap k1 a
m
, let !k2 :: k2
k2 = Key s k1 -> k2
g forall a b. (a -> b) -> a -> b
$ forall k s. k -> Key s k
unsafeKey k1
k1
]
mapMaybeWithKey
:: forall s k a b. (Key s k -> a -> Maybe b)
-> HashMap s k a
-> SomeHashMapWith (SupersetProof 'Hashed s) k b
mapMaybeWithKey :: forall s k a b.
(Key s k -> a -> Maybe b)
-> HashMap s k a -> SomeHashMapWith (SupersetProof 'Hashed s) k b
mapMaybeWithKey Key s k -> a -> Maybe b
f (HashMap HashMap k a
m)
= forall s k a (p :: * -> *).
HashMap s k a -> p s -> SomeHashMapWith p k a
SomeHashMapWith (forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall k v1 v2.
(k -> v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2
HashMap.mapMaybeWithKey (Key s k -> a -> Maybe b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k s. k -> Key s k
unsafeKey) HashMap k a
m)
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) s r.
(InSet f r :-> InSet f s) -> SupersetProof f s r
SupersetProof forall p q. p :-> q
unsafeSubset
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
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
mapEitherWithKey Key s k -> a -> Either b c
p (HashMap HashMap k a
m)
| HashMap k (Either b c)
m' <- forall k v1 v2. (k -> v1 -> v2) -> HashMap k v1 -> HashMap k v2
HashMap.mapWithKey (Key s k -> a -> Either b c
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k s. k -> Key s k
unsafeKey) HashMap k a
m
= forall s t k a b (p :: * -> * -> *).
HashMap s k a -> HashMap t k b -> p s t -> Some2HashMapWith p k a b
Some2HashMapWith
(forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall v1 v2 k. (v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2
HashMap.mapMaybe (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. a -> Maybe a
Just (forall a b. a -> b -> a
const forall a. Maybe a
Nothing)) HashMap k (Either b c)
m')
(forall s k a. HashMap k a -> HashMap s k a
HashMap forall a b. (a -> b) -> a -> b
$ forall v1 v2 k. (v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2
HashMap.mapMaybe (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just) HashMap k (Either b c)
m')
forall a b. (a -> b) -> a -> b
$ forall (f :: Flavor) s a r q.
(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)
-> PartitionProof f s a r q
PartitionProof
do \Key s k
k -> case forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (forall {k} (p :: k) x. Refined p x -> x
unrefine Key s k
k) HashMap k a
m of
Maybe a
Nothing -> forall a. HasCallStack => [Char] -> a
error
[Char]
"mapEitherWithKey: bug: Data.HashMap.Refined has been subverted"
Just a
x -> case Key s k -> a -> Either b c
p Key s k
k a
x of
Left b
_ -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall k s. k -> Key s k
unsafeKey forall a b. (a -> b) -> a -> b
$ forall {k} (p :: k) x. Refined p x -> x
unrefine Key s k
k
Right c
_ -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall k s. k -> Key s k
unsafeKey forall a b. (a -> b) -> a -> b
$ forall {k} (p :: k) x. Refined p x -> x
unrefine Key s k
k
forall p q. p :-> q
unsafeSubset forall p' q' p'' q'' p q. (p' :-> q') -> (p'' :-> q'') -> p :-> q
unsafeSubsetWith2 \InSet 'Hashed t :-> InSet 'Hashed Any
f InSet 'Hashed t :-> InSet 'Hashed Any
g -> forall p' q' p'' q'' p q. (p' :-> q') -> (p'' :-> q'') -> p :-> q
unsafeSubsetWith2 InSet 'Hashed t :-> InSet 'Hashed Any
f InSet 'Hashed t :-> InSet 'Hashed Any
g
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
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
backpermuteKeys Key s2 k2 -> Key s1 k1
f HashMap s1 k1 a
m = forall s k a. KnownHashSet s k => (Key s k -> a) -> HashMap s k a
fromSet \Key s2 k2
k -> HashMap s1 k1 a
m forall s k a. Hashable k => HashMap s k a -> Key s k -> a
! Key s2 k2 -> Key s1 k1
f Key s2 k2
k