Why does my Haskell assertion only happen in IHask

2019-05-07 03:57发布

问题:

If I define

import Control.Exception (assert)
import Data.Char (ord)

f :: String -> String
f s = assert (all (`elem` letters) s) $ (letters!!) <$> (ix <$> s)
    where
        ix ch = (ord ch - ord 'A')
        letters = ['A'..'Z']

then if I execute

f "AB.CD"

in IHaskell, I get

:10:7-12: Assertion failed

as I would expect. But in all other settings, the assertion seems to be ignored. For example in GHCi (7.10.2) I get

ghci>f "AB.CD"
"AB*** Exception: Prelude.!!: negative index 

and if I put the expression in a program

main :: IO ()
main = do
    print $ f "AB.CD"

I get

prgm: Prelude.!!: negative index
"AB

Why is my assertion being ignored everywhere but in IHaskell?


In GHCi, :set gives:

options currently set: none.
base language is: Haskell2010
with the following modifiers:
  -XNoDatatypeContexts
  -XNondecreasingIndentation
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
  -fimplicit-import-qualified
warning settings: