{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE Rank2Types #-}
module Data.TMap
(
TMap
, empty
, one
, insert
, delete
, unionWith
, union
, intersectionWith
, intersection
, map
, adjust
, alter
, lookup
, member
, size
, keys
, keysWith
, toListWith
) where
import Prelude hiding (lookup, map)
import Data.Functor.Identity (Identity (..))
import Data.Typeable (Typeable)
import GHC.Exts (coerce)
import Type.Reflection (SomeTypeRep, TypeRep)
import qualified Data.TypeRepMap as F
type TMap = F.TypeRepMap Identity
empty :: TMap
empty :: TMap
empty = TMap
forall k (f :: k -> *). TypeRepMap f
F.empty
{-# INLINE empty #-}
one :: forall a . Typeable a => a -> TMap
one :: a -> TMap
one a
x = TMap -> TMap
coerce (Typeable a => Identity a -> TMap
forall k (a :: k) (f :: k -> *). Typeable a => f a -> TypeRepMap f
F.one @a @Identity (Identity a -> TMap) -> Identity a -> TMap
forall a b. (a -> b) -> a -> b
$ a -> Identity a
coerce a
x)
{-# INLINE one #-}
insert :: forall a . Typeable a => a -> TMap -> TMap
insert :: a -> TMap -> TMap
insert a
x = (TMap -> TMap) -> TMap -> TMap
coerce (Typeable a => Identity a -> TMap -> TMap
forall k (a :: k) (f :: k -> *).
Typeable a =>
f a -> TypeRepMap f -> TypeRepMap f
F.insert @a @Identity (Identity a -> TMap -> TMap) -> Identity a -> TMap -> TMap
forall a b. (a -> b) -> a -> b
$ a -> Identity a
coerce a
x)
{-# INLINE insert #-}
delete :: forall a . Typeable a => TMap -> TMap
delete :: TMap -> TMap
delete = Typeable a => TMap -> TMap
forall k (a :: k) (f :: k -> *).
Typeable a =>
TypeRepMap f -> TypeRepMap f
F.delete @a @Identity
{-# INLINE delete #-}
unionWith :: (forall x . Typeable x => x -> x -> x) -> TMap -> TMap -> TMap
unionWith :: (forall x. Typeable x => x -> x -> x) -> TMap -> TMap -> TMap
unionWith forall x. Typeable x => x -> x -> x
f = (forall x. Typeable x => Identity x -> Identity x -> Identity x)
-> TMap -> TMap -> TMap
forall k (f :: k -> *).
(forall (x :: k). Typeable x => f x -> f x -> f x)
-> TypeRepMap f -> TypeRepMap f -> TypeRepMap f
F.unionWith forall x. Typeable x => Identity x -> Identity x -> Identity x
fId
where
fId :: forall y . Typeable y => Identity y -> Identity y -> Identity y
fId :: Identity y -> Identity y -> Identity y
fId Identity y
y1 Identity y
y2 = y -> Identity y
forall a. a -> Identity a
Identity (y -> Identity y) -> y -> Identity y
forall a b. (a -> b) -> a -> b
$ y -> y -> y
forall x. Typeable x => x -> x -> x
f (Identity y -> y
coerce Identity y
y1) (Identity y -> y
coerce Identity y
y2)
{-# INLINE unionWith #-}
union :: TMap -> TMap -> TMap
union :: TMap -> TMap -> TMap
union = TMap -> TMap -> TMap
forall k (f :: k -> *).
TypeRepMap f -> TypeRepMap f -> TypeRepMap f
F.union
{-# INLINE union #-}
intersectionWith :: (forall x . Typeable x => x -> x -> x) -> TMap -> TMap -> TMap
intersectionWith :: (forall x. Typeable x => x -> x -> x) -> TMap -> TMap -> TMap
intersectionWith forall x. Typeable x => x -> x -> x
f = (forall x. Typeable x => Identity x -> Identity x -> Identity x)
-> TMap -> TMap -> TMap
forall k (f :: k -> *).
(forall (x :: k). Typeable x => f x -> f x -> f x)
-> TypeRepMap f -> TypeRepMap f -> TypeRepMap f
F.intersectionWith forall x. Typeable x => Identity x -> Identity x -> Identity x
fId
where
fId :: forall y . Typeable y => Identity y -> Identity y -> Identity y
fId :: Identity y -> Identity y -> Identity y
fId Identity y
y1 Identity y
y2 = Identity y -> Identity y -> Identity y
forall x. Typeable x => x -> x -> x
f (Identity y -> Identity y
coerce Identity y
y1) (Identity y -> Identity y
coerce Identity y
y2)
{-# INLINE intersectionWith #-}
intersection :: TMap -> TMap -> TMap
intersection :: TMap -> TMap -> TMap
intersection = TMap -> TMap -> TMap
forall k (f :: k -> *).
TypeRepMap f -> TypeRepMap f -> TypeRepMap f
F.intersection
{-# INLINE intersection #-}
lookup :: forall a . Typeable a => TMap -> Maybe a
lookup :: TMap -> Maybe a
lookup = (TMap -> Maybe (Identity a)) -> TMap -> Maybe a
coerce (Typeable a => TMap -> Maybe (Identity a)
forall k (a :: k) (f :: k -> *).
Typeable a =>
TypeRepMap f -> Maybe (f a)
F.lookup @a @Identity)
{-# INLINE lookup #-}
member :: forall a . Typeable a => TMap -> Bool
member :: TMap -> Bool
member = Typeable a => TMap -> Bool
forall k (a :: k) (f :: k -> *). Typeable a => TypeRepMap f -> Bool
F.member @a @Identity
{-# INLINE member #-}
size :: TMap -> Int
size :: TMap -> Int
size = TMap -> Int
forall k (f :: k -> *). TypeRepMap f -> Int
F.size
{-# INLINE size #-}
keys :: TMap -> [SomeTypeRep]
keys :: TMap -> [SomeTypeRep]
keys = TMap -> [SomeTypeRep]
forall k (f :: k -> *). TypeRepMap f -> [SomeTypeRep]
F.keys
{-# INLINE keys #-}
keysWith :: (forall a . TypeRep a -> r) -> TMap -> [r]
keysWith :: (forall a. TypeRep a -> r) -> TMap -> [r]
keysWith = (forall a. TypeRep a -> r) -> TMap -> [r]
forall k (f :: k -> *) r.
(forall (a :: k). TypeRep a -> r) -> TypeRepMap f -> [r]
F.keysWith
{-# INLINE keysWith #-}
toListWith :: (forall a . Typeable a => a -> r) -> TMap -> [r]
toListWith :: (forall a. Typeable a => a -> r) -> TMap -> [r]
toListWith forall a. Typeable a => a -> r
f = (forall a. Typeable a => Identity a -> r) -> TMap -> [r]
forall k (f :: k -> *) r.
(forall (a :: k). Typeable a => f a -> r) -> TypeRepMap f -> [r]
F.toListWith (a -> r
forall a. Typeable a => a -> r
f (a -> r) -> (Identity a -> a) -> Identity a -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
forall a. Identity a -> a
runIdentity)
{-# INLINE toListWith #-}
map :: (forall a . Typeable a => a -> a) -> TMap -> TMap
map :: (forall a. Typeable a => a -> a) -> TMap -> TMap
map forall a. Typeable a => a -> a
f = (forall x. Typeable x => Identity x -> Identity x) -> TMap -> TMap
forall k (f :: k -> *) (g :: k -> *).
(forall (x :: k). Typeable x => f x -> g x)
-> TypeRepMap f -> TypeRepMap g
F.hoistWithKey ((x -> x) -> Identity x -> Identity x
forall a. (a -> a) -> Identity a -> Identity a
liftToIdentity x -> x
forall a. Typeable a => a -> a
f)
{-# INLINE map #-}
adjust :: Typeable a => (a -> a) -> TMap -> TMap
adjust :: (a -> a) -> TMap -> TMap
adjust a -> a
f = (Identity a -> Identity a) -> TMap -> TMap
forall k (a :: k) (f :: k -> *).
Typeable a =>
(f a -> f a) -> TypeRepMap f -> TypeRepMap f
F.adjust ((a -> a) -> Identity a -> Identity a
forall a. (a -> a) -> Identity a -> Identity a
liftToIdentity a -> a
f)
{-# INLINE adjust #-}
alter :: Typeable a => (Maybe a -> Maybe a) -> TMap -> TMap
alter :: (Maybe a -> Maybe a) -> TMap -> TMap
alter Maybe a -> Maybe a
f = (Maybe (Identity a) -> Maybe (Identity a)) -> TMap -> TMap
forall k (a :: k) (f :: k -> *).
Typeable a =>
(Maybe (f a) -> Maybe (f a)) -> TypeRepMap f -> TypeRepMap f
F.alter ((Maybe a -> Maybe a) -> Maybe (Identity a) -> Maybe (Identity a)
forall a.
(Maybe a -> Maybe a) -> Maybe (Identity a) -> Maybe (Identity a)
liftF Maybe a -> Maybe a
f)
where
liftF :: forall a . (Maybe a -> Maybe a) -> Maybe (Identity a) -> Maybe (Identity a)
liftF :: (Maybe a -> Maybe a) -> Maybe (Identity a) -> Maybe (Identity a)
liftF = (Maybe a -> Maybe a) -> Maybe (Identity a) -> Maybe (Identity a)
coerce
{-# INLINE alter #-}
liftToIdentity :: forall a . (a -> a) -> Identity a -> Identity a
liftToIdentity :: (a -> a) -> Identity a -> Identity a
liftToIdentity = (a -> a) -> Identity a -> Identity a
coerce