Why does my Haskell assertion only happen in IHaskell?

白昼怎懂夜的黑 提交于 2019-12-07 11:12:44

问题


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:

来源:https://stackoverflow.com/questions/32727418/why-does-my-haskell-assertion-only-happen-in-ihaskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!