úÎâ¿"?      !"#$%&'()*+,-./0123456789:;<=>Bas van Dijk, Anders KaseorgBSD3#Bas van Dijk <v.dijk.bas@gmail.com>Safe 9;<=?DORTA function like  that runs a monad transformer t in its base monad b. It is used in .7Handy type synonym that composes the monadic states of t and m.It can be used to define the  for new  instances.A function that runs a m8 computation on the monadic state that was captured by A  RunInBase m4 function yields a computation in the base monad of m$ that returns the monadic state of m/. This state can later be used to restore the m computation using .Example type equalities:  RunInBase (? m) b ~ forall a. ? m a -> b ( m a) RunInBase (@" m) b ~ forall a. @ m a -> b ( m (A a)) RunInBase (B e m) b ~ forall a. C e => B e m a -> b ( m (D e a)) RunInBase (E! e m) b ~ forall a. E e m a -> b ( m (D e a)) RunInBase (F# m) b ~ forall a. F m a -> b ( m [a]) RunInBase (G! r m) b ~ forall a. G m a -> b ( m a) RunInBase (H" s m) b ~ forall a. H s m a -> b ( m (a, s)) RunInBase (I w m) b ~ forall a. J w => I w m a -> b ( m (a, w)) RunInBase (K r w s m) b ~ forall a. J w => K r w s m a -> b ( m (a, s, w)) For a transformed base monad m ~ t b, 'RunInBase m b' ~   t.Writing instancesThe usual way to write a 5 instance for a transformer stack over a base monad 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 , , .Monadic state that m adds to the base monad b.'For all base (non-transformed) monads,  StM m a ~ a: StM L a ~ a StM A a ~ a StM (D8 e) a ~ a StM [] a ~ a StM ((->) r) a ~ a StM M a ~ a StM N a ~ a StM (O s) a ~ a If m is a transformed monad, m ~ t b, * is the monadic state of the transformer t (given by its   from  ). For a transformer stack,  is defined recursively: StM (? m) a ~  ? m a ~ StM m a StM (@ m) a ~  @ m a ~ StM m (A a) StM (B e m) a ~  B m a ~ C e => StM m (D e a) StM (E e m) a ~  E m a ~ StM m (D e a) StM (F m) a ~  F m a ~ StM m [a] StM (G r m) a ~  G m a ~ StM m a StM (H s m) a ~  H m a ~ StM m (a, s) StM (I w m) a ~  I m a ~ J w => StM m (a, w) StM (K r w s m) a ~  K m a ~ J w => StM m (a, s, w)  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 P laws: &liftBaseWith . const . return = return TliftBaseWith (const (m >>= f)) = liftBaseWith (const m) >>= liftBaseWith . const . fThe difference with liftBase. is that before lifting the base computation  liftBaseWith captures the state of m0. It then provides the base computation with a  function that allows running m7 computations in the base monad on the captured state: ÿwwithFileLifted :: 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 1 is usually not implemented directly, but using . Construct a m' computation from the monadic state of m that is returned from a  function.Instances should satisfy: 9liftBaseWith (\runInBase -> runInBase m) >>= restoreM = m1 is usually not implemented directly, but using .A function like   that runs a monad transformer t% which wraps the monad transformers n and n'. This is used in .A function like   that runs a monad transformer t$ which wraps the monad transformer t'. This is used in . )A function that runs a transformed monad t n, on the monadic state that was captured by  A Run t" function yields a computation in n$ that returns the monadic state of t,. This state can later be used to restore a t computation using  .Example type equalities: Run ? ~ forall n b. Q n => ? n b -> n b Run @ ~ forall n b. Q n => @ n b -> n (A b) Run (B e) ~ forall n b. (Q n, C e) => B e n b -> n (D e b) Run (E e) ~ forall n b. Q n => E e n b -> n (D e b) Run F ~ forall n b. Q n => F n b -> n [b] Run (G r) ~ forall n b. Q n => G r n b -> n b Run (H s) ~ forall n b. Q n => H s n b -> n (a, s) Run (I w) ~ forall n b. (Q n, J w) => I w n b -> n (a, w) Run (K r w s) ~ forall n b. (Q n, J w) => K r w s n b -> n (a, s, w) &This type is usually satisfied by the run function of a transformer: flip R :: r -> Run (G r) flip S :: s -> Run (H s) T :: Run @ The MonadTransControl% type class is a stronger version of U: Instances of U know how to lift› actions in the base monad to the transformed monad. These lifted actions, however, are completely unaware of the monadic state added by the transformer. g instances are aware of the monadic state of the transformer and allow to save and restore this state.„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 U0 instances can only lift the return type of the withFile function: –withFileLifted :: MonadTrans t => FilePath -> IOMode -> (Handle -> IO r) -> t IO r withFileLifted file mode action = lift (withFile file mode action)  However, U is not powerful enough to make withFileLifted! accept a function that returns t IOb. 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: Û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 Monadic state of t.CThe monadic state of a monad transformer is the result type of its run function, e.g.: R :: G r m a -> r -> m a   (G r) a ~ a S :: H s m a -> s -> m (a, s)   (H s) a ~ (a, s) T :: @ m a -> m (A a)   @ a ~ A a Provided type instances: StT ? a ~ a StT @ a ~ A a StT (B e) a ~ C e => D e a StT (E e) a ~ D e a StT F a ~ [a] StT (G r) a ~ a StT (H s) a ~ (a, s) StT (I w) a ~ J w => (a, w) StT (K r w s) a ~ J w => (a, s, w) liftWith is similar to liftR in that it lifts a computation from the argument monad to the constructed monad.-Instances should satisfy similar laws as the U laws: "liftWith . const . return = return HliftWith (const (m >>= f)) = liftWith (const m) >>= liftWith . const . fThe difference with lift is that before lifting the m computation liftWith captures the state of t. It then provides the m computation with a   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:  liftWith :: Q m => ((Q n => G r n b -> n b) -> m a) -> G r m a liftWith f = G (r -> f (action -> R action r)) liftWith :: Q m => ((Q n => H s n b -> n (b, s)) -> m a) -> H s m a liftWith f = H (s -> V (x -> (x, s)) (f (action -> S action s))) liftWith :: Q m => ((Q n => @ n b -> n (A b)) -> m a) -> @ m a liftWith f = @ (V Just (f T))  Construct a t' computation from the monadic state of t that is returned from a   function.Instances should satisfy: 2liftWith (\run -> run t) >>= restoreT . return = trestoreTJ is usually implemented through the constructor of the monad transformer: G :: (r -> m a) -> G! r m a restoreT :: m a -> G r m a restoreT action = G { runReaderT = W action } H :: (s -> m (a, s)) -> H& s m a restoreT :: m (a, s) -> H s m a restoreT action = H { runStateT = W action } @ :: m (A a) -> @ m a restoreT :: m (A a) -> @ m a restoreT action = @ action Example type signatures:  restoreT :: Q$ m => m a -> ? m a restoreT :: Q m => m (A a) -> @ m a restoreT :: (Q m, C e) => m (D e a) -> B e m a restoreT :: Q m => m (D e a) -> E e m a restoreT :: Q$ m => m [a] -> F m a restoreT :: Q$ m => m a -> G r m a restoreT :: Q$ m => m (a, s) -> H s m a restoreT :: (Q m, J w) => m (a, w) -> I w m a restoreT :: (Q m, J w) => m (a, s, w) -> K r w s m a Default definition for the   method.Default definition for the   method.Default definition for the   method.Default definition for the   method for double  .Default defintion for the  method.Note that it composes a   of t with a  of m to give a  of t m: defaultLiftBaseWith = \f ->  ) $ \run -> E $ \runInBase -> f $ runInBase . run Default definition for the  method. Note that: defaultRestoreM =   . An often used composition:  control f =  f >>= Example: ÿ-liftedBracket :: MonadBaseControl IO m => m a -> (a -> m b) -> (a -> m c) -> m c liftedBracket acquire release action = control $ \runInBase -> bracket (runInBase acquire) (\saved -> runInBase (restoreM saved >>= release)) (\saved -> runInBase (restoreM saved >>= action)) eEmbed a transformer function as an function in the base monad returning a mutated transformer state.Performs the same function as =, but discards transformer state from the embedded function.*Capture the current state of a transformer.Capture the current state above the base monad liftBaseOp is a particular application of 1 that allows lifting control operations of type: ((a -> b c) -> b c)to: ( b m => (a -> m c) -> m c) For example: "liftBaseOp alloca :: (Storable a,  L m) => (Ptr a -> m c) -> m c liftBaseOp_ is a particular application of 1 that allows lifting control operations of type:  (b a -> b a)to: ( b m => m a -> m a) For example: liftBaseOp_ mask_ ::  L m => m a -> m aliftBaseDiscard is a particular application of 1 that allows lifting control operations of type:  (b () -> b a)to: ( b m => m () -> m a)*Note that, while the argument computation m ()< has access to the captured state, all its side-effects in mG are discarded. It is run only for its side-effects in the base monad b. For example: liftBaseDiscard forkIO ::  L m => m () -> m ThreadIdliftBaseOpDiscard is a particular application of 1 that allows lifting control operations of type: ((a -> b ()) -> b c)to: ( b m => (a -> m ()) -> m c)*Note that, while the argument computation m ()< has access to the captured state, all its side-effects in mG are discarded. It is run only for its side-effects in the base monad b. For example: )liftBaseDiscard (runServer addr port) ::  L m => m () -> m ()Transform an action in t m; using a transformer that operates on the underlying monad m? Monad constructorMonad deconstructorMonad constructorMonad constructorMonad deconstructorMonad constructor !"#$%&'()*+,-./0123456789:;<=>  9   !"#$%&'()*+,-./0123456789:;<=>X      !"#$%&'()*+,-./0123456789:;<=>?@ABCADEFGHAIJAIKFLMANOAPQARSATUAVWFGXAYZ[\]F^_F`aFbcdefFGgARhATiADjAklFGmFGno,monad-control-1.0.2.2-9aTLeaO4El6L4t2OM8GECcControl.Monad.Trans.ControlRunInBaseDefault ComposeSt RunInBaseMonadBaseControlStM liftBaseWithrestoreM RunDefault2 RunDefaultRunMonadTransControlStTliftWithrestoreTdefaultLiftWithdefaultRestoreTdefaultLiftWith2defaultRestoreT2defaultLiftBaseWithdefaultRestoreMcontrolembedembed_captureTcaptureM liftBaseOp liftBaseOp_liftBaseDiscardliftBaseOpDiscard liftThrough$fMonadBaseControlbRWST$fMonadBaseControlbRWST0$fMonadBaseControlbWriterT$fMonadBaseControlbWriterT0$fMonadBaseControlbErrorT$fMonadBaseControlbExceptT$fMonadBaseControlbStateT$fMonadBaseControlbStateT0$fMonadBaseControlbReaderT$fMonadBaseControlbListT$fMonadBaseControlbMaybeT$fMonadBaseControlbIdentityT$fMonadBaseControlSTST$fMonadBaseControlSTST0$fMonadBaseControlSTMSTM"$fMonadBaseControlIdentityIdentity$fMonadBaseControl(->)(->)$fMonadBaseControl[][]$fMonadBaseControlEitherEither$fMonadBaseControlMaybeMaybe$fMonadBaseControlIOIO$fMonadTransControlRWST$fMonadTransControlRWST0$fMonadTransControlWriterT$fMonadTransControlWriterT0$fMonadTransControlStateT$fMonadTransControlStateT0$fMonadTransControlReaderT$fMonadTransControlListT$fMonadTransControlExceptT$fMonadTransControlErrorT$fMonadTransControlMaybeT$fMonadTransControlIdentityTtransformers-0.5.2.0Control.Monad.Trans.Identity IdentityTControl.Monad.Trans.MaybeMaybeTbaseGHC.BaseMaybeControl.Monad.Trans.ErrorErrorTError Data.EitherEitherControl.Monad.Trans.ExceptExceptTControl.Monad.Trans.ListListTControl.Monad.Trans.ReaderReaderTControl.Monad.Trans.State.LazyStateTControl.Monad.Trans.Writer.LazyWriterTMonoidControl.Monad.Trans.RWS.LazyRWSTghc-prim GHC.TypesIOData.Functor.IdentityIdentity GHC.Conc.SyncSTMControl.Monad.ST.Lazy.ImpST.transformers-base-0.4.4-DPTfYkTApgo2fdoO6WqXzcControl.Monad.Base MonadBaseMonad runReaderT runStateT runMaybeTControl.Monad.Trans.Class MonadTransliftMconst