我有這個程式Main.hs:
main :: IO ()
main = do
if False then undefined
else do
let x = 5
print x
當我編譯ghc Main.hs它compils以及生成Main的可執行檔案,但是當(初始化與陰謀后cabal init)我盡量讓cabal new-run這給出了一個錯誤:
$ cabal new-run
Build profile: -w ghc-8.6.5 -O1
In order, the following will be built (use -v for more details):
- ghc-vs-cabal-0.1.0.0 (exe:ghc-vs-cabal) (file Main.hs changed)
Preprocessing executable 'ghc-vs-cabal' for ghc-vs-cabal-0.1.0.0..
Building executable 'ghc-vs-cabal' for ghc-vs-cabal-0.1.0.0..
[1 of 1] Compiling Main ( Main.hs, /home/ivan/ghc_vs_cabal/dist-newstyle/build/x86_64-linux/ghc-8.6.5/ghc-vs-cabal-0.1.0.0/x/ghc-vs-cabal/build/ghc-vs-cabal/ghc-vs-cabal-tmp/Main.o )
Main.hs:4:10: error: Empty 'do' block
|
4 | else do
| ^^
有了cabal run它也會出錯。
我有陰謀集團版本2.4.0.0:
$ cabal --version
cabal-install version 2.4.0.0
compiled using version 2.4.0.1 of the Cabal library
和 ghc 版本8.6.5:
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.6.5
有人知道發生了什么嗎?
我知道如果我添加縮進我可以修復它:
main :: IO ()
main = do
if False then undefined
else do
let x = 5
print x
但我想知道為什么它編譯ghc但不編譯cabal new-run
uj5u.com熱心網友回復:
您的代碼需要一個語言擴展來決議:NondecreasingIndentation. 這個擴展的存在是為了避免嵌套dos的尷尬失控效果
main :: IO ()
main = do
if False then undefined
else do -- next block should be indented in standard Haskell
let x = 5
print x -- ...but this can easily get out of hand if you do it multiple times
NondecreasingIndentation允許嵌套do塊注冊為嵌套,只要它與包含塊的縮進一樣多,而不是超過容器。
根據GHC 手冊,NondecreasingIndentation默認情況下處于Haskell2010啟用狀態,但在模式下禁用(除非再次明確啟用)。我找不到相應的cabal檔案,但我們大概可以猜到它默認為指定Haskell2010.
您可以通過添加 pragma 來指定要啟用或禁用的源檔案中的擴展名,而不管外部選項如何
{-# LANGUAGE NondecreasingIndentation #-}
到檔案的最頂部。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/398722.html
