cabal 构建给出解析错误,但代码在 GHCi 中运行良好

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

我的代码在 GHCi 中有效,但是当我尝试使用 cabal build 构建项目时,它给出了错误 parse error on input ‘<-’

这是一个最小的例子:

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

原来我的GHCi设置了使用-XNondecreasingIndentation扩展,用命令:show language

可以看到
base language is: Haskell2010
with the following modifiers:
  -XNoDatatypeContexts
  -XNondecreasingIndentation

没有这个扩展,这是错误的语法:

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

但这没关系:

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

要解决此问题,请添加

  default-extensions: 
    NondecreasingIndentation

到 .cabal 文件,或者如果你更喜欢将 {-# language NondecreasingIndentation #-} 添加到这个模块。或者,重新格式化上面的代码,或者使用 Haskell98 而不是 Haskell2010。