Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is access to the internal structure of a monad required for a monad transformer?

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?

like image 813
Victor Denisov Avatar asked Aug 03 '12 08:08

Victor Denisov


People also ask

Why use monad transformers?

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).

What is Liftio?

A Monad that can convert any given IO[A] into a F[A] , useful for defining parametric signatures and composing monad transformer stacks.


1 Answers

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.

like image 107
Joachim Breitner Avatar answered Oct 28 '22 14:10

Joachim Breitner