{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Data.TypeMap.Internal.Dynamic.Alt where

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative (Applicative, (<$>))
#endif
import Data.Typeable
#if MIN_VERSION_base(4,10,0)
import GHC.Exts (Any)
import qualified Type.Reflection as T
#else
import GHC.Prim (Any, Proxy#)
#endif
import Unsafe.Coerce
import qualified Data.Map as Map

import Data.TypeMap.Internal.Dynamic
  (TypeMap(..), Item, Typed, UnTyped, ItemFun, ItemKleisli)

-- | Insert an element indexed by type @t@.
insert
  :: forall t x
  .  Typeable t => Item x t -> TypeMap x -> TypeMap x
insert :: Item x t -> TypeMap x -> TypeMap x
insert Item x t
v (TypeMap Map TypeRep Any
m) = Map TypeRep Any -> TypeMap x
forall x. Map TypeRep Any -> TypeMap x
TypeMap (TypeRep -> Any -> Map TypeRep Any -> Map TypeRep Any
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Proxy t -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy t
forall k (t :: k). Proxy t
Proxy @t)) (Item x t -> Any
coerce Item x t
v) Map TypeRep Any
m)
  where
    coerce :: Item x t -> Any
    coerce :: Item x t -> Any
coerce = Item x t -> Any
forall a b. a -> b
unsafeCoerce

-- What is a good fixity for (<:)?

-- | Infix version of 'insert' to facilitate a literal-ish syntax:
--
-- @
-- 'Data.TypeMap.Dynamic.empty'
--   '<:' 'at' @t1 v1
--   '<:' 'at' @t2 v2
-- @
(<:)
  :: forall t x proxy
  .  Typeable t => TypeMap x -> (proxy t, Item x t) -> TypeMap x
<: :: TypeMap x -> (proxy t, Item x t) -> TypeMap x
(<:) TypeMap x
tm (proxy t
_, Item x t
v) = Item x t -> TypeMap x -> TypeMap x
forall t x. Typeable t => Item x t -> TypeMap x -> TypeMap x
insert @t Item x t
v TypeMap x
tm

-- | See @('<:')@.
at :: forall t a
   .  Typeable t => a -> (Proxy t, a)
at :: a -> (Proxy t, a)
at = (,) Proxy t
forall k (t :: k). Proxy t
Proxy

-- | Update an element indexed by type @t@.
update
  :: forall t x
  .  Typeable t => (Item x t -> Maybe (Item x t)) -> TypeMap x -> TypeMap x
update :: (Item x t -> Maybe (Item x t)) -> TypeMap x -> TypeMap x
update Item x t -> Maybe (Item x t)
f (TypeMap Map TypeRep Any
m) = Map TypeRep Any -> TypeMap x
forall x. Map TypeRep Any -> TypeMap x
TypeMap ((Any -> Maybe Any) -> TypeRep -> Map TypeRep Any -> Map TypeRep Any
forall k a. Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a
Map.update ((Item x t -> Maybe (Item x t)) -> Any -> Maybe Any
coerce Item x t -> Maybe (Item x t)
f) (Proxy t -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy t
forall k (t :: k). Proxy t
Proxy @t)) Map TypeRep Any
m)
  where
    coerce :: (Item x t -> Maybe (Item x t)) -> (Any -> Maybe Any)
    coerce :: (Item x t -> Maybe (Item x t)) -> Any -> Maybe Any
coerce = (Item x t -> Maybe (Item x t)) -> Any -> Maybe Any
forall a b. a -> b
unsafeCoerce

-- | Update a (possibly absent) element indexed by type @t@.
alter
  :: forall t x
  .  Typeable t => (Maybe (Item x t) -> Maybe (Item x t)) -> TypeMap x -> TypeMap x
alter :: (Maybe (Item x t) -> Maybe (Item x t)) -> TypeMap x -> TypeMap x
alter Maybe (Item x t) -> Maybe (Item x t)
f (TypeMap Map TypeRep Any
m) = Map TypeRep Any -> TypeMap x
forall x. Map TypeRep Any -> TypeMap x
TypeMap ((Maybe Any -> Maybe Any)
-> TypeRep -> Map TypeRep Any -> Map TypeRep Any
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter ((Maybe (Item x t) -> Maybe (Item x t)) -> Maybe Any -> Maybe Any
coerce Maybe (Item x t) -> Maybe (Item x t)
f) (Proxy t -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy t
forall k (t :: k). Proxy t
Proxy @t)) Map TypeRep Any
m)
  where
    coerce :: (Maybe (Item x t) -> Maybe (Item x t)) -> (Maybe Any -> Maybe Any)
    coerce :: (Maybe (Item x t) -> Maybe (Item x t)) -> Maybe Any -> Maybe Any
coerce = (Maybe (Item x t) -> Maybe (Item x t)) -> Maybe Any -> Maybe Any
forall a b. a -> b
unsafeCoerce

-- | Lookup an element indexed by type @t@.
lookup
  :: forall t x
  .  Typeable t => TypeMap x -> Maybe (Item x t)
lookup :: TypeMap x -> Maybe (Item x t)
lookup (TypeMap Map TypeRep Any
m) = Maybe Any -> Maybe (Item x t)
coerce (TypeRep -> Map TypeRep Any -> Maybe Any
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Proxy t -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy t
forall k (t :: k). Proxy t
Proxy @t)) Map TypeRep Any
m)
  where
    coerce :: Maybe Any -> Maybe (Item x t)
    coerce :: Maybe Any -> Maybe (Item x t)
coerce = Maybe Any -> Maybe (Item x t)
forall a b. a -> b
unsafeCoerce

-- | Delete a key and its value from the map.
-- Does nothing if the key does not exist.
delete
  :: forall t x
  .  Typeable t => TypeMap x -> TypeMap x
delete :: TypeMap x -> TypeMap x
delete (TypeMap Map TypeRep Any
m) = Map TypeRep Any -> TypeMap x
forall x. Map TypeRep Any -> TypeMap x
TypeMap (TypeRep -> Map TypeRep Any -> Map TypeRep Any
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete (Proxy t -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy t
forall k (t :: k). Proxy t
Proxy @t)) Map TypeRep Any
m)

-- | Map a function on all elements.
map
  :: forall x y. (forall t. Typeable t => Item x t -> Item y t)
  -> TypeMap x -> TypeMap y
map :: (forall t. Typeable t => Item x t -> Item y t)
-> TypeMap x -> TypeMap y
map forall t. Typeable t => Item x t -> Item y t
f (TypeMap Map TypeRep Any
m) = Map TypeRep Any -> TypeMap y
forall x. Map TypeRep Any -> TypeMap x
TypeMap ((TypeRep -> Any -> Any) -> Map TypeRep Any -> Map TypeRep Any
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey TypeRep -> UnTyped (ItemFun x y)
TypeRep -> Any -> Any
f' Map TypeRep Any
m)
  where
    f' :: TypeRep -> UnTyped (ItemFun x y)
f' = (forall t. Typeable t => Typed_ (ItemFun x y) t)
-> TypeRep -> UnTyped (ItemFun x y)
forall x.
(forall t. Typeable t => Typed_ x t) -> TypeRep -> UnTyped x
withTypeRep @(ItemFun x y)
      (Typed (ItemFun x y) t -> Typed_ (ItemFun x y) t
forall x t. Typed x t -> Typed_ x t
Typed_ (Typeable t => Item x t -> Item y t
forall t. Typeable t => Item x t -> Item y t
f @t) :: forall t. Typeable t => Typed_ (ItemFun x y) t)

-- | Traverse the type map. ('map' with effects.)
traverse
  :: forall f x y
  .  Applicative f
  => (forall t. Typeable t => Item x t -> f (Item y t))
  -> TypeMap x -> f (TypeMap y)
traverse :: (forall t. Typeable t => Item x t -> f (Item y t))
-> TypeMap x -> f (TypeMap y)
traverse forall t. Typeable t => Item x t -> f (Item y t)
f (TypeMap Map TypeRep Any
m) = Map TypeRep Any -> TypeMap y
forall x. Map TypeRep Any -> TypeMap x
TypeMap (Map TypeRep Any -> TypeMap y)
-> f (Map TypeRep Any) -> f (TypeMap y)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TypeRep -> Any -> f Any) -> Map TypeRep Any -> f (Map TypeRep Any)
forall (t :: * -> *) k a b.
Applicative t =>
(k -> a -> t b) -> Map k a -> t (Map k b)
Map.traverseWithKey TypeRep -> UnTyped (ItemKleisli f x y)
TypeRep -> Any -> f Any
f' Map TypeRep Any
m
  where
    f' :: TypeRep -> UnTyped (ItemKleisli f x y)
f' = (forall t. Typeable t => Typed_ (ItemKleisli f x y) t)
-> TypeRep -> UnTyped (ItemKleisli f x y)
forall x.
(forall t. Typeable t => Typed_ x t) -> TypeRep -> UnTyped x
withTypeRep @(ItemKleisli f x y)
      (Typed (ItemKleisli f x y) t -> Typed_ (ItemKleisli f x y) t
forall x t. Typed x t -> Typed_ x t
Typed_ (Typeable t => Item x t -> f (Item y t)
forall t. Typeable t => Item x t -> f (Item y t)
f @t) :: forall t. Typeable t => Typed_ (ItemKleisli f x y) t)

-- * Unsafe internals

newtype Typed_ x t = Typed_ (Typed x t)

withTypeRep
  :: forall x
  .  (forall t. Typeable t => Typed_ x t)
  -> TypeRep -> UnTyped x
#if MIN_VERSION_base(4,10,0)
withTypeRep :: (forall t. Typeable t => Typed_ x t) -> TypeRep -> UnTyped x
withTypeRep forall t. Typeable t => Typed_ x t
f TypeRep
someRep =
  case TypeRep
someRep of
    T.SomeTypeRep (TypeRep a
rep' :: T.TypeRep t) ->
      -- We still need to unsafely coerce the kind of t to Type
      -- and Typed to UnTyped
      ((TypeRep Any -> Typed_ x Any) -> TypeRep a -> UnTyped x
forall a b. a -> b
unsafeCoerce
        ((\TypeRep a
rep -> TypeRep a -> (Typeable a => Typed_ x a) -> Typed_ x a
forall k (a :: k) r. TypeRep a -> (Typeable a => r) -> r
T.withTypeable TypeRep a
rep Typeable a => Typed_ x a
forall t. Typeable t => Typed_ x t
f) :: forall a. T.TypeRep a -> Typed_ x a)
        :: T.TypeRep t -> UnTyped x) TypeRep a
rep'
#else
withTypeRep f rep =
  withTypeable (WithTypeable f :: WithTypeable x) (\_ -> rep)

newtype WithTypeable x
  = WithTypeable (forall t. Typeable t => Typed_ x t)

withTypeable
  :: WithTypeable x -> (Proxy# () -> TypeRep) -> UnTyped x
withTypeable = unsafeCoerce
#endif