Question about error message

Alastair Reid
17 Aug 2002 23:38:37 +0100

> Hello, all There is an error message that I get from GHCI that I do
> not understand.  Usually it is caused by a syntax error or an
> indentation error, but I have occasionally seen it in other contexts
> and I'd like to understand what it is trying to tell me.
> The message is that the last statement in a do construct must be an
> expression (exact text below).

If you have a quick glance at the Haskell report
( section 3.14), you'll
see that do syntax is defined as follows:

  do {e}             = e
  do {e;stmts}       = e >> do {stmts} 
  do {p <- e; stmts} = e >>= \ p -> do {stmts}

(I dropped the error reporting part of the last rule for clarity).

Try applying these rules to one of the programs that produces the error

    main = do{ line <- getLine }

and you'll get:

    main = getLine >>= \ line -> do{}

Now try to eliminate the final do{} and you'll find that there's no
translation - hence the error message.

Alastair Reid         
Reid Consulting (UK) Limited