如何在使用 cabal 运行的脚本中启用断言?

问题描述 投票:0回答:1

我想在我用 cabal 运行的脚本中使用

assert

文档这么说

断言通常可以通过编译器标志打开或关闭(对于 GHC,断言通常处于打开状态,除非使用 -O 打开优化或给出 -fignore-asserts 选项)。当断言关闭时,断言的第一个参数将被忽略,第二个参数将作为结果返回。

根据输出,这两个标志均未使用(见下文)。不幸的是,文档没有提到如何在需要时打开断言。

剧本:

{- cabal:
build-depends: base
-}

-- cabal run debug.hs

import System.Environment
import Control.Exception (assert)

main :: IO ()
main = do
    let sum = assert (2 > 3) $ 1+ 1
    print sum

执行:

PS C:\Users\<me>\Desktop\<...>\haskell> cabal run debug.hs
Warning: The package list for 'hackage.haskell.org' is 1096 days old.
Run 'cabal update' to get the latest list of available packages.
Resolving dependencies...
Build profile: -w ghc-8.10.2 -O1
In order, the following will be built (use -v for more details):
 - fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Preprocessing executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
[1 of 1] Compiling Main             ( Main.hs, C:\Users\<me>\AppData\Local\Temp\cabal-repl.-26728\dist-newstyle\build\x86_64-windows\ghc-8.10.2\fake-package-0\x\script\build\script\script-tmp\Main.o )
Linking C:\Users\<me>\AppData\Local\Temp\cabal-repl.-26728\dist-newstyle\build\x86_64-windows\ghc-8.10.2\fake-package-0\x\script\build\script\script.exe ...
2
haskell assert cabal
1个回答
0
投票

我将“O”误读为“0”,优化确实已打开,需要使用

--disable-optimization
禁用。

PS C:\Users\<me>\Desktop\<...>\haskell> cabal run debug.hs --disable-optimization
Warning: The package list for 'hackage.haskell.org' is 1096 days old.
Run 'cabal update' to get the latest list of available packages.
Resolving dependencies...
Build profile: -w ghc-8.10.2 -O0
In order, the following will be built (use -v for more details):
 - fake-package-0 (exe:script) (first run)
Configuring executable 'script' for fake-package-0..
Preprocessing executable 'script' for fake-package-0..
Building executable 'script' for fake-package-0..
[1 of 1] Compiling Main             ( Main.hs, C:\Users\<me>\AppData\Local\Temp\cabal-repl.-11816\dist-newstyle\build\x86_64-windows\ghc-8.10.2\fake-package-0\x\script\noopt\build\script\script-tmp\Main.o )
Linking C:\Users\<me>\AppData\Local\Temp\cabal-repl.-11816\dist-newstyle\build\x86_64-windows\ghc-8.10.2\fake-package-0\x\script\noopt\build\script\script.exe ...
script: Assertion failed
CallStack (from HasCallStack):
  assert, called at Main.hs:13:15 in main:Main
© www.soinside.com 2019 - 2024. All rights reserved.