cabal build给出了解析错误,但代码在GHCi中工作正常

时间:2016-04-13 17:01:53

标签: haskell cabal

我的代码适用于GHCi,但是当我尝试使用cabal build构建项目时,它会出错 parse error on input ‘<-’

这是一个最小的例子:

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

1 个答案:

答案 0 :(得分:2)

事实证明我的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。

相关问题