{-# LANGUAGE CPP, MagicHash #-}
{-# OPTIONS_HADDOCK not-home #-}
module Control.Effect.Type.Internal.BaseControl where

import Data.Coerce
import GHC.Exts (Proxy#, proxy#)
import Control.Effect.Internal.Union
import Control.Effect.Internal.Utils
import Control.Effect.Internal.Itself
import Control.Effect.Type.Optional
import Control.Monad
import Control.Monad.Base
import Control.Monad.Trans
import Control.Monad.Trans.Control
import Control.Monad.Trans.Except
import Control.Monad.Trans.Reader
import Control.Monad.Trans.State.Strict as SSt
import Control.Monad.Trans.State.Lazy as LSt
import Control.Monad.Trans.Writer.Lazy as LWr
import Control.Monad.Trans.Writer.Strict as SWr
import Control.Monad.Trans.Writer.CPS as CPSWr

-- | A /helper primitive effect/ that allows for lowering computations to a
-- base monad.
--
-- Helper primitive effects are effects that allow you to avoid interpreting one
-- of your own effects as a primitive if the power needed from direct access to
-- the underlying monad can instead be provided by the relevant helper primitive
-- effect. The reason why you'd want to do this is that helper primitive effects
-- already have 'ThreadsEff' instances defined for them; so you don't have to
-- define any for your own effect.
--
-- The helper primitive effects offered in this library are -- in order of
-- ascending power -- 'Control.Effect.Regional.Regional',
-- 'Control.Effect.Optional.Optional', 'Control.Effect.BaseControl.BaseControl'
-- and 'Control.Effect.Unlift.Unlift'.
--
-- __'BaseControl' is typically used as a primitive effect__.
-- If you define a 'Control.Effect.Carrier' that relies on a novel
-- non-trivial monad transformer @t@, then you need to make a
-- a @'ThreadsEff' t ('BaseControl' b)@ instance (if possible).
-- 'threadBaseControlViaClass' can help you with that.
--
-- The following threading constraints accept 'BaseControl':
--
-- * 'Control.Effect.ReaderThreads'
-- * 'Control.Effect.State.StateThreads'
-- * 'Control.Effect.State.StateLazyThreads'
-- * 'Control.Effect.Error.ErrorThreads'
-- * 'Control.Effect.Writer.WriterThreads'
-- * 'Control.Effect.Writer.WriterLazyThreads'
newtype BaseControl b m a where
  GainBaseControl :: (  forall z
                      . (MonadBaseControl b z, Coercible z m)
                     => Proxy# z
                     -> a
                     )
                  -> BaseControl b m a

-- | A valid definition of 'threadEff' for a @'ThreadsEff' t ('BaseControl' b)@
-- instance, given that @t@ lifts @'MonadBaseControl' b@ for any @b@.
threadBaseControlViaClass :: forall b t m a
                           . ( MonadTrans t
                             , Monad m
                             ,    forall z
                                . MonadBaseControl b z
                               => MonadBaseControl b (t z)
                             ,    forall z
                                . Coercible z m
                              => Coercible (t z) (t m)
                             )
                          => (forall x. BaseControl b m x -> m x)
                          -> BaseControl b (t m) a -> t m a
threadBaseControlViaClass :: (forall x. BaseControl b m x -> m x)
-> BaseControl b (t m) a -> t m a
threadBaseControlViaClass forall x. BaseControl b m x -> m x
alg (GainBaseControl forall (z :: * -> *).
(MonadBaseControl b z, Coercible z (t m)) =>
Proxy# z -> a
main) =
  m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> t m a) -> m a -> t m a
forall a b. (a -> b) -> a -> b
$ BaseControl b m a -> m a
forall x. BaseControl b m x -> m x
alg (BaseControl b m a -> m a) -> BaseControl b m a -> m a
forall a b. (a -> b) -> a -> b
$ (forall (z :: * -> *).
 (MonadBaseControl b z, Coercible z m) =>
 Proxy# z -> a)
-> BaseControl b m a
forall (b :: * -> *) (m :: * -> *) a.
(forall (z :: * -> *).
 (MonadBaseControl b z, Coercible z m) =>
 Proxy# z -> a)
-> BaseControl b m a
GainBaseControl ((forall (z :: * -> *).
  (MonadBaseControl b z, Coercible z m) =>
  Proxy# z -> a)
 -> BaseControl b m a)
-> (forall (z :: * -> *).
    (MonadBaseControl b z, Coercible z m) =>
    Proxy# z -> a)
-> BaseControl b m a
forall a b. (a -> b) -> a -> b
$ \(Proxy# z
_ :: Proxy# z) ->
    Proxy# (t z) -> a
forall (z :: * -> *).
(MonadBaseControl b z, Coercible z (t m)) =>
Proxy# z -> a
main (Proxy# (t z)
forall k (a :: k). Proxy# a
proxy# :: Proxy# (t z))
{-# INLINE threadBaseControlViaClass #-}

-- | A valid definition of 'threadEff' for a @'ThreadsEff' t ('Optional' s)@
-- instance, given that @t@ threads @'BaseControl' b@ for any @b@.
threadOptionalViaBaseControl :: forall s t m a
                              . ( Functor s
                                , Monad m
                                , Monad (t m)
                                , ThreadsEff t (BaseControl m)
                                )
                             => (forall x. Optional s m x -> m x)
                             -> Optional s (t m) a -> t m a
threadOptionalViaBaseControl :: (forall x. Optional s m x -> m x) -> Optional s (t m) a -> t m a
threadOptionalViaBaseControl forall x. Optional s m x -> m x
alg (Optionally s a
sa t m a
m) =
    t m (t m a) -> t m a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join
  (t m (t m a) -> t m a) -> t m (t m a) -> t m a
forall a b. (a -> b) -> a -> b
$ (forall x. BaseControl m m x -> m x)
-> BaseControl m (t m) (t m a) -> t m (t m a)
forall (t :: (* -> *) -> * -> *) (e :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(ThreadsEff t e, Monad m) =>
(forall x. e m x -> m x) -> e (t m) a -> t m a
threadEff (\(GainBaseControl main) -> x -> m x
forall (m :: * -> *) a. Monad m => a -> m a
return (x -> m x) -> x -> m x
forall a b. (a -> b) -> a -> b
$ Proxy# (Itself m) -> x
forall (z :: * -> *).
(MonadBaseControl m z, Coercible z m) =>
Proxy# z -> x
main (Proxy# (Itself m)
forall k (a :: k). Proxy# a
proxy# :: Proxy# (Itself m)))
  (BaseControl m (t m) (t m a) -> t m (t m a))
-> BaseControl m (t m) (t m a) -> t m (t m a)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(forall (z :: * -> *).
 (MonadBaseControl m z, Coercible z m) =>
 Proxy# z -> a)
-> BaseControl m m a
forall (b :: * -> *) (m :: * -> *) a.
(forall (z :: * -> *).
 (MonadBaseControl b z, Coercible z m) =>
 Proxy# z -> a)
-> BaseControl b m a
GainBaseControl @m ((forall (z :: * -> *).
  (MonadBaseControl m z, Coercible z (t m)) =>
  Proxy# z -> t m a)
 -> BaseControl m (t m) (t m a))
-> (forall (z :: * -> *).
    (MonadBaseControl m z, Coercible z (t m)) =>
    Proxy# z -> t m a)
-> BaseControl m (t m) (t m a)
forall a b. (a -> b) -> a -> b
$ \(Proxy# z
_ :: Proxy# z) ->
      z a -> t m a
coerce (z a -> t m a) -> z a -> t m a
forall a b. (a -> b) -> a -> b
$ z (z a) -> z a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (z (z a) -> z a) -> z (z a) -> z a
forall a b. (a -> b) -> a -> b
$ (RunInBase z m -> m (z a)) -> z (z a)
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith @m @z @(z a) ((RunInBase z m -> m (z a)) -> z (z a))
-> (RunInBase z m -> m (z a)) -> z (z a)
forall a b. (a -> b) -> a -> b
$ \RunInBase z m
lower -> do
          (Optional s m (z a) -> m (z a)) -> Optional s m (z a) -> m (z a)
forall (n :: * -> *) (m :: * -> *) (e :: (* -> *) -> * -> *) a b.
(Coercible n m, RepresentationalEff e) =>
(e m a -> m b) -> e n a -> n b
coerceAlg Optional s m (z a) -> m (z a)
forall x. Optional s m x -> m x
alg
        (Optional s m (z a) -> m (z a)) -> Optional s m (z a) -> m (z a)
forall a b. (a -> b) -> a -> b
$ s (z a) -> m (z a) -> Optional s m (z a)
forall (s :: * -> *) a (m :: * -> *). s a -> m a -> Optional s m a
Optionally ((a -> z a) -> s a -> s (z a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Applicative z => a -> z a
forall (f :: * -> *) a. Applicative f => a -> f a
pure @z) s a
sa)
                     ((StM z a -> z a) -> m (StM z a) -> m (z a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap StM z a -> z a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
StM m a -> m a
restoreM ((z a -> m (StM z a)) -> t m a -> m (StM z a)
coerce (z a -> m (StM z a)
RunInBase z m
lower @a) t m a
m))
{-# INLINE threadOptionalViaBaseControl #-}


#define THREAD_BASE_CONTROL(monadT)                \
instance ThreadsEff (monadT) (BaseControl b) where \
  threadEff = threadBaseControlViaClass;           \
  {-# INLINE threadEff #-}

#define THREAD_BASE_CONTROL_CTX(ctx, monadT)              \
instance ctx => ThreadsEff (monadT) (BaseControl b) where \
  threadEff = threadBaseControlViaClass;                  \
  {-# INLINE threadEff #-}

THREAD_BASE_CONTROL(ReaderT i)
THREAD_BASE_CONTROL(ExceptT e)
THREAD_BASE_CONTROL(LSt.StateT s)
THREAD_BASE_CONTROL(SSt.StateT s)
THREAD_BASE_CONTROL_CTX(Monoid w, LWr.WriterT w)
THREAD_BASE_CONTROL_CTX(Monoid w, SWr.WriterT w)

-- monad-control still doesn't have a MonadBaseControl instance for CPS
-- WriterT, so we use a work-around to make this instance.
instance Monoid w => ThreadsEff (CPSWr.WriterT w) (BaseControl b) where
  threadEff :: (forall x. BaseControl b m x -> m x)
-> BaseControl b (WriterT w m) a -> WriterT w m a
threadEff forall x. BaseControl b m x -> m x
alg (GainBaseControl forall (z :: * -> *).
(MonadBaseControl b z, Coercible z (WriterT w m)) =>
Proxy# z -> a
main) =
    m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a) -> m a -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ BaseControl b m a -> m a
forall x. BaseControl b m x -> m x
alg (BaseControl b m a -> m a) -> BaseControl b m a -> m a
forall a b. (a -> b) -> a -> b
$ (forall (z :: * -> *).
 (MonadBaseControl b z, Coercible z m) =>
 Proxy# z -> a)
-> BaseControl b m a
forall (b :: * -> *) (m :: * -> *) a.
(forall (z :: * -> *).
 (MonadBaseControl b z, Coercible z m) =>
 Proxy# z -> a)
-> BaseControl b m a
GainBaseControl ((forall (z :: * -> *).
  (MonadBaseControl b z, Coercible z m) =>
  Proxy# z -> a)
 -> BaseControl b m a)
-> (forall (z :: * -> *).
    (MonadBaseControl b z, Coercible z m) =>
    Proxy# z -> a)
-> BaseControl b m a
forall a b. (a -> b) -> a -> b
$ \(Proxy# z
_ :: Proxy# z) ->
      Proxy# (WriterCPS w z) -> a
forall (z :: * -> *).
(MonadBaseControl b z, Coercible z (WriterT w m)) =>
Proxy# z -> a
main (Proxy# (WriterCPS w z)
forall k (a :: k). Proxy# a
proxy# :: Proxy# (WriterCPS w z))
  {-# INLINE threadEff #-}


newtype WriterCPS s m a = WriterCPS { WriterCPS s m a -> WriterT s m a
unWriterCPS :: CPSWr.WriterT s m a }
  deriving (a -> WriterCPS s m b -> WriterCPS s m a
(a -> b) -> WriterCPS s m a -> WriterCPS s m b
(forall a b. (a -> b) -> WriterCPS s m a -> WriterCPS s m b)
-> (forall a b. a -> WriterCPS s m b -> WriterCPS s m a)
-> Functor (WriterCPS s m)
forall a b. a -> WriterCPS s m b -> WriterCPS s m a
forall a b. (a -> b) -> WriterCPS s m a -> WriterCPS s m b
forall s (m :: * -> *) a b.
Functor m =>
a -> WriterCPS s m b -> WriterCPS s m a
forall s (m :: * -> *) a b.
Functor m =>
(a -> b) -> WriterCPS s m a -> WriterCPS s m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> WriterCPS s m b -> WriterCPS s m a
$c<$ :: forall s (m :: * -> *) a b.
Functor m =>
a -> WriterCPS s m b -> WriterCPS s m a
fmap :: (a -> b) -> WriterCPS s m a -> WriterCPS s m b
$cfmap :: forall s (m :: * -> *) a b.
Functor m =>
(a -> b) -> WriterCPS s m a -> WriterCPS s m b
Functor, Functor (WriterCPS s m)
a -> WriterCPS s m a
Functor (WriterCPS s m)
-> (forall a. a -> WriterCPS s m a)
-> (forall a b.
    WriterCPS s m (a -> b) -> WriterCPS s m a -> WriterCPS s m b)
-> (forall a b c.
    (a -> b -> c)
    -> WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m c)
-> (forall a b.
    WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b)
-> (forall a b.
    WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m a)
-> Applicative (WriterCPS s m)
WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b
WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m a
WriterCPS s m (a -> b) -> WriterCPS s m a -> WriterCPS s m b
(a -> b -> c)
-> WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m c
forall a. a -> WriterCPS s m a
forall a b. WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m a
forall a b. WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b
forall a b.
WriterCPS s m (a -> b) -> WriterCPS s m a -> WriterCPS s m b
forall a b c.
(a -> b -> c)
-> WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m c
forall s (m :: * -> *). Monad m => Functor (WriterCPS s m)
forall s (m :: * -> *) a. Monad m => a -> WriterCPS s m a
forall s (m :: * -> *) a b.
Monad m =>
WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m a
forall s (m :: * -> *) a b.
Monad m =>
WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b
forall s (m :: * -> *) a b.
Monad m =>
WriterCPS s m (a -> b) -> WriterCPS s m a -> WriterCPS s m b
forall s (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m a
$c<* :: forall s (m :: * -> *) a b.
Monad m =>
WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m a
*> :: WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b
$c*> :: forall s (m :: * -> *) a b.
Monad m =>
WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b
liftA2 :: (a -> b -> c)
-> WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m c
$cliftA2 :: forall s (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m c
<*> :: WriterCPS s m (a -> b) -> WriterCPS s m a -> WriterCPS s m b
$c<*> :: forall s (m :: * -> *) a b.
Monad m =>
WriterCPS s m (a -> b) -> WriterCPS s m a -> WriterCPS s m b
pure :: a -> WriterCPS s m a
$cpure :: forall s (m :: * -> *) a. Monad m => a -> WriterCPS s m a
$cp1Applicative :: forall s (m :: * -> *). Monad m => Functor (WriterCPS s m)
Applicative, Applicative (WriterCPS s m)
a -> WriterCPS s m a
Applicative (WriterCPS s m)
-> (forall a b.
    WriterCPS s m a -> (a -> WriterCPS s m b) -> WriterCPS s m b)
-> (forall a b.
    WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b)
-> (forall a. a -> WriterCPS s m a)
-> Monad (WriterCPS s m)
WriterCPS s m a -> (a -> WriterCPS s m b) -> WriterCPS s m b
WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b
forall a. a -> WriterCPS s m a
forall a b. WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b
forall a b.
WriterCPS s m a -> (a -> WriterCPS s m b) -> WriterCPS s m b
forall s (m :: * -> *). Monad m => Applicative (WriterCPS s m)
forall s (m :: * -> *) a. Monad m => a -> WriterCPS s m a
forall s (m :: * -> *) a b.
Monad m =>
WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b
forall s (m :: * -> *) a b.
Monad m =>
WriterCPS s m a -> (a -> WriterCPS s m b) -> WriterCPS s m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> WriterCPS s m a
$creturn :: forall s (m :: * -> *) a. Monad m => a -> WriterCPS s m a
>> :: WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b
$c>> :: forall s (m :: * -> *) a b.
Monad m =>
WriterCPS s m a -> WriterCPS s m b -> WriterCPS s m b
>>= :: WriterCPS s m a -> (a -> WriterCPS s m b) -> WriterCPS s m b
$c>>= :: forall s (m :: * -> *) a b.
Monad m =>
WriterCPS s m a -> (a -> WriterCPS s m b) -> WriterCPS s m b
$cp1Monad :: forall s (m :: * -> *). Monad m => Applicative (WriterCPS s m)
Monad)
  deriving m a -> WriterCPS s m a
(forall (m :: * -> *) a. Monad m => m a -> WriterCPS s m a)
-> MonadTrans (WriterCPS s)
forall s (m :: * -> *) a. Monad m => m a -> WriterCPS s m a
forall (m :: * -> *) a. Monad m => m a -> WriterCPS s m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> WriterCPS s m a
$clift :: forall s (m :: * -> *) a. Monad m => m a -> WriterCPS s m a
MonadTrans

instance MonadBase b m => MonadBase b (WriterCPS s m) where
  liftBase :: b α -> WriterCPS s m α
liftBase = m α -> WriterCPS s m α
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m α -> WriterCPS s m α) -> (b α -> m α) -> b α -> WriterCPS s m α
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b α -> m α
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase
  {-# INLINE liftBase #-}

instance (Monoid s, MonadBaseControl b m)
      => MonadBaseControl b (WriterCPS s m) where
  type StM (WriterCPS s m) a = StM m (a, s)

  liftBaseWith :: (RunInBase (WriterCPS s m) b -> b a) -> WriterCPS s m a
liftBaseWith RunInBase (WriterCPS s m) b -> b a
main = m a -> WriterCPS s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterCPS s m a) -> m a -> WriterCPS s m a
forall a b. (a -> b) -> a -> b
$ (RunInBase m b -> b a) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith ((RunInBase m b -> b a) -> m a) -> (RunInBase m b -> b a) -> m a
forall a b. (a -> b) -> a -> b
$ \RunInBase m b
run_it ->
    RunInBase (WriterCPS s m) b -> b a
main (m (a, s) -> b (StM m (a, s))
RunInBase m b
run_it (m (a, s) -> b (StM m (a, s)))
-> (WriterT s m a -> m (a, s)) -> WriterT s m a -> b (StM m (a, s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterT s m a -> m (a, s)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
CPSWr.runWriterT (WriterT s m a -> b (StM m (a, s)))
-> (WriterCPS s m a -> WriterT s m a)
-> WriterCPS s m a
-> b (StM m (a, s))
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# WriterCPS s m a -> WriterT s m a
forall s (m :: * -> *) a. WriterCPS s m a -> WriterT s m a
unWriterCPS)
  {-# INLINE liftBaseWith #-}

  restoreM :: StM (WriterCPS s m) a -> WriterCPS s m a
restoreM = WriterT s m a -> WriterCPS s m a
forall s (m :: * -> *) a. WriterT s m a -> WriterCPS s m a
WriterCPS (WriterT s m a -> WriterCPS s m a)
-> (StM m (a, s) -> WriterT s m a)
-> StM m (a, s)
-> WriterCPS s m a
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. m (a, s) -> WriterT s m a
forall (m :: * -> *) w a.
(Functor m, Monoid w) =>
m (a, w) -> WriterT w m a
CPSWr.writerT (m (a, s) -> WriterT s m a)
-> (StM m (a, s) -> m (a, s)) -> StM m (a, s) -> WriterT s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StM m (a, s) -> m (a, s)
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
StM m a -> m a
restoreM
  {-# INLINE restoreM #-}