[Haskell-cafe] Monadic bind fixity: do vs (>>)

Michael Baikov manpacket at gmail.com
Wed Feb 15 06:50:30 CET 2012


Most docs ([1], [2]) about do-notation syntactic sugar tends  to
describe following expressions as equivalent:

"do { a; b; c }"  and "a >> b >> c", but they are not: first one gets
de-sugared into  "a >> (b >> c)", second one is equivalent to "(a >>
b) >> c", because (>>) is declared using infixl.

This should not be a problem, monadic law of Associativity states that
"(m >>= f) >>= g  ≡  m >>= (\x -> f x >>= g)", but this leads to
generating different Core output and may lead to different performance
(and it does, do { Just 4 ; Just 4 ... } is about 2% faster than Just
4 >> Just 4 >> ... if compiled with -O0, but 13% slower when compiled
with -O11)

This also leads to lots of fun when your monad  breaks Associativity law :)

Is there any reasons except for those 13% speed gain for this?

[1]: http://en.wikibooks.org/wiki/Haskell/do_Notation
[2]: http://book.realworldhaskell.org/read/monads.html#monads.dot



More information about the Haskell-Cafe mailing list