{-# 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

-- | Extract a 'Lift'ed 'Monad'ic action from an effectful computation.
--
-- @since 1.0.0.0
runM :: LiftC m a -> m a
runM :: 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)
LiftC m a
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)
LiftC m a -> LiftC m a -> LiftC m a
LiftC m a -> LiftC m [a]
LiftC m a -> LiftC m [a]
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 :: LiftC m a -> LiftC m [a]
$cmany :: forall (m :: * -> *) a. Alternative m => LiftC m a -> LiftC m [a]
some :: LiftC m a -> LiftC m [a]
$csome :: forall (m :: * -> *) a. Alternative m => LiftC m a -> LiftC m [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 :: LiftC m a
$cempty :: forall (m :: * -> *) a. Alternative m => LiftC m a
$cp1Alternative :: forall (m :: * -> *). Alternative m => Applicative (LiftC m)
Alternative, Functor (LiftC m)
a -> LiftC m a
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)
LiftC m a -> LiftC m b -> LiftC m b
LiftC m a -> LiftC m b -> LiftC m a
LiftC m (a -> b) -> LiftC m a -> LiftC m b
(a -> b -> c) -> LiftC m a -> LiftC m b -> LiftC m c
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
<* :: 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
*> :: 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 :: (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
<*> :: 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 :: a -> LiftC m a
$cpure :: forall (m :: * -> *) a. Applicative m => a -> LiftC m a
$cp1Applicative :: forall (m :: * -> *). Applicative m => Functor (LiftC m)
Applicative, a -> LiftC m b -> LiftC m a
(a -> b) -> LiftC m a -> LiftC m b
(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
<$ :: a -> LiftC m b -> LiftC m a
$c<$ :: forall (m :: * -> *) a b. Functor m => a -> LiftC m b -> LiftC m a
fmap :: (a -> b) -> LiftC m a -> LiftC m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> LiftC m a -> LiftC m b
Functor, Applicative (LiftC m)
a -> LiftC m a
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)
LiftC m a -> (a -> LiftC m b) -> LiftC m b
LiftC m a -> LiftC m b -> LiftC m b
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 :: a -> LiftC m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> LiftC m a
>> :: 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
>>= :: 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
$cp1Monad :: forall (m :: * -> *). Monad m => Applicative (LiftC m)
Monad, Monad (LiftC m)
Monad (LiftC m)
-> (forall a. String -> LiftC m a) -> MonadFail (LiftC m)
String -> LiftC m a
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 :: String -> LiftC m a
$cfail :: forall (m :: * -> *) a. MonadFail m => String -> LiftC m a
$cp1MonadFail :: forall (m :: * -> *). MonadFail m => Monad (LiftC m)
Fail.MonadFail, Monad (LiftC m)
Monad (LiftC m)
-> (forall a. (a -> LiftC m a) -> LiftC m a) -> MonadFix (LiftC m)
(a -> LiftC m a) -> LiftC m a
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 :: (a -> LiftC m a) -> LiftC m a
$cmfix :: forall (m :: * -> *) a. MonadFix m => (a -> LiftC m a) -> LiftC m a
$cp1MonadFix :: forall (m :: * -> *). MonadFix m => Monad (LiftC m)
MonadFix, Monad (LiftC m)
Monad (LiftC m)
-> (forall a. IO a -> LiftC m a) -> MonadIO (LiftC m)
IO a -> LiftC m a
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 :: IO a -> LiftC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> LiftC m a
$cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (LiftC m)
MonadIO, Monad (LiftC m)
Alternative (LiftC m)
LiftC m a
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)
LiftC m a -> LiftC m a -> LiftC m a
forall a. LiftC m a
forall a. 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
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
mplus :: 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 :: LiftC m a
$cmzero :: forall (m :: * -> *) a. MonadPlus m => LiftC m a
$cp2MonadPlus :: forall (m :: * -> *). MonadPlus m => Monad (LiftC m)
$cp1MonadPlus :: forall (m :: * -> *). MonadPlus m => Alternative (LiftC m)
MonadPlus)

instance MonadTrans LiftC where
  lift :: 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 :: 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 #-}