Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monad vs Monad transformers

"Monads allow the programmer to build up computations using sequential building blocks" therefore it allows us to combine some computations. If this is the case, then why the following code can not be run?

import Control.Monad.Trans.State

gt :: State String String
gt = do
   name <- get
   putStrLn "HI" -- Here is the source of problem!
   put "T"
   return ("hh..." ++ name ++ "...!")


main= do
  print $ execState gt "W.."
  print $ evalState gt "W.."
  • Why cannot we put different functions in a monad (like the above example)?

  • Why do we need an additional layer, i.e. transformers to combine the monads?

like image 745
4xx Avatar asked Jan 30 '23 00:01

4xx


1 Answers

Monad transformers are the mechanisms for putting different functions in a monad.

A monad only knows how to combine computations that are within the abilities of that monad. You can't do I/O in a State monad, but you can in a StateT s IO a monad. However, you will need to use liftIO on the computations that do I/O.

import Control.Monad.Trans.State
import Control.Monad.IO.Class (liftIO)

gt :: StateT String IO String
gt = do
   name <- get
   liftIO $ putStrLn "HI"
   put "T"
   return ("hh..." ++ name ++ "...!")


main = do
  print =<< execStateT gt "W.."
  print =<< evalStateT gt "W.."
like image 58
4castle Avatar answered Feb 11 '23 07:02

4castle