Monadic bind fixity: do vs (>>)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Monadic bind fixity: do vs (>>)

Mikhail Baykov
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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Monadic bind fixity: do vs (>>)

Ryan Ingram
The desugaring is simpler with the current setup:

do { e }
  => e
do { let p = e; STMTS }
  => let p = e in (do { STMTS })
do { e; STMTS }
  => e >> (do { STMTS })
do { p <- e; STMTS }
  => e >>= \x -> case x of { p -> (do { STMTS }) ; _ -> fail "pattern match failure" }
       [x is a fresh variable]

My guess is that >> is infixl because
  (1) m >>= f >>= g should make sense
  (2) >> should match fixity and precedence with >>=

On Tue, Feb 14, 2012 at 9:50 PM, Michael Baikov <[hidden email]> wrote:
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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe