Is access to the internal structure of a monad req

2019-02-12 14:52发布

问题:

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?

回答1:

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.