{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}

-- | A carrier for 'Lift' allowing monadic actions to be lifted from an outer context into an inner one with 'sendM', and for an inner context to run actions in an outer one with 'liftWith'.
--
-- @since 1.0.0.0
module Control.Carrier.Lift
( -- * Lift carrier
  runM
, LiftC(..)
  -- * Lift effect
, module Control.Effect.Lift
) where

import Control.Algebra
import Control.Applicative (Alternative)
import Control.Effect.Lift
import Control.Monad (MonadPlus)
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.IO.Unlift (MonadUnliftIO)

-- | Extract a 'Lift'ed 'Monad'ic action from an effectful computation.
--
-- @since 1.0.0.0
runM :: LiftC m a -> m a
runM :: forall (m :: * -> *) a. LiftC m a -> m a
runM (LiftC m a
m) = m a
m
{-# INLINE runM #-}

-- | @since 1.0.0.0
newtype LiftC m a = LiftC (m a)
  deriving (Applicative (LiftC m)
Applicative (LiftC m) =>
(forall a. LiftC m a)
-> (forall a. LiftC m a -> LiftC m a -> LiftC m a)
-> (forall a. LiftC m a -> LiftC m [a])
-> (forall a. LiftC m a -> LiftC m [a])
-> Alternative (LiftC m)
forall a. LiftC m a
forall a. LiftC m a -> LiftC m [a]
forall a. LiftC m a -> LiftC m a -> LiftC m a
forall (f :: * -> *).
Applicative f =>
(forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
forall (m :: * -> *). Alternative m => Applicative (LiftC m)
forall (m :: * -> *) a. Alternative m => LiftC m a
forall (m :: * -> *) a. Alternative m => LiftC m a -> LiftC m [a]
forall (m :: * -> *) a.
Alternative m =>
LiftC m a -> LiftC m a -> LiftC m a
$cempty :: forall (m :: * -> *) a. Alternative m => LiftC m a
empty :: forall a. LiftC m a
$c<|> :: forall (m :: * -> *) a.
Alternative m =>
LiftC m a -> LiftC m a -> LiftC m a
<|> :: forall a. LiftC m a -> LiftC m a -> LiftC m a
$csome :: forall (m :: * -> *) a. Alternative m => LiftC m a -> LiftC m [a]
some :: forall a. LiftC m a -> LiftC m [a]
$cmany :: forall (m :: * -> *) a. Alternative m => LiftC m a -> LiftC m [a]
many :: forall a. LiftC m a -> LiftC m [a]
Alternative, Functor (LiftC m)
Functor (LiftC m) =>
(forall a. a -> LiftC m a)
-> (forall a b. LiftC m (a -> b) -> LiftC m a -> LiftC m b)
-> (forall a b c.
    (a -> b -> c) -> LiftC m a -> LiftC m b -> LiftC m c)
-> (forall a b. LiftC m a -> LiftC m b -> LiftC m b)
-> (forall a b. LiftC m a -> LiftC m b -> LiftC m a)
-> Applicative (LiftC m)
forall a. a -> LiftC m a
forall a b. LiftC m a -> LiftC m b -> LiftC m a
forall a b. LiftC m a -> LiftC m b -> LiftC m b
forall a b. LiftC m (a -> b) -> LiftC m a -> LiftC m b
forall a b c. (a -> b -> c) -> LiftC m a -> LiftC m b -> LiftC 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
forall (m :: * -> *). Applicative m => Functor (LiftC m)
forall (m :: * -> *) a. Applicative m => a -> LiftC m a
forall (m :: * -> *) a b.
Applicative m =>
LiftC m a -> LiftC m b -> LiftC m a
forall (m :: * -> *) a b.
Applicative m =>
LiftC m a -> LiftC m b -> LiftC m b
forall (m :: * -> *) a b.
Applicative m =>
LiftC m (a -> b) -> LiftC m a -> LiftC m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> LiftC m a -> LiftC m b -> LiftC m c
$cpure :: forall (m :: * -> *) a. Applicative m => a -> LiftC m a
pure :: forall a. a -> LiftC m a
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
LiftC m (a -> b) -> LiftC m a -> LiftC m b
<*> :: forall a b. LiftC m (a -> b) -> LiftC m a -> LiftC m b
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> LiftC m a -> LiftC m b -> LiftC m c
liftA2 :: forall a b c. (a -> b -> c) -> LiftC m a -> LiftC m b -> LiftC m c
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
LiftC m a -> LiftC m b -> LiftC m b
*> :: forall a b. LiftC m a -> LiftC m b -> LiftC m b
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
LiftC m a -> LiftC m b -> LiftC m a
<* :: forall a b. LiftC m a -> LiftC m b -> LiftC m a
Applicative, (forall a b. (a -> b) -> LiftC m a -> LiftC m b)
-> (forall a b. a -> LiftC m b -> LiftC m a) -> Functor (LiftC m)
forall a b. a -> LiftC m b -> LiftC m a
forall a b. (a -> b) -> LiftC m a -> LiftC m b
forall (m :: * -> *) a b. Functor m => a -> LiftC m b -> LiftC m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> LiftC m a -> LiftC m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> LiftC m a -> LiftC m b
fmap :: forall a b. (a -> b) -> LiftC m a -> LiftC m b
$c<$ :: forall (m :: * -> *) a b. Functor m => a -> LiftC m b -> LiftC m a
<$ :: forall a b. a -> LiftC m b -> LiftC m a
Functor, Applicative (LiftC m)
Applicative (LiftC m) =>
(forall a b. LiftC m a -> (a -> LiftC m b) -> LiftC m b)
-> (forall a b. LiftC m a -> LiftC m b -> LiftC m b)
-> (forall a. a -> LiftC m a)
-> Monad (LiftC m)
forall a. a -> LiftC m a
forall a b. LiftC m a -> LiftC m b -> LiftC m b
forall a b. LiftC m a -> (a -> LiftC m b) -> LiftC m b
forall (m :: * -> *). Monad m => Applicative (LiftC m)
forall (m :: * -> *) a. Monad m => a -> LiftC m a
forall (m :: * -> *) a b.
Monad m =>
LiftC m a -> LiftC m b -> LiftC m b
forall (m :: * -> *) a b.
Monad m =>
LiftC m a -> (a -> LiftC m b) -> LiftC 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
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
LiftC m a -> (a -> LiftC m b) -> LiftC m b
>>= :: forall a b. LiftC m a -> (a -> LiftC m b) -> LiftC m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
LiftC m a -> LiftC m b -> LiftC m b
>> :: forall a b. LiftC m a -> LiftC m b -> LiftC m b
$creturn :: forall (m :: * -> *) a. Monad m => a -> LiftC m a
return :: forall a. a -> LiftC m a
Monad, Monad (LiftC m)
Monad (LiftC m) =>
(forall a. String -> LiftC m a) -> MonadFail (LiftC m)
forall a. String -> LiftC m a
forall (m :: * -> *).
Monad m =>
(forall a. String -> m a) -> MonadFail m
forall (m :: * -> *). MonadFail m => Monad (LiftC m)
forall (m :: * -> *) a. MonadFail m => String -> LiftC m a
$cfail :: forall (m :: * -> *) a. MonadFail m => String -> LiftC m a
fail :: forall a. String -> LiftC m a
Fail.MonadFail, Monad (LiftC m)
Monad (LiftC m) =>
(forall a. (a -> LiftC m a) -> LiftC m a) -> MonadFix (LiftC m)
forall a. (a -> LiftC m a) -> LiftC m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
forall (m :: * -> *). MonadFix m => Monad (LiftC m)
forall (m :: * -> *) a. MonadFix m => (a -> LiftC m a) -> LiftC m a
$cmfix :: forall (m :: * -> *) a. MonadFix m => (a -> LiftC m a) -> LiftC m a
mfix :: forall a. (a -> LiftC m a) -> LiftC m a
MonadFix, Monad (LiftC m)
Monad (LiftC m) =>
(forall a. IO a -> LiftC m a) -> MonadIO (LiftC m)
forall a. IO a -> LiftC m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (LiftC m)
forall (m :: * -> *) a. MonadIO m => IO a -> LiftC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> LiftC m a
liftIO :: forall a. IO a -> LiftC m a
MonadIO, Monad (LiftC m)
Alternative (LiftC m)
(Alternative (LiftC m), Monad (LiftC m)) =>
(forall a. LiftC m a)
-> (forall a. LiftC m a -> LiftC m a -> LiftC m a)
-> MonadPlus (LiftC m)
forall a. LiftC m a
forall a. LiftC m a -> LiftC m a -> LiftC m a
forall (m :: * -> *). MonadPlus m => Monad (LiftC m)
forall (m :: * -> *). MonadPlus m => Alternative (LiftC m)
forall (m :: * -> *) a. MonadPlus m => LiftC m a
forall (m :: * -> *) a.
MonadPlus m =>
LiftC m a -> LiftC m a -> LiftC m a
forall (m :: * -> *).
(Alternative m, Monad m) =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m
$cmzero :: forall (m :: * -> *) a. MonadPlus m => LiftC m a
mzero :: forall a. LiftC m a
$cmplus :: forall (m :: * -> *) a.
MonadPlus m =>
LiftC m a -> LiftC m a -> LiftC m a
mplus :: forall a. LiftC m a -> LiftC m a -> LiftC m a
MonadPlus, MonadIO (LiftC m)
MonadIO (LiftC m) =>
(forall b. ((forall a. LiftC m a -> IO a) -> IO b) -> LiftC m b)
-> MonadUnliftIO (LiftC m)
forall b. ((forall a. LiftC m a -> IO a) -> IO b) -> LiftC m b
forall (m :: * -> *).
MonadIO m =>
(forall b. ((forall a. m a -> IO a) -> IO b) -> m b)
-> MonadUnliftIO m
forall (m :: * -> *). MonadUnliftIO m => MonadIO (LiftC m)
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. LiftC m a -> IO a) -> IO b) -> LiftC m b
$cwithRunInIO :: forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. LiftC m a -> IO a) -> IO b) -> LiftC m b
withRunInIO :: forall b. ((forall a. LiftC m a -> IO a) -> IO b) -> LiftC m b
MonadUnliftIO)

instance MonadTrans LiftC where
  lift :: forall (m :: * -> *) a. Monad m => m a -> LiftC m a
lift = m a -> LiftC m a
forall (m :: * -> *) a. m a -> LiftC m a
LiftC
  {-# INLINE lift #-}

instance Monad m => Algebra (Lift m) (LiftC m) where
  alg :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (LiftC m) -> Lift m n a -> ctx () -> LiftC m (ctx a)
alg Handler ctx n (LiftC m)
hdl (LiftWith forall (ctx :: * -> *).
Functor ctx =>
Handler ctx n m -> ctx () -> m (ctx a)
with) = m (ctx a) -> LiftC m (ctx a)
forall (m :: * -> *) a. m a -> LiftC m a
LiftC (m (ctx a) -> LiftC m (ctx a))
-> (ctx () -> m (ctx a)) -> ctx () -> LiftC m (ctx a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handler ctx n m -> ctx () -> m (ctx a)
forall (ctx :: * -> *).
Functor ctx =>
Handler ctx n m -> ctx () -> m (ctx a)
with (LiftC m (ctx x) -> m (ctx x)
forall (m :: * -> *) a. LiftC m a -> m a
runM (LiftC m (ctx x) -> m (ctx x))
-> (ctx (n x) -> LiftC m (ctx x)) -> ctx (n x) -> m (ctx x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ctx (n x) -> LiftC m (ctx x)
Handler ctx n (LiftC m)
hdl)
  {-# INLINE alg #-}