The result of the monad inside the monad transformer

This is my first introduction to Monad Transformers, so the answer may be obvious.

Say I'm inside a do block of type StateT MyMonad MyType, I want another function of the same type to change state and return a value of type MyMonad MyType. How can i achieve this? I think the examples here show this in guessSession, but I can't figure out how to apply it!

+5
source share
1 answer

If you want to use the main monad in a monad transformer, you can use lift:

lift :: (MonadTrans t, Monad m) => m a -> t m a

In this case, tthere is StateT MyState, and m- MyMonad. So for example:

foo :: StateT MyState MyMonad MyType
foo = do
  modify $ \s -> s+1
  lift $ doSomethingInMyMonad 42

Monad " " , MyMonad MyType ; : , . , StateT s m State s, , lift m StateT s m.

Monad Transformer Library (mtl), StateT, ReaderT .., , lift; , modify ask, - . ( - , StateT s (ReaderT r IO).)

, IO , IO :

liftIO :: (MonadIO m) => IO a -> m a

So liftIO (putStrLn "Hello, world!") IO, StateT Int IO, ContT r (WriterT [String] IO) ..

( foo , - .)

+9