{-# 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 Control.Monad.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 { unLift :: sig (m k) }
deriving stock (Functor, Generic1)
deriving anyclass (HFunctor, Effect)
runM :: LiftC m a -> m a
runM = runLiftC
sendM :: (Member (Lift n) sig, Carrier sig m, Functor n) => n a -> m a
sendM = send . Lift . fmap pure
newtype LiftC m a = LiftC { runLiftC :: m a }
deriving newtype (Alternative, Applicative, Functor, Monad, MonadFail, MonadFix, MonadIO, MonadPlus)
instance MonadTrans LiftC where
lift = LiftC
instance Monad m => Carrier (Lift m) (LiftC m) where
eff = LiftC . (>>= runLiftC) . unLift
instance MonadUnliftIO m => MonadUnliftIO (LiftC m) where
askUnliftIO = LiftC $ withUnliftIO $ \u -> return (UnliftIO (unliftIO u . runLiftC))
{-# INLINE askUnliftIO #-}
withRunInIO inner = LiftC $ withRunInIO $ \run -> inner (run . runLiftC)
{-# INLINE withRunInIO #-}