[Haskell-cafe] More Flexible Monad Transformer OR Bad Coding Style

Edward Z. Yang ezyang at MIT.EDU
Mon Aug 9 15:37:55 EDT 2010

Excerpts from aditya siram's message of Mon Aug 09 15:05:14 -0400 2010:
> Until test is called in 'main' we don't know the order of monads. In fact
> even the base monad is not know. All we know is that it uses the State and
> Writer monad. In each call to 'test' in main we can determine the stacking
> order and the base monad yielding different results. This seems to be a more
> flexible way of using monad transformers but I haven't seen this in code
> before so is there anything wrong with this style?

There are two points here:

* You have observed that running the monads in different orders results
  in different values: however, in the case of the examples you've chosen,
  the essential character of the result is still the same.  This is not
  universally true: only some monads "commute", so to speak.  So you definitely
  do not want to leave the ordering implicit if order matters.  But when
  the monads are commutative...

* The general feeling on this issue, as I've heard from Don, is that generic
  types for specifying monad stacks are too fragile: due to the extra generality,
  it's a lot easier to write code that typechecks but doesn't do what you actually
  want it to.  A decent compromise might be to use a concrete stack, and offer
  a utility function that "lifts" it into the generic monad typeclasses.


More information about the Haskell-Cafe mailing list