GHCi produces incorrect results when evaluating with compiled code
When GHCi evaluates in non-interpreted mode, it sometimes produces incorrect results. The following example---extracted from a much larger program---illustrates the problem:
import Lex num = 100000 txt1 = "aaa,aaa" main :: IO () main = print $ sum $ map (length . haskellLex) $ replicate num txt1
This program lexes the same string 100000 times, and prints the total number of tokens. Since we are lexing the same string, we'd expect to always get the same result, but due to this bug, this is not the case! We repeat the process 100000 times so that we can encounter the bug somewhat reliably---the problem does not occur every time.
To reproduce the problem, we need to load
Lex in compiled form, not interpreted and with some optimizations:
ghc -c -O1 -dynamic Lex.hs
The source code for the "lexer" is as follows:
module Lex (haskellLex) where cclass :: Char -> Int cclass c = case c of 'a' -> 10 ',' -> 11 'A' -> 0 'B' -> 0 'C' -> 0 'D' -> 0 'E' -> 0 'F' -> 0 haskellLex :: String -> [()] haskellLex  =  haskellLex (i:is) = case cclass i of 10 -> haskellLex62 is 11 -> () : haskellLex is haskellLex62 :: String -> [()] haskellLex62  = [()] haskellLex62 (i:is) = case cclass i of 0 -> [()] 1 -> [()] 2 -> [()] 3 -> [()] 4 -> [()] 10 -> haskellLex62 is 11 -> () : haskellLex (i:is) x -> error ("[GHC BUG] cclass returned: " ++ show (i,x)
This is a minimized version from a full lexer. As odd as it looks, removing pretty much anything seems to cause the bug to go away. This is what happens when we run the program:
runhaskell test.hs test.hs: [GHC BUG] cclass returned: (',',-556) CallStack (from HasCallStack): error, called at Lex.hs:36:12 in main:Lex
The problem is that after many evaluations, the function
-556 for character
,, when it should have returned
I've been able to reproduce this on two Linux machines, both running 64-bit Ubuntu (16.04). The issue does not seem to happen on Mac OS.