{-# LANGUAGE CPP #-}
module Control.Effect.Type.ReaderPrim
(
ReaderPrim(..)
, threadReaderPrim
, threadReaderPrimViaClass
, threadReaderPrimViaRegional
) where
import Control.Monad.Trans
import Control.Monad.Reader.Class (MonadReader)
import qualified Control.Monad.Reader.Class as RC
import Control.Monad.Trans.Except (ExceptT(..))
import Control.Monad.Trans.Reader (ReaderT(..))
import qualified Control.Monad.Trans.Reader as R
import qualified Control.Monad.Trans.State.Strict as SSt
import qualified Control.Monad.Trans.State.Lazy as LSt
import qualified Control.Monad.Trans.Writer.Lazy as LWr
import qualified Control.Monad.Trans.Writer.Strict as SWr
import qualified Control.Monad.Trans.Writer.CPS as CPSWr
import Control.Monad.Trans.Cont (ContT(..))
import qualified Control.Monad.Trans.Cont as C
import Control.Effect.Internal.ViaAlg
import Control.Effect.Type.Regional
import Control.Effect.Internal.Reflection
import Control.Effect.Internal.Union
data ReaderPrim i :: Effect where
ReaderPrimAsk :: ReaderPrim i m i
ReaderPrimLocal :: (i -> i) -> m a -> ReaderPrim i m a
instance ( Reifies s (ReifiedEffAlgebra (ReaderPrim i) m)
, Monad m
) => MonadReader i (ViaAlg s (ReaderPrim i) m) where
ask :: ViaAlg s (ReaderPrim i) m i
ask = case forall a. Reifies s a => a
forall k (s :: k) a. Reifies s a => a
reflect @s of
ReifiedEffAlgebra forall x. ReaderPrim i m x -> m x
alg -> (ReaderPrim i m i -> m i)
-> ReaderPrim i (ViaAlg s (ReaderPrim i) m) i
-> ViaAlg s (ReaderPrim i) m i
forall (n :: * -> *) (m :: * -> *) (e :: Effect) a b.
(Coercible n m, RepresentationalEff e) =>
(e m a -> m b) -> e n a -> n b
coerceAlg ReaderPrim i m i -> m i
forall x. ReaderPrim i m x -> m x
alg ReaderPrim i (ViaAlg s (ReaderPrim i) m) i
forall i (m :: * -> *). ReaderPrim i m i
ReaderPrimAsk
{-# INLINE ask #-}
local :: (i -> i)
-> ViaAlg s (ReaderPrim i) m a -> ViaAlg s (ReaderPrim i) m a
local i -> i
f ViaAlg s (ReaderPrim i) m a
m = case forall a. Reifies s a => a
forall k (s :: k) a. Reifies s a => a
reflect @s of
ReifiedEffAlgebra forall x. ReaderPrim i m x -> m x
alg -> (ReaderPrim i m a -> m a)
-> ReaderPrim i (ViaAlg s (ReaderPrim i) m) a
-> ViaAlg s (ReaderPrim i) m a
forall (n :: * -> *) (m :: * -> *) (e :: Effect) a b.
(Coercible n m, RepresentationalEff e) =>
(e m a -> m b) -> e n a -> n b
coerceAlg ReaderPrim i m a -> m a
forall x. ReaderPrim i m x -> m x
alg ((i -> i)
-> ViaAlg s (ReaderPrim i) m a
-> ReaderPrim i (ViaAlg s (ReaderPrim i) m) a
forall i (m :: * -> *) a. (i -> i) -> m a -> ReaderPrim i m a
ReaderPrimLocal i -> i
f ViaAlg s (ReaderPrim i) m a
m)
{-# INLINE local #-}
threadReaderPrim :: forall i t m a
. (MonadTrans t, Monad m)
=> ( (forall x. ReaderPrim i m x -> m x)
-> (i -> i) -> t m a -> t m a
)
-> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (t m) a -> t m a
threadReaderPrim :: ((forall x. ReaderPrim i m x -> m x) -> (i -> i) -> t m a -> t m a)
-> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (t m) a
-> t m a
threadReaderPrim (forall x. ReaderPrim i m x -> m x) -> (i -> i) -> t m a -> t m a
h forall x. ReaderPrim i m x -> m x
alg = \case
ReaderPrim i (t m) a
ReaderPrimAsk -> m i -> t m i
forall (t :: Effect) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderPrim i m i -> m i
forall x. ReaderPrim i m x -> m x
alg ReaderPrim i m i
forall i (m :: * -> *). ReaderPrim i m i
ReaderPrimAsk)
ReaderPrimLocal i -> i
f t m a
m -> (forall x. ReaderPrim i m x -> m x) -> (i -> i) -> t m a -> t m a
h forall x. ReaderPrim i m x -> m x
alg i -> i
f t m a
m
{-# INLINE threadReaderPrim #-}
threadReaderPrimViaClass :: forall i t m a
. Monad m
=> ( RepresentationalT t
, MonadTrans t
, forall b. MonadReader i b => MonadReader i (t b)
)
=> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (t m) a -> t m a
threadReaderPrimViaClass :: (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (t m) a -> t m a
threadReaderPrimViaClass forall x. ReaderPrim i m x -> m x
alg ReaderPrim i (t m) a
e = ReifiedEffAlgebra (ReaderPrim i) m
-> (forall s (pr :: * -> *).
(pr ~ Proxy, Reifies s (ReifiedEffAlgebra (ReaderPrim i) m)) =>
pr s -> t m a)
-> t m a
forall a r.
a
-> (forall s (pr :: * -> *).
(pr ~ Proxy, Reifies s a) =>
pr s -> r)
-> r
reify ((forall x. ReaderPrim i m x -> m x)
-> ReifiedEffAlgebra (ReaderPrim i) m
forall k (e :: (k -> *) -> k -> *) (m :: k -> *).
(forall (x :: k). e m x -> m x) -> ReifiedEffAlgebra e m
ReifiedEffAlgebra forall x. ReaderPrim i m x -> m x
alg) ((forall s (pr :: * -> *).
(pr ~ Proxy, Reifies s (ReifiedEffAlgebra (ReaderPrim i) m)) =>
pr s -> t m a)
-> t m a)
-> (forall s (pr :: * -> *).
(pr ~ Proxy, Reifies s (ReifiedEffAlgebra (ReaderPrim i) m)) =>
pr s -> t m a)
-> t m a
forall a b. (a -> b) -> a -> b
$ \(pr s
_ :: pr s) ->
case ReaderPrim i (t m) a
e of
ReaderPrim i (t m) a
ReaderPrimAsk -> m i -> t m i
forall (t :: Effect) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderPrim i m i -> m i
forall x. ReaderPrim i m x -> m x
alg ReaderPrim i m i
forall i (m :: * -> *). ReaderPrim i m i
ReaderPrimAsk)
ReaderPrimLocal i -> i
f t m a
m -> t (ViaAlg s (ReaderPrim i) m) a -> t m a
forall s (e :: Effect) (t :: Effect) (m :: * -> *) a.
RepresentationalT t =>
t (ViaAlg s e m) a -> t m a
unViaAlgT ((i -> i)
-> t (ViaAlg s (ReaderPrim i) m) a
-> t (ViaAlg s (ReaderPrim i) m) a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
RC.local i -> i
f (t m a -> t (ViaAlg s (ReaderPrim i) m) a
forall s (e :: Effect) (t :: Effect) (m :: * -> *) a.
RepresentationalT t =>
t m a -> t (ViaAlg s e m) a
viaAlgT @s @(ReaderPrim i) t m a
m))
{-# INLINE threadReaderPrimViaClass #-}
threadReaderPrimViaRegional :: forall i t m a
. ( Monad m
, MonadTrans t
, ThreadsEff t (Regional ())
)
=> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (t m) a -> t m a
threadReaderPrimViaRegional :: (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (t m) a -> t m a
threadReaderPrimViaRegional forall x. ReaderPrim i m x -> m x
alg ReaderPrim i (t m) a
ReaderPrimAsk = m i -> t m i
forall (t :: Effect) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderPrim i m i -> m i
forall x. ReaderPrim i m x -> m x
alg ReaderPrim i m i
forall i (m :: * -> *). ReaderPrim i m i
ReaderPrimAsk)
threadReaderPrimViaRegional forall x. ReaderPrim i m x -> m x
alg (ReaderPrimLocal i -> i
f t m a
m) =
(forall x. Regional () m x -> m x) -> Regional () (t m) a -> t m a
forall (t :: Effect) (e :: Effect) (m :: * -> *) a.
(ThreadsEff t e, Monad m) =>
(forall x. e m x -> m x) -> e (t m) a -> t m a
threadEff (\(Regionally _ m') -> ReaderPrim i m x -> m x
forall x. ReaderPrim i m x -> m x
alg (ReaderPrim i m x -> m x) -> ReaderPrim i m x -> m x
forall a b. (a -> b) -> a -> b
$ (i -> i) -> m x -> ReaderPrim i m x
forall i (m :: * -> *) a. (i -> i) -> m a -> ReaderPrim i m a
ReaderPrimLocal i -> i
f m x
m') (() -> t m a -> Regional () (t m) a
forall s (m :: * -> *) a. s -> m a -> Regional s m a
Regionally () t m a
m)
{-# INLINE threadReaderPrimViaRegional #-}
#define THREAD_READER(monadT) \
instance ThreadsEff (monadT) (ReaderPrim threadedInput) where \
threadEff = threadReaderPrimViaClass; \
{-# INLINE threadEff #-}
#define THREAD_READER_CTX(ctx, monadT) \
instance ctx => ThreadsEff (monadT) (ReaderPrim threadedInput) where \
threadEff = threadReaderPrimViaClass; \
{-# INLINE threadEff #-}
instance ThreadsEff (ReaderT i') (ReaderPrim i) where
threadEff :: (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (ReaderT i' m) a -> ReaderT i' m a
threadEff = ((forall x. ReaderPrim i m x -> m x)
-> (i -> i) -> ReaderT i' m a -> ReaderT i' m a)
-> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (ReaderT i' m) a
-> ReaderT i' m a
forall i (t :: Effect) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
((forall x. ReaderPrim i m x -> m x) -> (i -> i) -> t m a -> t m a)
-> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (t m) a
-> t m a
threadReaderPrim (((forall x. ReaderPrim i m x -> m x)
-> (i -> i) -> ReaderT i' m a -> ReaderT i' m a)
-> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (ReaderT i' m) a
-> ReaderT i' m a)
-> ((forall x. ReaderPrim i m x -> m x)
-> (i -> i) -> ReaderT i' m a -> ReaderT i' m a)
-> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (ReaderT i' m) a
-> ReaderT i' m a
forall a b. (a -> b) -> a -> b
$ \forall x. ReaderPrim i m x -> m x
alg i -> i
f ReaderT i' m a
m ->
(m a -> m a) -> ReaderT i' m a -> ReaderT i' m a
forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
R.mapReaderT (ReaderPrim i m a -> m a
forall x. ReaderPrim i m x -> m x
alg (ReaderPrim i m a -> m a)
-> (m a -> ReaderPrim i m a) -> m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (i -> i) -> m a -> ReaderPrim i m a
forall i (m :: * -> *) a. (i -> i) -> m a -> ReaderPrim i m a
ReaderPrimLocal i -> i
f) ReaderT i' m a
m
{-# INLINE threadEff #-}
instance Monoid w => ThreadsEff (CPSWr.WriterT w) (ReaderPrim i) where
threadEff :: (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (WriterT w m) a -> WriterT w m a
threadEff = ((forall x. ReaderPrim i m x -> m x)
-> (i -> i) -> WriterT w m a -> WriterT w m a)
-> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (WriterT w m) a
-> WriterT w m a
forall i (t :: Effect) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
((forall x. ReaderPrim i m x -> m x) -> (i -> i) -> t m a -> t m a)
-> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (t m) a
-> t m a
threadReaderPrim (((forall x. ReaderPrim i m x -> m x)
-> (i -> i) -> WriterT w m a -> WriterT w m a)
-> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (WriterT w m) a
-> WriterT w m a)
-> ((forall x. ReaderPrim i m x -> m x)
-> (i -> i) -> WriterT w m a -> WriterT w m a)
-> (forall x. ReaderPrim i m x -> m x)
-> ReaderPrim i (WriterT w m) a
-> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \forall x. ReaderPrim i m x -> m x
alg i -> i
f WriterT w m a
m ->
(m (a, w) -> m (a, w)) -> WriterT w m a -> WriterT w m a
forall (n :: * -> *) w w' (m :: * -> *) a b.
(Monad n, Monoid w, Monoid w') =>
(m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
CPSWr.mapWriterT (ReaderPrim i m (a, w) -> m (a, w)
forall x. ReaderPrim i m x -> m x
alg (ReaderPrim i m (a, w) -> m (a, w))
-> (m (a, w) -> ReaderPrim i m (a, w)) -> m (a, w) -> m (a, w)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (i -> i) -> m (a, w) -> ReaderPrim i m (a, w)
forall i (m :: * -> *) a. (i -> i) -> m a -> ReaderPrim i m a
ReaderPrimLocal i -> i
f) WriterT w m a
m
{-# INLINE threadEff #-}
THREAD_READER(ExceptT e)
THREAD_READER(SSt.StateT s)
THREAD_READER(LSt.StateT s)
THREAD_READER_CTX(Monoid w, LWr.WriterT w)
THREAD_READER_CTX(Monoid w, SWr.WriterT w)
THREAD_READER(C.ContT r)