{-# LANGUAGE DeriveAnyClass, DeriveFunctor, DeriveGeneric, DerivingStrategies, FlexibleContexts, FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses #-}
module Control.Effect.Lift
(
Lift(..)
, sendM
, runM
, LiftC(..)
, Carrier
, Member
, run
) where
import Control.Applicative (Alternative(..))
import Control.Effect.Carrier
import Control.Monad (MonadPlus(..))
import qualified Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.IO.Unlift
import Control.Monad.Trans.Class
import GHC.Generics
newtype Lift sig m k = Lift { Lift sig m k -> sig (m k)
unLift :: sig (m k) }
deriving stock (a -> Lift sig m b -> Lift sig m a
(a -> b) -> Lift sig m a -> Lift sig m b
(forall a b. (a -> b) -> Lift sig m a -> Lift sig m b)
-> (forall a b. a -> Lift sig m b -> Lift sig m a)
-> Functor (Lift sig m)
forall a b. a -> Lift sig m b -> Lift sig m a
forall a b. (a -> b) -> Lift sig m a -> Lift sig m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
forall (sig :: * -> *) (m :: * -> *) a b.
(Functor sig, Functor m) =>
a -> Lift sig m b -> Lift sig m a
forall (sig :: * -> *) (m :: * -> *) a b.
(Functor sig, Functor m) =>
(a -> b) -> Lift sig m a -> Lift sig m b
<$ :: a -> Lift sig m b -> Lift sig m a
$c<$ :: forall (sig :: * -> *) (m :: * -> *) a b.
(Functor sig, Functor m) =>
a -> Lift sig m b -> Lift sig m a
fmap :: (a -> b) -> Lift sig m a -> Lift sig m b
$cfmap :: forall (sig :: * -> *) (m :: * -> *) a b.
(Functor sig, Functor m) =>
(a -> b) -> Lift sig m a -> Lift sig m b
Functor, (forall a. Lift sig m a -> Rep1 (Lift sig m) a)
-> (forall a. Rep1 (Lift sig m) a -> Lift sig m a)
-> Generic1 (Lift sig m)
forall a. Rep1 (Lift sig m) a -> Lift sig m a
forall a. Lift sig m a -> Rep1 (Lift sig m) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
forall (sig :: * -> *) (m :: * -> *) a.
Functor sig =>
Rep1 (Lift sig m) a -> Lift sig m a
forall (sig :: * -> *) (m :: * -> *) a.
Functor sig =>
Lift sig m a -> Rep1 (Lift sig m) a
$cto1 :: forall (sig :: * -> *) (m :: * -> *) a.
Functor sig =>
Rep1 (Lift sig m) a -> Lift sig m a
$cfrom1 :: forall (sig :: * -> *) (m :: * -> *) a.
Functor sig =>
Lift sig m a -> Rep1 (Lift sig m) a
Generic1)
deriving anyclass ((forall (f :: * -> *) a b.
Functor (Lift sig f) =>
(a -> b) -> Lift sig f a -> Lift sig f b)
-> (forall (m :: * -> *) (n :: * -> *) a.
Functor m =>
(forall x. m x -> n x) -> Lift sig m a -> Lift sig n a)
-> HFunctor (Lift sig)
forall (sig :: * -> *) (f :: * -> *) a b.
(Functor sig, Functor (Lift sig f)) =>
(a -> b) -> Lift sig f a -> Lift sig f b
forall (sig :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor sig, Functor m) =>
(forall x. m x -> n x) -> Lift sig m a -> Lift sig n a
forall (f :: * -> *) a b.
Functor (Lift sig f) =>
(a -> b) -> Lift sig f a -> Lift sig f b
forall (m :: * -> *) (n :: * -> *) a.
Functor m =>
(forall x. m x -> n x) -> Lift sig m a -> Lift sig n a
forall (h :: (* -> *) -> * -> *).
(forall (f :: * -> *) a b.
Functor (h f) =>
(a -> b) -> h f a -> h f b)
-> (forall (m :: * -> *) (n :: * -> *) a.
Functor m =>
(forall x. m x -> n x) -> h m a -> h n a)
-> HFunctor h
hmap :: (forall x. m x -> n x) -> Lift sig m a -> Lift sig n a
$chmap :: forall (sig :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor sig, Functor m) =>
(forall x. m x -> n x) -> Lift sig m a -> Lift sig n a
fmap' :: (a -> b) -> Lift sig f a -> Lift sig f b
$cfmap' :: forall (sig :: * -> *) (f :: * -> *) a b.
(Functor sig, Functor (Lift sig f)) =>
(a -> b) -> Lift sig f a -> Lift sig f b
HFunctor, HFunctor (Lift sig)
HFunctor (Lift sig) =>
(forall (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor f, Monad m) =>
f ()
-> (forall x. f (m x) -> n (f x))
-> Lift sig m a
-> Lift sig n (f a))
-> Effect (Lift sig)
forall (sig :: * -> *). Functor sig => HFunctor (Lift sig)
forall (sig :: * -> *) (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor sig, Functor f, Monad m) =>
f ()
-> (forall x. f (m x) -> n (f x))
-> Lift sig m a
-> Lift sig n (f a)
forall (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor f, Monad m) =>
f ()
-> (forall x. f (m x) -> n (f x))
-> Lift sig m a
-> Lift sig n (f a)
forall (sig :: (* -> *) -> * -> *).
HFunctor sig =>
(forall (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor f, Monad m) =>
f () -> (forall x. f (m x) -> n (f x)) -> sig m a -> sig n (f a))
-> Effect sig
handle :: f ()
-> (forall x. f (m x) -> n (f x))
-> Lift sig m a
-> Lift sig n (f a)
$chandle :: forall (sig :: * -> *) (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
(Functor sig, Functor f, Monad m) =>
f ()
-> (forall x. f (m x) -> n (f x))
-> Lift sig m a
-> Lift sig n (f a)
$cp1Effect :: forall (sig :: * -> *). Functor sig => HFunctor (Lift sig)
Effect)
runM :: LiftC m a -> m a
runM :: LiftC m a -> m a
runM = LiftC m a -> m a
forall (m :: * -> *) a. LiftC m a -> m a
runLiftC
sendM :: (Member (Lift n) sig, Carrier sig m, Functor n) => n a -> m a
sendM :: n a -> m a
sendM = Lift n m a -> m a
forall (effect :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member effect sig, Carrier sig m) =>
effect m a -> m a
send (Lift n m a -> m a) -> (n a -> Lift n m a) -> n a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. n (m a) -> Lift n m a
forall (sig :: * -> *) (m :: * -> *) k. sig (m k) -> Lift sig m k
Lift (n (m a) -> Lift n m a) -> (n a -> n (m a)) -> n a -> Lift n m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m a) -> n a -> n (m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
newtype LiftC m a = LiftC { LiftC m a -> m a
runLiftC :: m a }
deriving newtype (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
instance Monad m => Carrier (Lift m) (LiftC m) where
eff :: Lift m (LiftC m) a -> LiftC m a
eff = m a -> LiftC m a
forall (m :: * -> *) a. m a -> LiftC m a
LiftC (m a -> LiftC m a)
-> (Lift m (LiftC m) a -> m a) -> Lift m (LiftC m) a -> LiftC m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (m (LiftC m a) -> (LiftC m a -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LiftC m a -> m a
forall (m :: * -> *) a. LiftC m a -> m a
runLiftC) (m (LiftC m a) -> m a)
-> (Lift m (LiftC m) a -> m (LiftC m a))
-> Lift m (LiftC m) a
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lift m (LiftC m) a -> m (LiftC m a)
forall (sig :: * -> *) (m :: * -> *) k. Lift sig m k -> sig (m k)
unLift
instance MonadUnliftIO m => MonadUnliftIO (LiftC m) where
askUnliftIO :: LiftC m (UnliftIO (LiftC m))
askUnliftIO = m (UnliftIO (LiftC m)) -> LiftC m (UnliftIO (LiftC m))
forall (m :: * -> *) a. m a -> LiftC m a
LiftC (m (UnliftIO (LiftC m)) -> LiftC m (UnliftIO (LiftC m)))
-> m (UnliftIO (LiftC m)) -> LiftC m (UnliftIO (LiftC m))
forall a b. (a -> b) -> a -> b
$ (UnliftIO m -> IO (UnliftIO (LiftC m))) -> m (UnliftIO (LiftC m))
forall (m :: * -> *) a.
MonadUnliftIO m =>
(UnliftIO m -> IO a) -> m a
withUnliftIO ((UnliftIO m -> IO (UnliftIO (LiftC m))) -> m (UnliftIO (LiftC m)))
-> (UnliftIO m -> IO (UnliftIO (LiftC m)))
-> m (UnliftIO (LiftC m))
forall a b. (a -> b) -> a -> b
$ \u :: UnliftIO m
u -> UnliftIO (LiftC m) -> IO (UnliftIO (LiftC m))
forall (m :: * -> *) a. Monad m => a -> m a
return ((forall a. LiftC m a -> IO a) -> UnliftIO (LiftC m)
forall (m :: * -> *). (forall a. m a -> IO a) -> UnliftIO m
UnliftIO (UnliftIO m -> forall a. m a -> IO a
forall (m :: * -> *). UnliftIO m -> forall a. m a -> IO a
unliftIO UnliftIO m
u (m a -> IO a) -> (LiftC m a -> m a) -> LiftC m a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LiftC m a -> m a
forall (m :: * -> *) a. LiftC m a -> m a
runLiftC))
{-# INLINE askUnliftIO #-}
withRunInIO :: ((forall a. LiftC m a -> IO a) -> IO b) -> LiftC m b
withRunInIO inner :: (forall a. LiftC m a -> IO a) -> IO b
inner = m b -> LiftC m b
forall (m :: * -> *) a. m a -> LiftC m a
LiftC (m b -> LiftC m b) -> m b -> LiftC m b
forall a b. (a -> b) -> a -> b
$ ((forall a. m a -> IO a) -> IO b) -> m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO b) -> m b)
-> ((forall a. m a -> IO a) -> IO b) -> m b
forall a b. (a -> b) -> a -> b
$ \run :: forall a. m a -> IO a
run -> (forall a. LiftC m a -> IO a) -> IO b
inner (m a -> IO a
forall a. m a -> IO a
run (m a -> IO a) -> (LiftC m a -> m a) -> LiftC m a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LiftC m a -> m a
forall (m :: * -> *) a. LiftC m a -> m a
runLiftC)
{-# INLINE withRunInIO #-}