I want to use assert
in a script that I run with cabal.
The documentation says that
Assertions can normally be turned on or off with a compiler flag (for GHC, assertions are normally on unless optimisation is turned on with -O or the -fignore-asserts option is given). When assertions are turned off, the first argument to assert is ignored, and the second argument is returned as the result.
Neither flag is used according to the output (see below). Unfortunately, the documentation does not mention how to turn assertions on if needed.
the script:
{- 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
execution:
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
You figured it out, but for completeness, there are several other ways:
--disable-optimizations
add --ghc-option=-fno-ignore-asserts
to the command line instead.Or enable it in the source code itself:
Add either of these into the initial {- cabal: ... -}
section:
ghc-options: -fno-ignore-asserts
ghc-options: -O0
Or right after it:
{-# OPTIONS_GHC -fno-ignore-asserts #-}
{-# OPTIONS_GHC -O0 #-}