10

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?

Matt Fenwick
49.3k24 gold badges130 silver badges198 bronze badges
asked Aug 3, 2012 at 8:43
1
  • 2
    I think usually it's the other way around. It's the monad transformer that defines the real implementation of the monad, and then you apply it to the Identity monad in order to obtain the "basic" instance of that monad (e.g. you apply StateT to Identity to get State). Beware, I'm not sure of this :) Commented Aug 3, 2012 at 8:54

1 Answer 1

11

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.

answered Aug 3, 2012 at 11:01
Sign up to request clarification or add additional context in comments.

7 Comments

Do I understand correctly that you give this case as an example where you don't need information about inner structure? But you actually use the fact that inner monad is list.
No; the inner structure in question is that of the monad that you want to turn into a transformer, in this case 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?
I have some code where I communicate over socket using Handler.
If the question is 'is access to the internal structure required to write a monad transformer', wouldn't the answer be 'yes'?
@VictorDenisov - given those requirements, you may want to look into one of the various iteratee flavors (iteratee,conduit,pipes, lots of options). That problem is pretty much directly in their design space.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.