new recursive do notation (ghc 6.12.x) spoils layout

Ross Paterson ross at soi.city.ac.uk
Wed Jun 23 08:12:07 EDT 2010


There's also an underlying semantic issue, which is not yet resolved.

The GHC implementation of mdo (following Levent and John's paper)
performs a dependency analysis on the statements in the mdo to apply
mfix to the shortest possible subsegments of statements.  For example,

  mdo
    a <- f b
    b <- g b
    c <- h b
    d <- k d
    return d

is translated to the equivalent of

  do
    (a,b) <- mfix $ \ (a,b) -> do
       a <- f b
       b <- g b
       return (a,b)
    c <- h b
    d <- mfix $ \ d ->
       d <- k d
       return d
    return d

As the User's Guide notes, the choice of segments can change the semantics
with some monads.

When rec was added to GHC, the implementation used the same code and
thus also did automatic segmentation.  The original idea of rec (in
arrow notation) was that it would give the programmer direct control
over these segments: the loop/mfix combinators would go wherever the
programmer put a rec, but I didn't realize Simon had done it the other
way until he mentioned it last October.  So:

 * if rec is to continue to do automatic segmentation, it might as well
   be a modifier on the do keyword (though this would break backward
   compatibility of arrow notation),

 * but I want to argue against automatic segmentation, because I don't
   think the compiler should be making subtle changes to the program's
   semantics on its own.  It would simplify the compiler a bit too.


More information about the Glasgow-haskell-users mailing list