The Do-Let-Braces problem

Simon Marlow
Thu, 15 Feb 2001 12:37:13 -0000

> Mark Utting writes:
> > fb::IO ()
> > fb =3D=20
> >     do {
> >         putStr "Enter Data: ";
> >         line <- getLine;
> >         let line2 =3D line;
> >         putStr line2;
> >        }
> >=20
> > ERROR "f.hs" (line 13): Syntax error in definition=20
> >             (unexpected symbol "putStr") =20
> I find it hard to determine exactly from the Report whether this is
> a bug in Haskell, or just a bug in ghc.  For what it's worth, nhc98
> parses and accepts your example exactly as you intended.

GHC and Hugs both conform to the report here.  The ';' on the end of the
'let' line is interpreted as the terminator for the 'line2 =3D line'
declaration, the layout system inserts a '}' before 'putStr', and
there's a missing semicolon before 'putStr' since we're back in
non-layout mode now. =20

Arguably the layout rule is not clever enough: it only allows one token
of lookahead when deciding whether to insert a close curly or not (in
the above example, if it had two tokens of lookahead it could determine
that the close curly was required before the ';' in the 'let'
declaration).  I'm not seriously suggesting that the layout rule should
be extended to cope with this, rather that since it isn't general enough
we should simplify or throw away altogether the parse error condition in
the layout rule.

In my experience, mixing layout and non-layout in nested contexts
generally leads to trouble: stick to one or the other.