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

instance MonadTrans LiftC where
  lift :: forall (m :: * -> *) a. Monad m => m a -> LiftC m a
lift = 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) = forall (m :: * -> *) a. m a -> LiftC m a
LiftC forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (ctx :: * -> *).
Functor ctx =>
Handler ctx n m -> ctx () -> m (ctx a)
with (forall (m :: * -> *) a. LiftC m a -> m a
runM forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handler ctx n (LiftC m)
hdl)
  {-# INLINE alg #-}