Search code examples
haskellcabal

cabal build gives parse error, but code works fine in GHCi


My code works in GHCi, but when I try to build the project with cabal build, it gives the error parse error on input ‘<-’

Here's a minimal example:

foo :: IO ()
foo = do
    let x = do
        b <- getLine
        return b
    return ()

Solution

  • It turns out that my GHCi is set up to use the -XNondecreasingIndentation extension, which can be seen with the command :show language

    base language is: Haskell2010
    with the following modifiers:
      -XNoDatatypeContexts
      -XNondecreasingIndentation
    

    Without this extension this is bad syntax:

    foo :: IO ()
    foo = do
        let x = do
            b <- getLine
            return b
        return ()
    

    but this is okay:

    foo :: IO ()
    foo = do
        let 
            x = do
                b <- getLine
                return b
        return ()
    

    To fix the problem, add

      default-extensions: 
        NondecreasingIndentation
    

    to the .cabal file, or if you prefer add {-# language NondecreasingIndentation #-} to just this one module. Alternatively, reformat the code as above, or use Haskell98 instead of Haskell2010.