Compiler error for Haskell program not understood

Matt Robbins

I'm working on a large Haskell program, so I'm only including the code that seems relevant here to make the problem clearer. Please comment if I should include more.

When I compile, I get only one error message: "parse error (possibly incorrect indentation or mismatched brackets)"

Here is some code with no errors (printStmt) and a very similar section (VarDeclStmt) with the error directed at the line reading, "expr >>= \s ->". I don't see why one would be fine and the other would cause a problem. In what way are they different?

printStmt = 
            keyword "print" >>
            expr >>= \e ->
            symbol ";" >>
            return (Print e)

varDeclStmt = do
              keyword "var" >>
              expr >>= \s -> --ERROR
              symbol "=" >>
              expr >>= \e ->
              return (VarDecl s e) 
chi

This is an indentation issue. The block

varDeclStmt = do
              keyword "var" >>
              expr >>= \s -> --ERROR
              symbol "=" >>
              expr >>= \e ->
              return (VarDecl s e) 

parses as

varDeclStmt = do
              { keyword "var" >>
              ; expr >>= \s -> --ERROR
              ; symbol "=" >>
              ; expr >>= \e ->
              ; return (VarDecl s e) 
              }

which is nonsense, since the first entry keyword "var" >> is not valid.

Note that indenting the whole block further (or less) does not change how it is parsed.

The easiest fix is to remove do completely, so that the text below that is not parsed as a block, hence it is not split into separate entries, but parsed as if it were on a single line.

Otherwise, you can switch to proper do notation

varDeclStmt = do
              keyword "var"
              s <- expr
              symbol "="
              e <- expr
              return (VarDecl s e) 

or (worst solution), make the do block a single-entry one, making the block indented more than the first line, as follos

varDeclStmt = do
              keyword "var" >>
                expr >>= \s ->
                symbol "=" >>
                expr >>= \e ->
                return (VarDecl s e) 

The above solution is silly, though, since the purpose of do is splitting the block into entries, and the indentation is made so that there is only one entry. So, we use two things to counteract each other.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

GLUT Error in Haskell Program

Compiler error but program executes fine

C compiler bug or program error?

Conditional return type not understood by compiler

Program error: undefined member: >>= Haskell

Java program keep running, no compiler's error

Compiler error when trying to run the below program

Compiler throws error "undefined symbol" in C program

Haskell compiler fails while compiling a Modified Alonzo program

C++ program using the haskell compiler when it errors in vs code

Same program gives compiler error in one compiler and not the other

Haskell - exit a program with a specified error code

Error in simple haskell program with generic data type

gcc-5: internal compiler error: Bus error (program as)

why does the compiler give error on the following haskell code?

Haskell: How to fix the "type variable ambigous" compiler error?

Cannot understand compiler error when implement Monad for StateT in haskell

Why does supplying an empty list to this Haskell function give compiler error?

How to fix 'declared but not used' compiler error in this simple program?

no error raised by compiler but not getting the correct answer in character occurrence program

c program segment error from GCC compiler: concatenate two strings

Adding Time Program Compiler Error: cannot find symbol

Program working Fine in one compiler but showing error in codechef

Why generic type parent type is not understood by the compiler in parent constructor

NumPy genfromxt TypeError: data type not understood error

Lighthouse - robots.txt error Syntax not understood

Babel loader error: rest/spread operator not understood

Portable Haskell Compiler

Compiler Integration Tests in Haskell