{-# LANGUAGE CPP #-}
{-# OPTIONS_HADDOCK not-home #-}
module Control.Effect.Type.Fix
(
Fix(..)
, threadFixViaClass
) where
import Control.Monad.Fix
import qualified Control.Monad.Trans.Except as E
import qualified Control.Monad.Trans.Reader as R
import qualified Control.Monad.Trans.State.Lazy as LSt
import qualified Control.Monad.Trans.State.Strict as SSt
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.Effect.Internal.ViaAlg
import Control.Effect.Internal.Reflection
import Control.Effect.Internal.Utils
import Control.Effect.Internal.Union
newtype Fix :: Effect where
Fix :: (a -> m a) -> Fix m a
instance ( Reifies s (ReifiedEffAlgebra Fix m)
, Monad m
) => MonadFix (ViaAlg s Fix m) where
mfix :: (a -> ViaAlg s Fix m a) -> ViaAlg s Fix m a
mfix a -> ViaAlg s Fix m a
f = case forall a. Reifies s a => a
forall k (s :: k) a. Reifies s a => a
reflect @s of
ReifiedEffAlgebra forall x. Fix m x -> m x
alg -> (Fix m a -> m a) -> Fix (ViaAlg s Fix m) a -> ViaAlg s Fix 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 Fix m a -> m a
forall x. Fix m x -> m x
alg ((a -> ViaAlg s Fix m a) -> Fix (ViaAlg s Fix m) a
forall a (m :: * -> *). (a -> m a) -> Fix m a
Fix a -> ViaAlg s Fix m a
f)
{-# INLINE mfix #-}
threadFixViaClass :: Monad m
=> ( RepresentationalT t
, forall b. MonadFix b => MonadFix (t b)
)
=> (forall x. Fix m x -> m x)
-> Fix (t m) a -> t m a
threadFixViaClass :: (forall x. Fix m x -> m x) -> Fix (t m) a -> t m a
threadFixViaClass forall x. Fix m x -> m x
alg (Fix a -> t m a
f) = ReifiedEffAlgebra Fix m
-> (forall s (pr :: * -> *).
(pr ~ Proxy, Reifies s (ReifiedEffAlgebra Fix 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. Fix m x -> m x) -> ReifiedEffAlgebra Fix m
forall k (e :: (k -> *) -> k -> *) (m :: k -> *).
(forall (x :: k). e m x -> m x) -> ReifiedEffAlgebra e m
ReifiedEffAlgebra forall x. Fix m x -> m x
alg) ((forall s (pr :: * -> *).
(pr ~ Proxy, Reifies s (ReifiedEffAlgebra Fix m)) =>
pr s -> t m a)
-> t m a)
-> (forall s (pr :: * -> *).
(pr ~ Proxy, Reifies s (ReifiedEffAlgebra Fix m)) =>
pr s -> t m a)
-> t m a
forall a b. (a -> b) -> a -> b
$ \(pr s
_ :: pr s) ->
t (ViaAlg s Fix 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 ((a -> t (ViaAlg s Fix m) a) -> t (ViaAlg s Fix m) a
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (forall s (e :: Effect) (t :: Effect) (m :: * -> *) a.
RepresentationalT t =>
t m a -> t (ViaAlg s e m) a
forall (t :: Effect) (m :: * -> *) a.
RepresentationalT t =>
t m a -> t (ViaAlg s Fix m) a
viaAlgT @s @Fix (t m a -> t (ViaAlg s Fix m) a)
-> (a -> t m a) -> a -> t (ViaAlg s Fix m) a
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. a -> t m a
f))
{-# INLINE threadFixViaClass #-}
#define THREADFIX(monadT) \
instance ThreadsEff (monadT) Fix where \
threadEff = threadFixViaClass; \
{-# INLINE threadEff #-}
#define THREADFIX_CTX(ctx, monadT) \
instance ctx => ThreadsEff (monadT) Fix where \
threadEff = threadFixViaClass; \
{-# INLINE threadEff #-}
THREADFIX(LSt.StateT s)
THREADFIX(SSt.StateT s)
THREADFIX_CTX(Monoid s, LWr.WriterT s)
THREADFIX_CTX(Monoid s, SWr.WriterT s)
THREADFIX(CPSWr.WriterT s)
THREADFIX(E.ExceptT e)
THREADFIX(R.ReaderT i)