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 Answer 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.
7 Comments
IO (and in your case Get). You never need information about the inner structure of the monad that you want to wrap. That leads to the question why you want a GetT transformer in the first place, i.e. what do you want to achieve?
Identitymonad in order to obtain the "basic" instance of that monad (e.g. you applyStateTtoIdentityto getState). Beware, I'm not sure of this :)