3 ms·
What problems does haskell create How do Monad transformers solve it @jeremyjh
by noloblo 4y ago
What problems does haskell create
How do Monad transformers solve it @jeremyjh
- jeremyjh 4y agoThe main problems are immutability and inability perform I/O outside the IO monad. To address immutability you have Monads like Reader, Writer, State which can give you a similar experience that you would have with mutable data and/or global variables. For example with the state monad you can have code like: runState do value <- get put counter+1 The problem comes when you also want to do IO with that state; if your code is in the IO monad it has no access to the State monad and can't call those methods. There are ways to work around that but monad transformers give you a StateT which can be applied to any base monad, including IO, giving you the capability of both.
- jmaker 4y agoWel IO is State, cf. https://hackage.haskell.org/package/transformers-0.6.1.0/docs/src/Control.Monad.Trans.State.Strict.html#line-262 https://hackage.haskell.org/package/transformers-0.6.1.0/doc... You set up your pipeline in terms of monads and then when you want to execute it you run it in IO. You can generalize IO to MonadIO or define an alias to some transformer pinned at IO. It adds a Computational overhead. Transformers are a solution to monad composition which isn’t naturally possible.
- noloblo 4y agoIs there an example of StateT applied to IO? That's not too complex for python#++ devs?
- jmaker 4y agoNone. Transformers are a solution to the composability of monads.