layout rules for "where"

Zhanyong Wan zhanyong.wan@yale.edu
Thu, 07 Feb 2002 14:55:57 -0500


Hi,

The following is a message I sent to haskell.org.  Malcolm Wallace
suggested this is a hugs bug.  I'm hoping you are interested in this.

Regards,

- Zhanyong Wan

################################################

Hi,

I have been a big fan of Haskell's layout rules, but was got by them
lately.

Here's what I was doing.  I wrote something like:

> foo =
>   ...
>   where
>     bar = ...
>
> ...
> -- 600 lines of Haskell code suppressed
>
> baz = ...

Unsurprisingly, when I load this module in Hugs, I can use baz as it is
in the top level.

Later, I had a better implementation for foo, so I no longer needed
the local definition for bar.  I made the change, but unfortunately
forgot to remove the "where" keyword, so the code became:

> foo =
>   ...
>   where
>
> ...
> -- 600 lines of Haskell code suppressed
>
> baz = ...

and it compiled!  However, when I tried to use baz, Hugs complained
that it's undefined.

Why?  Because the compiler thinks that *all the code* (that is, 600+
lines) after the dangling "where" is just local definitions to foo.

I was lucky to spot the source of the problem quickly because I knew I
recently changed that line, but in general, this kind of error can be
very hard to locate.  (In my case, the offending code is 600 lines
away from where the bug is manifested, and the number can be
arbitrarily high.)  I imagine this would be particularly scary to
Haskell beginners and may even turn them away.

Can't we require that a local definition be more indented than the
enclosing definition?  That way my code would've been rejected by the
compiler, and the error message (something like "Empty where-clause on
line xxx") can actually be helpful.

Alternatively, if the compiler warns about all unused local bindings by
default (See also the thread "Why is this legal" on the same mailing
list), it would avoid the vast confusion.

--
# Zhanyong Wan     http://pantheon.yale.edu/~zw23/ ____
# Yale University, Dept of Computer Science       /\___\
# P.O.Box 208285, New Haven, CT 06520-8285        ||___|