Is it necessary to have access to the internal structure of a monad to write the monad transformer?
For example: I'd like to have GetT
- transformer for Get
monad from Data.Binary.Get,
but this module doesn't expose internals of Get
monad. Does it mean that the only
way for me is to add GetT
directly to Data.Binary.Get module?
Monad transformers allow developers to compose the effects of different monads, even if the monads themselves are not the same. An example is writing a do-statement that can: abort computation (ExceptT), thread state (StateT), and connect to a database (via a Haskell library such as persistence or esqueleto).
A Monad that can convert any given IO[A] into a F[A] , useful for defining parametric signatures and composing monad transformer stacks.
In general, yes. See in this example how the the inner monad (here the list monad) can “undo” the effect of an “earlier” action of the outer monad:
> execWriterT (tell "Hi" >> tell "Ho" >> lift [()])
["HiHo"]
> execWriterT (tell "Hi" >> tell "Ho" >> lift [])
[]
Now assume you could turn every monad into a monad transformer. Then you would be able to construct a IOT
monad transformer, and this could would launch a missile but then undo it:
> execIOT (launchMissile >> lift [])
Hence it is not possible to turn an arbitrary monad, without looking at the definition, into a monad transformer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With