Try this variation on the same theme:
> 
>       mcompare x y = 
>                do i <- x ==~ y 
>                   if i then return EQ else do i <- x <=~ y 
>                   if i then return LT else do return GT
> 
> This code now has a very serious bug!

The code is illegal, isn't it?  The do expression on line 3 doesn't end in
an expression.

> If you use layout properly, as
> with strictly increasing indentation, this confusion is much less
> likely to arise.

It's clear to me now that what one person interprets as easy to read can
just as well be interpreted by someone else as confusing.  For example, I've
been working on the assumption that to a first approximation you can just
look for the last line indented *less* than the current declaration to find
the start of the current binding group.  This is nice and visual, and seems
to me to be the FOWIM approach.   This works (most of the time) for Haskell
98, but wouldn't work if we allowed nested blocks to be back-indented or
even same-indented.

However, this view isn't held by everyone - another popular view is
Malcolm's:

> To put it another way, in the following declaration:
>     class M a where
>     m :: a
>     m = ...
> what is more likely to be the programmer's intention?  A class with an
> empty definition?  Or a class with member m, albeit with a slightly
> unusual interpretation of the basic layout rule?  In the former case,
> the programmer is surely much more likely to write simply
>     class M a
>     m :: a
>     m = ...
> omitting the keyword `where' altogether.

I don't think this is particularly useful, because the example class
declaration would encompass the entire rest of the module.

However, I'm convinced by the argument that it makes no sense to disallow
particular coding styles just because some of the language designers think
they look ugly.  Hence I've modified GHC to allow do-expressions to nest at
the same indentation level - strictly an extension to Haskell 98, but it
doesn't break any legal Haskell 98 programs (this is only for do
expressions, not for where etc.).

> P.S. I think this is even clearer:
>       mcompare x y = 
>                do i <- x ==~ y 
>                   if i then return EQ
>                     else do i <- x <=~ y 
>                             return (if i then LT else GT)
> Now if only the layout rules would allow the `else' to align
> vertically with the `if'!

This is a separate issue and easily fixable - see my original message to the
list on layout.

Cheers,
        Simon


Reply via email to