[Haskell-cafe] Re: monad subexpressions

Brian Hulley brianh at metamilk.com
Sun Aug 5 23:03:02 EDT 2007

On Fri Aug 3 06:06:31 EDT Neil Mitchell wrote:
 > What are the semantics of
 >      do b >> f (<- a)
 > where does the evaluation of a get lifted to?

I suggest the execution of (a) should be done immediately before the 
action obtained by applying the monadic function whose argument it is 
part of:

       do b >> ( a >>= \ra -> f ra)

Similarly, I'd desugar

       if (<- a) then f (<- b) else g (<- c)


       a >>= \ra -> if ra then (b >>= \rb -> f rb) else (c >>= \rc -> g rc)

I think it would just be confusing to have to think about lines in the 
"do" block since "do" blocks are just syntactic sugar. The only possible 
thing that might be needed by the "do" block is to define what monad the 
action should be evaluated in.

Perhaps the rule could be that if (<- a) occurs in some expression the 
compiler should search for the nearest enclosing "do" block to identify 
which monad (a) should be evaluated in, then should again search 
outwards from the expression (<- a) to find the nearest enclosing 
expression (mexp) which yields a result in that same monad, then desugar 
to (a >>= \ra -> mexp') where mexp' is obtained from mexp by replacing 
that occurrence of (<- a) by (ra). (Evaluations would be done in the 
same order as depth first traversal of their occurrences in mexp)

Regarding objections by others about (<- a) being confused with a 
section, (<-) is not a valid operator therefore it can't be part of a 
section so no confusion should arise imho...

Best regards, Brian.

More information about the Haskell-Cafe mailing list