Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Control.Monad.Trans.Unlift
Description
See overview in the README.md
Synopsis
- class (MonadTransControl t, Forall (Identical t)) => MonadTransUnlift t
- newtype Unlift t = Unlift {}
- askUnlift :: forall t m. (MonadTransUnlift t, Monad m) => t m (Unlift t)
- askRun :: (MonadTransUnlift t, Monad (t m), Monad m) => t m (t m a -> m a)
- class (MonadBaseControl b m, Forall (IdenticalBase m)) => MonadBaseUnlift b m | m -> b
- newtype UnliftBase b m = UnliftBase {
- unliftBase :: forall a. m a -> b a
- askUnliftBase :: forall b m. MonadBaseUnlift b m => m (UnliftBase b m)
- askRunBase :: MonadBaseUnlift b m => m (m a -> b a)
- class MonadTrans (t :: (Type -> Type) -> Type -> Type) where
- class (Applicative b, Applicative m, Monad b, Monad m) => MonadBase (b :: Type -> Type) (m :: Type -> Type) | m -> b where
- liftBase :: b α -> m α
- class MonadTrans t => MonadTransControl (t :: (Type -> Type) -> Type -> Type) where
- class MonadBase b m => MonadBaseControl (b :: Type -> Type) (m :: Type -> Type) | m -> b where
Trans
class (MonadTransControl t, Forall (Identical t)) => MonadTransUnlift t Source #
A monad transformer which can be unlifted, obeying the monad morphism laws.
Since 0.1.0
Instances
(MonadTransControl t, Forall (Identical t)) => MonadTransUnlift t Source # | |
Defined in Control.Monad.Trans.Unlift |
A function which can move an action down the monad transformer stack, by providing any necessary environment to the action.
Note that, if ImpredicativeTypes worked reliably, this type wouldn't be
necessary, and askUnlift
would simply include a more generalized type.
Since 0.1.0
askUnlift :: forall t m. (MonadTransUnlift t, Monad m) => t m (Unlift t) Source #
Get the Unlift
action for the current transformer layer.
Since 0.1.0
askRun :: (MonadTransUnlift t, Monad (t m), Monad m) => t m (t m a -> m a) Source #
A simplified version of askUnlift
which addresses the common case where
polymorphism isn't necessary.
Since 0.1.0
Base
class (MonadBaseControl b m, Forall (IdenticalBase m)) => MonadBaseUnlift b m | m -> b Source #
A monad transformer stack which can be unlifted, obeying the monad morphism laws.
Since 0.1.0
Instances
(MonadBaseControl b m, Forall (IdenticalBase m)) => MonadBaseUnlift b m Source # | |
Defined in Control.Monad.Trans.Unlift |
newtype UnliftBase b m Source #
Similar to Unlift
, but instead of moving one layer down the stack, moves
the action to the base monad.
Since 0.1.0
Constructors
UnliftBase | |
Fields
|
askUnliftBase :: forall b m. MonadBaseUnlift b m => m (UnliftBase b m) Source #
Get the UnliftBase
action for the current transformer stack.
Since 0.1.0
askRunBase :: MonadBaseUnlift b m => m (m a -> b a) Source #
A simplified version of askUnliftBase
which addresses the common case
where polymorphism isn't necessary.
Since 0.1.0
Reexports
class MonadTrans (t :: (Type -> Type) -> Type -> Type) where Source #
The class of monad transformers. Instances should satisfy the
following laws, which state that lift
is a monad transformation:
Methods
lift :: Monad m => m a -> t m a Source #
Lift a computation from the argument monad to the constructed monad.
Instances
MonadTrans ListT | |
MonadTrans MaybeT | |
Monoid w => MonadTrans (WriterT w) | |
MonadTrans (StateT s) | |
MonadTrans (ReaderT r) | |
MonadTrans (ExceptT e) | |
MonadTrans (ErrorT e) | |
MonadTrans (IdentityT :: (Type -> Type) -> Type -> Type) | |
MonadTrans (StateT s) | |
Monoid w => MonadTrans (WriterT w) | |
Monoid w => MonadTrans (AccumT w) | |
MonadTrans (SelectT r) | |
MonadTrans (ContT r) | |
Monoid w => MonadTrans (RWST r w s) | |
Monoid w => MonadTrans (RWST r w s) | |
class (Applicative b, Applicative m, Monad b, Monad m) => MonadBase (b :: Type -> Type) (m :: Type -> Type) | m -> b where Source #
Instances
class MonadTrans t => MonadTransControl (t :: (Type -> Type) -> Type -> Type) where Source #
The MonadTransControl
type class is a stronger version of
:MonadTrans
Instances of
know how to MonadTrans
actions in the base monad to
the transformed monad. These lifted actions, however, are completely unaware
of the monadic state added by the transformer.lift
instances are aware of the monadic state of the
transformer and allow to save and restore this state.MonadTransControl
This allows to lift functions that have a monad transformer in both positive and negative position. Take, for example, the function
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
instances can only lift the return type of the MonadTrans
withFile
function:
withFileLifted :: MonadTrans t => FilePath -> IOMode -> (Handle -> IO r) -> t IO r withFileLifted file mode action = lift (withFile file mode action)
However,
is not powerful enough to make MonadTrans
withFileLifted
accept a function that returns t IO
. The reason is that we need to take
away the transformer layer in order to pass the function to
.
withFile
allows us to do this:MonadTransControl
withFileLifted' :: (Monad (t IO), MonadTransControl t) => FilePath -> IOMode -> (Handle -> t IO r) -> t IO r withFileLifted' file mode action = liftWith (\run -> withFile file mode (run . action)) >>= restoreT . return
Associated Types
type StT (t :: (Type -> Type) -> Type -> Type) a :: Type Source #
Monadic state of t
.
The monadic state of a monad transformer is the result type of its run
function, e.g.:
runReaderT
::ReaderT
r m a -> r -> m aStT
(ReaderT
r) a ~ arunStateT
::StateT
s m a -> s -> m (a, s)StT
(StateT
s) a ~ (a, s)runMaybeT
::MaybeT
m a -> m (Maybe
a)StT
MaybeT
a ~Maybe
a
Provided type instances:
StTIdentityT
a ~ a StTMaybeT
a ~Maybe
a StT (ErrorT
e) a ~Error
e =>Either
e a StT (ExceptT
e) a ~Either
e a StTListT
a ~ [a] StT (ReaderT
r) a ~ a StT (StateT
s) a ~ (a, s) StT (WriterT
w) a ~Monoid
w => (a, w) StT (RWST
r w s) a ~Monoid
w => (a, s, w)
Methods
liftWith :: Monad m => (Run t -> m a) -> t m a Source #
liftWith
is similar to lift
in that it lifts a computation from
the argument monad to the constructed monad.
Instances should satisfy similar laws as the MonadTrans
laws:
liftWith . const . return = return
liftWith (const (m >>= f)) = liftWith (const m) >>= liftWith . const . f
The difference with lift
is that before lifting the m
computation
liftWith
captures the state of t
. It then provides the m
computation with a Run
function that allows running t n
computations in
n
(for all n
) on the captured state, e.g.
withFileLifted :: (Monad (t IO), MonadTransControl t) => FilePath -> IOMode -> (Handle -> t IO r) -> t IO r withFileLifted file mode action = liftWith (\run -> withFile file mode (run . action)) >>= restoreT . return
If the Run
function is ignored, liftWith
coincides with lift
:
lift f = liftWith (const f)
Implementations use the
function associated with a transformer:Run
liftWith ::Monad
m => ((Monad
n =>ReaderT
r n b -> n b) -> m a) ->ReaderT
r m a liftWith f =ReaderT
(r -> f (action ->runReaderT
action r)) liftWith ::Monad
m => ((Monad
n =>StateT
s n b -> n (b, s)) -> m a) ->StateT
s m a liftWith f =StateT
(s ->liftM
(x -> (x, s)) (f (action ->runStateT
action s))) liftWith ::Monad
m => ((Monad
n =>MaybeT
n b -> n (Maybe
b)) -> m a) ->MaybeT
m a liftWith f =MaybeT
(liftM
Just
(frunMaybeT
))
restoreT :: Monad m => m (StT t a) -> t m a Source #
Construct a t
computation from the monadic state of t
that is
returned from a Run
function.
Instances should satisfy:
liftWith (\run -> run t) >>= restoreT . return = t
restoreT
is usually implemented through the constructor of the monad
transformer:
ReaderT
:: (r -> m a) ->ReaderT
r m a restoreT :: m a ->ReaderT
r m a restoreT action =ReaderT
{ runReaderT =const
action }StateT
:: (s -> m (a, s)) ->StateT
s m a restoreT :: m (a, s) ->StateT
s m a restoreT action =StateT
{ runStateT =const
action }MaybeT
:: m (Maybe
a) ->MaybeT
m a restoreT :: m (Maybe
a) ->MaybeT
m a restoreT action =MaybeT
action
Example type signatures:
restoreT ::Monad
m => m a ->IdentityT
m a restoreT ::Monad
m => m (Maybe
a) ->MaybeT
m a restoreT :: (Monad
m,Error
e) => m (Either
e a) ->ErrorT
e m a restoreT ::Monad
m => m (Either
e a) ->ExceptT
e m a restoreT ::Monad
m => m [a] ->ListT
m a restoreT ::Monad
m => m a ->ReaderT
r m a restoreT ::Monad
m => m (a, s) ->StateT
s m a restoreT :: (Monad
m,Monoid
w) => m (a, w) ->WriterT
w m a restoreT :: (Monad
m,Monoid
w) => m (a, s, w) ->RWST
r w s m a
Instances
MonadTransControl ListT | |
MonadTransControl MaybeT | |
Monoid w => MonadTransControl (WriterT w) | |
MonadTransControl (StateT s) | |
MonadTransControl (ReaderT r) | |
MonadTransControl (ExceptT e) | |
Error e => MonadTransControl (ErrorT e) | |
MonadTransControl (IdentityT :: (Type -> Type) -> Type -> Type) | |
MonadTransControl (StateT s) | |
Monoid w => MonadTransControl (WriterT w) | |
Monoid w => MonadTransControl (RWST r w s) | |
Monoid w => MonadTransControl (RWST r w s) | |
class MonadBase b m => MonadBaseControl (b :: Type -> Type) (m :: Type -> Type) | m -> b where Source #
Writing instances
The usual way to write a
instance for a transformer
stack over a base monad MonadBaseControl
B
is to write an instance MonadBaseControl B B
for the base monad, and MonadTransControl T
instances for every transformer
T
. Instances for
are then simply implemented using
MonadBaseControl
, ComposeSt
, defaultLiftBaseWith
.defaultRestoreM
Associated Types
type StM (m :: Type -> Type) a :: Type Source #
Monadic state that m
adds to the base monad b
.
For all base (non-transformed) monads, StM m a ~ a
:
StMIO
a ~ a StMMaybe
a ~ a StM (Either
e) a ~ a StM [] a ~ a StM ((->) r) a ~ a StMIdentity
a ~ a StMSTM
a ~ a StM (ST
s) a ~ a
If m
is a transformed monad, m ~ t b
,
is the monadic state of
the transformer StM
t
(given by its StT
from MonadTransControl
). For a
transformer stack,
is defined recursively:StM
StM (IdentityT
m) a ~ComposeSt
IdentityT
m a ~ StM m a StM (MaybeT
m) a ~ComposeSt
MaybeT
m a ~ StM m (Maybe
a) StM (ErrorT
e m) a ~ComposeSt
ErrorT
m a ~Error
e => StM m (Either
e a) StM (ExceptT
e m) a ~ComposeSt
ExceptT
m a ~ StM m (Either
e a) StM (ListT
m) a ~ComposeSt
ListT
m a ~ StM m [a] StM (ReaderT
r m) a ~ComposeSt
ReaderT
m a ~ StM m a StM (StateT
s m) a ~ComposeSt
StateT
m a ~ StM m (a, s) StM (WriterT
w m) a ~ComposeSt
WriterT
m a ~Monoid
w => StM m (a, w) StM (RWST
r w s m) a ~ComposeSt
RWST
m a ~Monoid
w => StM m (a, s, w)
Methods
liftBaseWith :: (RunInBase m b -> b a) -> m a Source #
liftBaseWith
is similar to liftIO
and liftBase
in that it
lifts a base computation to the constructed monad.
Instances should satisfy similar laws as the MonadIO
and MonadBase
laws:
liftBaseWith . const . return = return
liftBaseWith (const (m >>= f)) = liftBaseWith (const m) >>= liftBaseWith . const . f
The difference with liftBase
is that before lifting the base computation
liftBaseWith
captures the state of m
. It then provides the base
computation with a RunInBase
function that allows running m
computations in the base monad on the captured state:
withFileLifted :: MonadBaseControl IO m => FilePath -> IOMode -> (Handle -> m a) -> m a withFileLifted file mode action = liftBaseWith (\runInBase -> withFile file mode (runInBase . action)) >>= restoreM -- = control $ \runInBase -> withFile file mode (runInBase . action) -- = liftBaseOp (withFile file mode) action
is usually not implemented directly, but using
liftBaseWith
.defaultLiftBaseWith
restoreM :: StM m a -> m a Source #
Construct a m
computation from the monadic state of m
that is
returned from a RunInBase
function.
Instances should satisfy:
liftBaseWith (\runInBase -> runInBase m) >>= restoreM = m
is usually not implemented directly, but using
restoreM
.defaultRestoreM