[Haskell] monadic where

John Meacham john at repetae.net
Sun Sep 18 20:53:17 EDT 2005

All the talk of automatic monad lifting has reminded me of an extension
I have wanted on several occasions.

f x y = z where
   a = ...
   b <- ...
   c <- ...

translates too 

f x y = do
    let a =  ..
    b <- ...
    c <- ...

now, this doesn't seem that worth it until you consider guards
in which case it becomes _very_ nice compared to the alternative.

f x y 
   | b > c = ...
   | c <= 0 && a > b = ...
        a = ...
        b <- ...
        c <- ...
f x y = ...

there is really no clean (IMHO) way to express this idiom otherwise, and
the benefits are even greater when you consider pattern guards. Manually
you end up with a lot of strange nested cases or spurious subfunctions
and generally hard to read and write code.

Ideally, it would translate to an 'mdo' rather than a 'do', but not
every monad is an instance of MonadFix and although that would be nice
to have, it is not really vital since 'mdo', although indispensable
sometimes, is not generally needed except in a few special cases so I'd
stick to the standard 'do' translation.


John Meacham - ⑆repetae.net⑆john⑈ 

More information about the Haskell mailing list