{-# LANGUAGE CPP
, NoImplicitPrelude
, RankNTypes
, TypeFamilies
, FunctionalDependencies
, FlexibleInstances
, UndecidableInstances
, MultiParamTypeClasses #-}
{-# LANGUAGE Safe #-}
#if MIN_VERSION_transformers(0,4,0)
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
#endif
module Control.Monad.Trans.Control
(
MonadTransControl(..), Run
, RunDefault, defaultLiftWith, defaultRestoreT
, RunDefault2, defaultLiftWith2, defaultRestoreT2
, MonadBaseControl (..), RunInBase
, ComposeSt, RunInBaseDefault, defaultLiftBaseWith, defaultRestoreM
, control, controlT, embed, embed_, captureT, captureM
, liftBaseOp, liftBaseOp_
, liftBaseDiscard, liftBaseOpDiscard
, liftThrough
) where
import Data.Function ( (.), ($), const )
import Data.Monoid ( Monoid, mempty )
import Control.Monad ( Monad, (>>=), return, liftM )
import System.IO ( IO )
import Data.Maybe ( Maybe )
import Data.Either ( Either )
import Control.Monad ( void )
import Prelude ( id )
import Control.Monad.ST.Lazy.Safe ( ST )
import qualified Control.Monad.ST.Safe as Strict ( ST )
import Control.Monad.STM ( STM )
import Control.Monad.Trans.Class ( MonadTrans )
import Control.Monad.Trans.Identity ( IdentityT(IdentityT), runIdentityT )
import Control.Monad.Trans.List ( ListT (ListT), runListT )
import Control.Monad.Trans.Maybe ( MaybeT (MaybeT), runMaybeT )
import Control.Monad.Trans.Error ( ErrorT (ErrorT), runErrorT, Error )
import Control.Monad.Trans.Reader ( ReaderT (ReaderT), runReaderT )
import Control.Monad.Trans.State ( StateT (StateT), runStateT )
import Control.Monad.Trans.Writer ( WriterT (WriterT), runWriterT )
import Control.Monad.Trans.RWS ( RWST (RWST), runRWST )
import Control.Monad.Trans.Except ( ExceptT (ExceptT), runExceptT )
import qualified Control.Monad.Trans.RWS.Strict as Strict ( RWST (RWST), runRWST )
import qualified Control.Monad.Trans.State.Strict as Strict ( StateT (StateT), runStateT )
import qualified Control.Monad.Trans.Writer.Strict as Strict ( WriterT(WriterT), runWriterT )
import Data.Functor.Identity ( Identity )
import Control.Monad.Base ( MonadBase )
class MonadTrans t => MonadTransControl t where
type StT t a :: *
liftWith :: Monad m => (Run t -> m a) -> t m a
restoreT :: Monad m => m (StT t a) -> t m a
type Run t = forall n b. Monad n => t n b -> n (StT t b)
type RunDefault t t' = forall n b. Monad n => t n b -> n (StT t' b)
defaultLiftWith :: (Monad m, MonadTransControl n)
=> (forall b. n m b -> t m b)
-> (forall o b. t o b -> n o b)
-> (RunDefault t n -> m a)
-> t m a
defaultLiftWith :: (forall b. n m b -> t m b)
-> (forall (o :: * -> *) b. t o b -> n o b)
-> (RunDefault t n -> m a)
-> t m a
defaultLiftWith forall b. n m b -> t m b
t forall (o :: * -> *) b. t o b -> n o b
unT = \RunDefault t n -> m a
f -> n m a -> t m a
forall b. n m b -> t m b
t (n m a -> t m a) -> n m a -> t m a
forall a b. (a -> b) -> a -> b
$ (Run n -> m a) -> n m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith ((Run n -> m a) -> n m a) -> (Run n -> m a) -> n m a
forall a b. (a -> b) -> a -> b
$ \Run n
run -> RunDefault t n -> m a
f (RunDefault t n -> m a) -> RunDefault t n -> m a
forall a b. (a -> b) -> a -> b
$ n n b -> n (StT n b)
Run n
run (n n b -> n (StT n b)) -> (t n b -> n n b) -> t n b -> n (StT n b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t n b -> n n b
forall (o :: * -> *) b. t o b -> n o b
unT
{-# INLINABLE defaultLiftWith #-}
defaultRestoreT :: (Monad m, MonadTransControl n)
=> (n m a -> t m a)
-> m (StT n a)
-> t m a
defaultRestoreT :: (n m a -> t m a) -> m (StT n a) -> t m a
defaultRestoreT n m a -> t m a
t = n m a -> t m a
t (n m a -> t m a) -> (m (StT n a) -> n m a) -> m (StT n a) -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (StT n a) -> n m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT
{-# INLINABLE defaultRestoreT #-}
type RunDefault2 t n n' = forall m b. (Monad m, Monad (n' m)) => t m b -> m (StT n' (StT n b))
defaultLiftWith2 :: (Monad m, Monad (n' m), MonadTransControl n, MonadTransControl n')
=> (forall b. n (n' m) b -> t m b)
-> (forall o b. t o b -> n (n' o) b)
-> (RunDefault2 t n n' -> m a)
-> t m a
defaultLiftWith2 :: (forall b. n (n' m) b -> t m b)
-> (forall (o :: * -> *) b. t o b -> n (n' o) b)
-> (RunDefault2 t n n' -> m a)
-> t m a
defaultLiftWith2 forall b. n (n' m) b -> t m b
t forall (o :: * -> *) b. t o b -> n (n' o) b
unT = \RunDefault2 t n n' -> m a
f -> n (n' m) a -> t m a
forall b. n (n' m) b -> t m b
t (n (n' m) a -> t m a) -> n (n' m) a -> t m a
forall a b. (a -> b) -> a -> b
$ (Run n -> n' m a) -> n (n' m) a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith ((Run n -> n' m a) -> n (n' m) a)
-> (Run n -> n' m a) -> n (n' m) a
forall a b. (a -> b) -> a -> b
$ \Run n
run -> (Run n' -> m a) -> n' m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith ((Run n' -> m a) -> n' m a) -> (Run n' -> m a) -> n' m a
forall a b. (a -> b) -> a -> b
$ \Run n'
run' -> RunDefault2 t n n' -> m a
f (RunDefault2 t n n' -> m a) -> RunDefault2 t n n' -> m a
forall a b. (a -> b) -> a -> b
$ n' m (StT n b) -> m (StT n' (StT n b))
Run n'
run' (n' m (StT n b) -> m (StT n' (StT n b)))
-> (t m b -> n' m (StT n b)) -> t m b -> m (StT n' (StT n b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. n (n' m) b -> n' m (StT n b)
Run n
run (n (n' m) b -> n' m (StT n b))
-> (t m b -> n (n' m) b) -> t m b -> n' m (StT n b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t m b -> n (n' m) b
forall (o :: * -> *) b. t o b -> n (n' o) b
unT
{-# INLINABLE defaultLiftWith2 #-}
defaultRestoreT2 :: (Monad m, Monad (n' m), MonadTransControl n, MonadTransControl n')
=> (n (n' m) a -> t m a)
-> m (StT n' (StT n a))
-> t m a
defaultRestoreT2 :: (n (n' m) a -> t m a) -> m (StT n' (StT n a)) -> t m a
defaultRestoreT2 n (n' m) a -> t m a
t = n (n' m) a -> t m a
t (n (n' m) a -> t m a)
-> (m (StT n' (StT n a)) -> n (n' m) a)
-> m (StT n' (StT n a))
-> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. n' m (StT n a) -> n (n' m) a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT (n' m (StT n a) -> n (n' m) a)
-> (m (StT n' (StT n a)) -> n' m (StT n a))
-> m (StT n' (StT n a))
-> n (n' m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (StT n' (StT n a)) -> n' m (StT n a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT
{-# INLINABLE defaultRestoreT2 #-}
instance MonadTransControl IdentityT where
type StT IdentityT a = a
liftWith :: (Run IdentityT -> m a) -> IdentityT m a
liftWith Run IdentityT -> m a
f = m a -> IdentityT m a
forall k (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT (m a -> IdentityT m a) -> m a -> IdentityT m a
forall a b. (a -> b) -> a -> b
$ Run IdentityT -> m a
f (Run IdentityT -> m a) -> Run IdentityT -> m a
forall a b. (a -> b) -> a -> b
$ forall k (f :: k -> *) (a :: k). IdentityT f a -> f a
Run IdentityT
runIdentityT
restoreT :: m (StT IdentityT a) -> IdentityT m a
restoreT = m (StT IdentityT a) -> IdentityT m a
forall k (f :: k -> *) (a :: k). f a -> IdentityT f a
IdentityT
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance MonadTransControl MaybeT where
type StT MaybeT a = Maybe a
liftWith :: (Run MaybeT -> m a) -> MaybeT m a
liftWith Run MaybeT -> m a
f = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe a) -> MaybeT m a) -> m (Maybe a) -> MaybeT m a
forall a b. (a -> b) -> a -> b
$ (a -> Maybe a) -> m a -> m (Maybe a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (m a -> m (Maybe a)) -> m a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ Run MaybeT -> m a
f (Run MaybeT -> m a) -> Run MaybeT -> m a
forall a b. (a -> b) -> a -> b
$ Run MaybeT
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT
restoreT :: m (StT MaybeT a) -> MaybeT m a
restoreT = m (StT MaybeT a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance Error e => MonadTransControl (ErrorT e) where
type StT (ErrorT e) a = Either e a
liftWith :: (Run (ErrorT e) -> m a) -> ErrorT e m a
liftWith Run (ErrorT e) -> m a
f = m (Either e a) -> ErrorT e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT (m (Either e a) -> ErrorT e m a) -> m (Either e a) -> ErrorT e m a
forall a b. (a -> b) -> a -> b
$ (a -> Either e a) -> m a -> m (Either e a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> Either e a
forall (m :: * -> *) a. Monad m => a -> m a
return (m a -> m (Either e a)) -> m a -> m (Either e a)
forall a b. (a -> b) -> a -> b
$ Run (ErrorT e) -> m a
f (Run (ErrorT e) -> m a) -> Run (ErrorT e) -> m a
forall a b. (a -> b) -> a -> b
$ forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
Run (ErrorT e)
runErrorT
restoreT :: m (StT (ErrorT e) a) -> ErrorT e m a
restoreT = m (StT (ErrorT e) a) -> ErrorT e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance MonadTransControl (ExceptT e) where
type StT (ExceptT e) a = Either e a
liftWith :: (Run (ExceptT e) -> m a) -> ExceptT e m a
liftWith Run (ExceptT e) -> m a
f = m (Either e a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e a) -> ExceptT e m a)
-> m (Either e a) -> ExceptT e m a
forall a b. (a -> b) -> a -> b
$ (a -> Either e a) -> m a -> m (Either e a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> Either e a
forall (m :: * -> *) a. Monad m => a -> m a
return (m a -> m (Either e a)) -> m a -> m (Either e a)
forall a b. (a -> b) -> a -> b
$ Run (ExceptT e) -> m a
f (Run (ExceptT e) -> m a) -> Run (ExceptT e) -> m a
forall a b. (a -> b) -> a -> b
$ forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
Run (ExceptT e)
runExceptT
restoreT :: m (StT (ExceptT e) a) -> ExceptT e m a
restoreT = m (StT (ExceptT e) a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance MonadTransControl ListT where
type StT ListT a = [a]
liftWith :: (Run ListT -> m a) -> ListT m a
liftWith Run ListT -> m a
f = m [a] -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (m [a] -> ListT m a) -> m [a] -> ListT m a
forall a b. (a -> b) -> a -> b
$ (a -> [a]) -> m a -> m [a]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return (m a -> m [a]) -> m a -> m [a]
forall a b. (a -> b) -> a -> b
$ Run ListT -> m a
f (Run ListT -> m a) -> Run ListT -> m a
forall a b. (a -> b) -> a -> b
$ Run ListT
forall (m :: * -> *) a. ListT m a -> m [a]
runListT
restoreT :: m (StT ListT a) -> ListT m a
restoreT = m (StT ListT a) -> ListT m a
forall (m :: * -> *) a. m [a] -> ListT m a
ListT
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance MonadTransControl (ReaderT r) where
type StT (ReaderT r) a = a
liftWith :: (Run (ReaderT r) -> m a) -> ReaderT r m a
liftWith Run (ReaderT r) -> m a
f = (r -> m a) -> ReaderT r m a
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((r -> m a) -> ReaderT r m a) -> (r -> m a) -> ReaderT r m a
forall a b. (a -> b) -> a -> b
$ \r
r -> Run (ReaderT r) -> m a
f (Run (ReaderT r) -> m a) -> Run (ReaderT r) -> m a
forall a b. (a -> b) -> a -> b
$ \ReaderT r n b
t -> ReaderT r n b -> r -> n b
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT r n b
t r
r
restoreT :: m (StT (ReaderT r) a) -> ReaderT r m a
restoreT = (r -> m a) -> ReaderT r m a
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((r -> m a) -> ReaderT r m a)
-> (m a -> r -> m a) -> m a -> ReaderT r m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> r -> m a
forall a b. a -> b -> a
const
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance MonadTransControl (StateT s) where
type StT (StateT s) a = (a, s)
liftWith :: (Run (StateT s) -> m a) -> StateT s m a
liftWith Run (StateT s) -> m a
f = (s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT ((s -> m (a, s)) -> StateT s m a)
-> (s -> m (a, s)) -> StateT s m a
forall a b. (a -> b) -> a -> b
$ \s
s ->
(a -> (a, s)) -> m a -> m (a, s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\a
x -> (a
x, s
s))
(Run (StateT s) -> m a
f (Run (StateT s) -> m a) -> Run (StateT s) -> m a
forall a b. (a -> b) -> a -> b
$ \StateT s n b
t -> StateT s n b -> s -> n (b, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s n b
t s
s)
restoreT :: m (StT (StateT s) a) -> StateT s m a
restoreT = (s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT ((s -> m (a, s)) -> StateT s m a)
-> (m (a, s) -> s -> m (a, s)) -> m (a, s) -> StateT s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (a, s) -> s -> m (a, s)
forall a b. a -> b -> a
const
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance MonadTransControl (Strict.StateT s) where
type StT (Strict.StateT s) a = (a, s)
liftWith :: (Run (StateT s) -> m a) -> StateT s m a
liftWith Run (StateT s) -> m a
f = (s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
Strict.StateT ((s -> m (a, s)) -> StateT s m a)
-> (s -> m (a, s)) -> StateT s m a
forall a b. (a -> b) -> a -> b
$ \s
s ->
(a -> (a, s)) -> m a -> m (a, s)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\a
x -> (a
x, s
s))
(Run (StateT s) -> m a
f (Run (StateT s) -> m a) -> Run (StateT s) -> m a
forall a b. (a -> b) -> a -> b
$ \StateT s n b
t -> StateT s n b -> s -> n (b, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
Strict.runStateT StateT s n b
t s
s)
restoreT :: m (StT (StateT s) a) -> StateT s m a
restoreT = (s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
Strict.StateT ((s -> m (a, s)) -> StateT s m a)
-> (m (a, s) -> s -> m (a, s)) -> m (a, s) -> StateT s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (a, s) -> s -> m (a, s)
forall a b. a -> b -> a
const
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance Monoid w => MonadTransControl (WriterT w) where
type StT (WriterT w) a = (a, w)
liftWith :: (Run (WriterT w) -> m a) -> WriterT w m a
liftWith Run (WriterT w) -> m a
f = m (a, w) -> WriterT w m a
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
WriterT (m (a, w) -> WriterT w m a) -> m (a, w) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ (a -> (a, w)) -> m a -> m (a, w)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\a
x -> (a
x, w
forall a. Monoid a => a
mempty))
(Run (WriterT w) -> m a
f (Run (WriterT w) -> m a) -> Run (WriterT w) -> m a
forall a b. (a -> b) -> a -> b
$ forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
Run (WriterT w)
runWriterT)
restoreT :: m (StT (WriterT w) a) -> WriterT w m a
restoreT = m (StT (WriterT w) a) -> WriterT w m a
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
WriterT
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance Monoid w => MonadTransControl (Strict.WriterT w) where
type StT (Strict.WriterT w) a = (a, w)
liftWith :: (Run (WriterT w) -> m a) -> WriterT w m a
liftWith Run (WriterT w) -> m a
f = m (a, w) -> WriterT w m a
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
Strict.WriterT (m (a, w) -> WriterT w m a) -> m (a, w) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ (a -> (a, w)) -> m a -> m (a, w)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\a
x -> (a
x, w
forall a. Monoid a => a
mempty))
(Run (WriterT w) -> m a
f (Run (WriterT w) -> m a) -> Run (WriterT w) -> m a
forall a b. (a -> b) -> a -> b
$ forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
Run (WriterT w)
Strict.runWriterT)
restoreT :: m (StT (WriterT w) a) -> WriterT w m a
restoreT = m (StT (WriterT w) a) -> WriterT w m a
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
Strict.WriterT
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance Monoid w => MonadTransControl (RWST r w s) where
type StT (RWST r w s) a = (a, s, w)
liftWith :: (Run (RWST r w s) -> m a) -> RWST r w s m a
liftWith Run (RWST r w s) -> m a
f = (r -> s -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \r
r s
s -> (a -> (a, s, w)) -> m a -> m (a, s, w)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\a
x -> (a
x, s
s, w
forall a. Monoid a => a
mempty))
(Run (RWST r w s) -> m a
f (Run (RWST r w s) -> m a) -> Run (RWST r w s) -> m a
forall a b. (a -> b) -> a -> b
$ \RWST r w s n b
t -> RWST r w s n b -> r -> s -> n (b, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s n b
t r
r s
s)
restoreT :: m (StT (RWST r w s) a) -> RWST r w s m a
restoreT m (StT (RWST r w s) a)
mSt = (r -> s -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \r
_ s
_ -> m (a, s, w)
m (StT (RWST r w s) a)
mSt
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
instance Monoid w => MonadTransControl (Strict.RWST r w s) where
type StT (Strict.RWST r w s) a = (a, s, w)
liftWith :: (Run (RWST r w s) -> m a) -> RWST r w s m a
liftWith Run (RWST r w s) -> m a
f =
(r -> s -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Strict.RWST ((r -> s -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \r
r s
s -> (a -> (a, s, w)) -> m a -> m (a, s, w)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\a
x -> (a
x, s
s, w
forall a. Monoid a => a
mempty))
(Run (RWST r w s) -> m a
f (Run (RWST r w s) -> m a) -> Run (RWST r w s) -> m a
forall a b. (a -> b) -> a -> b
$ \RWST r w s n b
t -> RWST r w s n b -> r -> s -> n (b, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
Strict.runRWST RWST r w s n b
t r
r s
s)
restoreT :: m (StT (RWST r w s) a) -> RWST r w s m a
restoreT m (StT (RWST r w s) a)
mSt = (r -> s -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Strict.RWST ((r -> s -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \r
_ s
_ -> m (a, s, w)
m (StT (RWST r w s) a)
mSt
{-# INLINABLE liftWith #-}
{-# INLINABLE restoreT #-}
class MonadBase b m => MonadBaseControl b m | m -> b where
type StM m a :: *
liftBaseWith :: (RunInBase m b -> b a) -> m a
restoreM :: StM m a -> m a
type RunInBase m b = forall a. m a -> b (StM m a)
#define BASE(M) \
instance MonadBaseControl (M) (M) where { \
type StM (M) a = a; \
liftBaseWith f = f id; \
restoreM = return; \
{-# INLINABLE liftBaseWith #-}; \
{-# INLINABLE restoreM #-}}
BASE(IO)
BASE(Maybe)
BASE(Either e)
BASE([])
BASE((->) r)
BASE(Identity)
BASE(STM)
BASE(Strict.ST s)
BASE( ST s)
#undef BASE
type ComposeSt t m a = StM m (StT t a)
type RunInBaseDefault t m b = forall a. t m a -> b (ComposeSt t m a)
defaultLiftBaseWith :: (MonadTransControl t, MonadBaseControl b m)
=> (RunInBaseDefault t m b -> b a) -> t m a
defaultLiftBaseWith :: (RunInBaseDefault t m b -> b a) -> t m a
defaultLiftBaseWith = \RunInBaseDefault t m b -> b a
f -> (Run t -> m a) -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith ((Run t -> m a) -> t m a) -> (Run t -> m a) -> t m a
forall a b. (a -> b) -> a -> b
$ \Run t
run ->
(RunInBase m b -> b a) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith ((RunInBase m b -> b a) -> m a) -> (RunInBase m b -> b a) -> m a
forall a b. (a -> b) -> a -> b
$ \RunInBase m b
runInBase ->
RunInBaseDefault t m b -> b a
f (RunInBaseDefault t m b -> b a) -> RunInBaseDefault t m b -> b a
forall a b. (a -> b) -> a -> b
$ m (StT t a) -> b (StM m (StT t a))
RunInBase m b
runInBase (m (StT t a) -> b (StM m (StT t a)))
-> (t m a -> m (StT t a)) -> t m a -> b (StM m (StT t a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t m a -> m (StT t a)
Run t
run
{-# INLINABLE defaultLiftBaseWith #-}
defaultRestoreM :: (MonadTransControl t, MonadBaseControl b m)
=> ComposeSt t m a -> t m a
defaultRestoreM :: ComposeSt t m a -> t m a
defaultRestoreM = m (StT t a) -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT (m (StT t a) -> t m a)
-> (ComposeSt t m a -> m (StT t a)) -> ComposeSt t m a -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ComposeSt t m a -> m (StT t a)
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
StM m a -> m a
restoreM
{-# INLINABLE defaultRestoreM #-}
#define BODY(T) { \
type StM (T m) a = ComposeSt (T) m a; \
liftBaseWith = defaultLiftBaseWith; \
restoreM = defaultRestoreM; \
{-# INLINABLE liftBaseWith #-}; \
{-# INLINABLE restoreM #-}}
#define TRANS( T) \
instance ( MonadBaseControl b m) => MonadBaseControl b (T m) where BODY(T)
#define TRANS_CTX(CTX, T) \
instance (CTX, MonadBaseControl b m) => MonadBaseControl b (T m) where BODY(T)
TRANS(IdentityT)
TRANS(MaybeT)
TRANS(ListT)
TRANS(ReaderT r)
TRANS(Strict.StateT s)
TRANS( StateT s)
TRANS(ExceptT e)
TRANS_CTX(Error e, ErrorT e)
TRANS_CTX(Monoid w, Strict.WriterT w)
TRANS_CTX(Monoid w, WriterT w)
TRANS_CTX(Monoid w, Strict.RWST r w s)
TRANS_CTX(Monoid w, RWST r w s)
#undef BODY
#undef TRANS
#undef TRANS_CTX
control :: MonadBaseControl b m => (RunInBase m b -> b (StM m a)) -> m a
control :: (RunInBase m b -> b (StM m a)) -> m a
control RunInBase m b -> b (StM m a)
f = (RunInBase m b -> b (StM m a)) -> m (StM m a)
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith RunInBase m b -> b (StM m a)
f m (StM m a) -> (StM m a -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= StM m a -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
StM m a -> m a
restoreM
{-# INLINABLE control #-}
controlT :: (MonadTransControl t, Monad (t m), Monad m)
=> (Run t -> m (StT t a)) -> t m a
controlT :: (Run t -> m (StT t a)) -> t m a
controlT Run t -> m (StT t a)
f = (Run t -> m (StT t a)) -> t m (StT t a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith Run t -> m (StT t a)
f t m (StT t a) -> (StT t a -> t m a) -> t m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= m (StT t a) -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT (m (StT t a) -> t m a)
-> (StT t a -> m (StT t a)) -> StT t a -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StT t a -> m (StT t a)
forall (m :: * -> *) a. Monad m => a -> m a
return
{-# INLINABLE controlT #-}
embed :: MonadBaseControl b m => (a -> m c) -> m (a -> b (StM m c))
embed :: (a -> m c) -> m (a -> b (StM m c))
embed a -> m c
f = (RunInBase m b -> b (a -> b (StM m c))) -> m (a -> b (StM m c))
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith ((RunInBase m b -> b (a -> b (StM m c))) -> m (a -> b (StM m c)))
-> (RunInBase m b -> b (a -> b (StM m c))) -> m (a -> b (StM m c))
forall a b. (a -> b) -> a -> b
$ \RunInBase m b
runInBase -> (a -> b (StM m c)) -> b (a -> b (StM m c))
forall (m :: * -> *) a. Monad m => a -> m a
return (m c -> b (StM m c)
RunInBase m b
runInBase (m c -> b (StM m c)) -> (a -> m c) -> a -> b (StM m c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m c
f)
{-# INLINABLE embed #-}
embed_ :: MonadBaseControl b m => (a -> m ()) -> m (a -> b ())
embed_ :: (a -> m ()) -> m (a -> b ())
embed_ a -> m ()
f = (RunInBase m b -> b (a -> b ())) -> m (a -> b ())
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith ((RunInBase m b -> b (a -> b ())) -> m (a -> b ()))
-> (RunInBase m b -> b (a -> b ())) -> m (a -> b ())
forall a b. (a -> b) -> a -> b
$ \RunInBase m b
runInBase -> (a -> b ()) -> b (a -> b ())
forall (m :: * -> *) a. Monad m => a -> m a
return (b (StM m ()) -> b ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (b (StM m ()) -> b ()) -> (a -> b (StM m ())) -> a -> b ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m () -> b (StM m ())
RunInBase m b
runInBase (m () -> b (StM m ())) -> (a -> m ()) -> a -> b (StM m ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m ()
f)
{-# INLINABLE embed_ #-}
captureT :: (MonadTransControl t, Monad (t m), Monad m) => t m (StT t ())
captureT :: t m (StT t ())
captureT = (Run t -> m (StT t ())) -> t m (StT t ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith ((Run t -> m (StT t ())) -> t m (StT t ()))
-> (Run t -> m (StT t ())) -> t m (StT t ())
forall a b. (a -> b) -> a -> b
$ \Run t
runInM -> t m () -> m (StT t ())
Run t
runInM (() -> t m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
{-# INLINABLE captureT #-}
captureM :: MonadBaseControl b m => m (StM m ())
captureM :: m (StM m ())
captureM = (RunInBase m b -> b (StM m ())) -> m (StM m ())
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith ((RunInBase m b -> b (StM m ())) -> m (StM m ()))
-> (RunInBase m b -> b (StM m ())) -> m (StM m ())
forall a b. (a -> b) -> a -> b
$ \RunInBase m b
runInBase -> m () -> b (StM m ())
RunInBase m b
runInBase (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
{-# INLINABLE captureM #-}
liftBaseOp :: MonadBaseControl b m
=> ((a -> b (StM m c)) -> b (StM m d))
-> ((a -> m c) -> m d)
liftBaseOp :: ((a -> b (StM m c)) -> b (StM m d)) -> (a -> m c) -> m d
liftBaseOp (a -> b (StM m c)) -> b (StM m d)
f = \a -> m c
g -> (RunInBase m b -> b (StM m d)) -> m d
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m b -> b (StM m d)) -> m d)
-> (RunInBase m b -> b (StM m d)) -> m d
forall a b. (a -> b) -> a -> b
$ \RunInBase m b
runInBase -> (a -> b (StM m c)) -> b (StM m d)
f ((a -> b (StM m c)) -> b (StM m d))
-> (a -> b (StM m c)) -> b (StM m d)
forall a b. (a -> b) -> a -> b
$ m c -> b (StM m c)
RunInBase m b
runInBase (m c -> b (StM m c)) -> (a -> m c) -> a -> b (StM m c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m c
g
{-# INLINABLE liftBaseOp #-}
liftBaseOp_ :: MonadBaseControl b m
=> (b (StM m a) -> b (StM m c))
-> ( m a -> m c)
liftBaseOp_ :: (b (StM m a) -> b (StM m c)) -> m a -> m c
liftBaseOp_ b (StM m a) -> b (StM m c)
f = \m a
m -> (RunInBase m b -> b (StM m c)) -> m c
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m b -> b (StM m c)) -> m c)
-> (RunInBase m b -> b (StM m c)) -> m c
forall a b. (a -> b) -> a -> b
$ \RunInBase m b
runInBase -> b (StM m a) -> b (StM m c)
f (b (StM m a) -> b (StM m c)) -> b (StM m a) -> b (StM m c)
forall a b. (a -> b) -> a -> b
$ m a -> b (StM m a)
RunInBase m b
runInBase m a
m
{-# INLINABLE liftBaseOp_ #-}
liftBaseDiscard :: MonadBaseControl b m => (b () -> b a) -> (m () -> m a)
liftBaseDiscard :: (b () -> b a) -> m () -> m a
liftBaseDiscard b () -> b a
f = \m ()
m -> (RunInBase m b -> b a) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith ((RunInBase m b -> b a) -> m a) -> (RunInBase m b -> b a) -> m a
forall a b. (a -> b) -> a -> b
$ \RunInBase m b
runInBase -> b () -> b a
f (b () -> b a) -> b () -> b a
forall a b. (a -> b) -> a -> b
$ b (StM m ()) -> b ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (b (StM m ()) -> b ()) -> b (StM m ()) -> b ()
forall a b. (a -> b) -> a -> b
$ m () -> b (StM m ())
RunInBase m b
runInBase m ()
m
{-# INLINABLE liftBaseDiscard #-}
liftBaseOpDiscard :: MonadBaseControl b m
=> ((a -> b ()) -> b c)
-> (a -> m ()) -> m c
liftBaseOpDiscard :: ((a -> b ()) -> b c) -> (a -> m ()) -> m c
liftBaseOpDiscard (a -> b ()) -> b c
f a -> m ()
g = (RunInBase m b -> b c) -> m c
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith ((RunInBase m b -> b c) -> m c) -> (RunInBase m b -> b c) -> m c
forall a b. (a -> b) -> a -> b
$ \RunInBase m b
runInBase -> (a -> b ()) -> b c
f ((a -> b ()) -> b c) -> (a -> b ()) -> b c
forall a b. (a -> b) -> a -> b
$ b (StM m ()) -> b ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (b (StM m ()) -> b ()) -> (a -> b (StM m ())) -> a -> b ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m () -> b (StM m ())
RunInBase m b
runInBase (m () -> b (StM m ())) -> (a -> m ()) -> a -> b (StM m ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m ()
g
{-# INLINABLE liftBaseOpDiscard #-}
liftThrough
:: (MonadTransControl t, Monad (t m), Monad m)
=> (m (StT t a) -> m (StT t b))
-> t m a -> t m b
liftThrough :: (m (StT t a) -> m (StT t b)) -> t m a -> t m b
liftThrough m (StT t a) -> m (StT t b)
f t m a
t = do
StT t b
st <- (Run t -> m (StT t b)) -> t m (StT t b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith ((Run t -> m (StT t b)) -> t m (StT t b))
-> (Run t -> m (StT t b)) -> t m (StT t b)
forall a b. (a -> b) -> a -> b
$ \Run t
run -> do
m (StT t a) -> m (StT t b)
f (m (StT t a) -> m (StT t b)) -> m (StT t a) -> m (StT t b)
forall a b. (a -> b) -> a -> b
$ t m a -> m (StT t a)
Run t
run t m a
t
m (StT t b) -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT (m (StT t b) -> t m b) -> m (StT t b) -> t m b
forall a b. (a -> b) -> a -> b
$ StT t b -> m (StT t b)
forall (m :: * -> *) a. Monad m => a -> m a
return StT t b
st