| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Control.Monad.Trans.Unlift
Description
See overview in the README.md
- 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 where
- class (Applicative b, Applicative m, Monad b, Monad m) => MonadBase b m | m -> b where
- class MonadTrans t => MonadTransControl t where
- class MonadBase b m => MonadBaseControl b m | m -> b where
Trans
class (MonadTransControl t, Forall (Identical t)) => MonadTransUnlift t #
A monad transformer which can be unlifted, obeying the monad morphism laws.
Since 0.1.0
Instances
| (MonadTransControl t, Forall * (Identical t)) => MonadTransUnlift t # | |
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) #
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) #
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 #
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 # | |
newtype UnliftBase b m #
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) #
Get the UnliftBase action for the current transformer stack.
Since 0.1.0
askRunBase :: MonadBaseUnlift b m => m (m a -> b a) #
A simplified version of askUnliftBase which addresses the common case
where polymorphism isn't necessary.
Since 0.1.0
Reexports
class MonadTrans t where #
The class of monad transformers. Instances should satisfy the
following laws, which state that lift is a monad transformation:
Minimal complete definition
Methods
lift :: Monad m => m a -> t m a #
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 (ExceptT e) | |
| MonadTrans (ErrorT e) | |
| MonadTrans (IdentityT *) | |
| MonadTrans (StateT s) | |
| Monoid w => MonadTrans (WriterT w) | |
| MonadTrans (ReaderT * 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 m | m -> b where #
Minimal complete definition
Instances
| MonadBase [] [] | |
| MonadBase Maybe Maybe | |
| MonadBase IO IO | |
| MonadBase Identity Identity | |
| MonadBase STM STM | |
| MonadBase b m => MonadBase b (MaybeT m) | |
| MonadBase b m => MonadBase b (ListT m) | |
| (Monoid w, MonadBase b m) => MonadBase b (WriterT w m) | |
| (Monoid w, MonadBase b m) => MonadBase b (WriterT w m) | |
| MonadBase b m => MonadBase b (StateT s m) | |
| MonadBase b m => MonadBase b (StateT s m) | |
| MonadBase b m => MonadBase b (IdentityT * m) | |
| MonadBase b m => MonadBase b (ExceptT e m) | |
| (Error e, MonadBase b m) => MonadBase b (ErrorT e m) | |
| MonadBase b m => MonadBase b (ReaderT * r m) | |
| MonadBase b m => MonadBase b (ContT * r m) | |
| (Monoid w, MonadBase b m) => MonadBase b (RWST r w s m) | |
| (Monoid w, MonadBase b m) => MonadBase b (RWST r w s m) | |
| MonadBase ((->) r) ((->) r) | |
| MonadBase (Either e) (Either e) | |
| MonadBase (ST s) (ST s) | |
| MonadBase (ST s) (ST s) | |
class MonadTrans t => MonadTransControl t where #
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 MonadTranswithFile
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 MonadTranswithFileLifted
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 :: (* -> *) -> * -> *) a :: * #
Monadic state of t.
The monadic state of a monad transformer is the result type of its run
function, e.g.:
runReaderT::ReaderTr m a -> r -> m aStT(ReaderTr) a ~ arunStateT::StateTs m a -> s -> m (a, s)StT(StateTs) a ~ (a, s)runMaybeT::MaybeTm a -> m (Maybea)StTMaybeTa ~Maybea
Provided type instances:
StTIdentityTa ~ a StTMaybeTa ~Maybea StT (ErrorTe) a ~Errore =>Eithere a StT (ExceptTe) a ~Eithere a StTListTa ~ [a] StT (ReaderTr) a ~ a StT (StateTs) a ~ (a, s) StT (WriterTw) a ~Monoidw => (a, w) StT (RWSTr w s) a ~Monoidw => (a, s, w)
Methods
liftWith :: Monad m => (Run t -> m a) -> t m a #
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 ::Monadm => ((Monadn =>ReaderTr n b -> n b) -> m a) ->ReaderTr m a liftWith f =ReaderT(r -> f (action ->runReaderTaction r)) liftWith ::Monadm => ((Monadn =>StateTs n b -> n (b, s)) -> m a) ->StateTs m a liftWith f =StateT(s ->liftM(x -> (x, s)) (f (action ->runStateTaction s))) liftWith ::Monadm => ((Monadn =>MaybeTn b -> n (Maybeb)) -> m a) ->MaybeTm a liftWith f =MaybeT(liftMJust(frunMaybeT))
restoreT :: Monad m => m (StT t a) -> t m a #
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) ->ReaderTr m a restoreT :: m a ->ReaderTr m a restoreT action =ReaderT{ runReaderT =constaction }StateT:: (s -> m (a, s)) ->StateTs m a restoreT :: m (a, s) ->StateTs m a restoreT action =StateT{ runStateT =constaction }MaybeT:: m (Maybea) ->MaybeTm a restoreT :: m (Maybea) ->MaybeTm a restoreT action =MaybeTaction
Example type signatures:
restoreT ::Monadm => m a ->IdentityTm a restoreT ::Monadm => m (Maybea) ->MaybeTm a restoreT :: (Monadm,Errore) => m (Eithere a) ->ErrorTe m a restoreT ::Monadm => m (Eithere a) ->ExceptTe m a restoreT ::Monadm => m [a] ->ListTm a restoreT ::Monadm => m a ->ReaderTr m a restoreT ::Monadm => m (a, s) ->StateTs m a restoreT :: (Monadm,Monoidw) => m (a, w) ->WriterTw m a restoreT :: (Monadm,Monoidw) => m (a, s, w) ->RWSTr w s m a
Instances
| MonadTransControl ListT | |
| MonadTransControl MaybeT | |
| Monoid w => MonadTransControl (WriterT w) | |
| MonadTransControl (StateT s) | |
| MonadTransControl (ExceptT e) | |
| Error e => MonadTransControl (ErrorT e) | |
| MonadTransControl (IdentityT *) | |
| MonadTransControl (StateT s) | |
| Monoid w => MonadTransControl (WriterT w) | |
| MonadTransControl (ReaderT * r) | |
| Monoid w => MonadTransControl (RWST r w s) | |
| Monoid w => MonadTransControl (RWST r w s) | |
class MonadBase b m => MonadBaseControl b m | m -> b where #
Writing instances
The usual way to write a instance for a transformer
stack over a base monad MonadBaseControlB 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
Minimal complete definition
Associated Types
type StM (m :: * -> *) a :: * #
Monadic state that m adds to the base monad b.
For all base (non-transformed) monads, StM m a ~ a:
StMIOa ~ a StMMaybea ~ a StM (Eithere) a ~ a StM [] a ~ a StM ((->) r) a ~ a StMIdentitya ~ a StMSTMa ~ a StM (STs) a ~ a
If m is a transformed monad, m ~ t b, is the monadic state of
the transformer StMt (given by its StT from MonadTransControl). For a
transformer stack, is defined recursively:StM
StM (IdentityTm) a ~ComposeStIdentityTm a ~ StM m a StM (MaybeTm) a ~ComposeStMaybeTm a ~ StM m (Maybea) StM (ErrorTe m) a ~ComposeStErrorTm a ~Errore => StM m (Eithere a) StM (ExceptTe m) a ~ComposeStExceptTm a ~ StM m (Eithere a) StM (ListTm) a ~ComposeStListTm a ~ StM m [a] StM (ReaderTr m) a ~ComposeStReaderTm a ~ StM m a StM (StateTs m) a ~ComposeStStateTm a ~ StM m (a, s) StM (WriterTw m) a ~ComposeStWriterTm a ~Monoidw => StM m (a, w) StM (RWSTr w s m) a ~ComposeStRWSTm a ~Monoidw => StM m (a, s, w)
Methods
liftBaseWith :: (RunInBase m b -> b a) -> m a #
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
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
Instances