Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More Descriptive Error Messages From GHC

I ran some code through GHCI, and got this error:

*** Exception: Prelude.!!: index too large

After a while I went on to fix the bug (which was caused, as you might imagine, by an index that was too large), but I wish GHC would have told me at what line this large index was being evaluated.

Is there a way to either

  • A) make GHCI more verbose, or
  • B) use a common practice that avoids this error somehow (shy of using smaller indexes, of course)
like image 649
Mark Karavan Avatar asked Jan 09 '16 02:01

Mark Karavan


1 Answers

You can use GHC's profiling facilities to get a kind of stack trace on errors, for example, suppose this is your source file:

xs :: [Int]
xs = [1..10]

foo :: Int -> IO ()
foo i = print $ xs !! i

main :: IO ()
main = mapM_ foo [1..10]

If you compile this with

ghc --make -prof -fprof-auto  StackTrace.hs 

then run it as

./StackTrace +RTS -xc

then you get

*** Exception (reporting due to +RTS -xc): (THUNK_1_0), stack trace: 
  GHC.List.CAF
  --> evaluated by: Main.foo,
  called from Main.main,
  called from Main.CAF
StackTrace: Prelude.!!: index too large

which at least tells you the mainfoo chain.

like image 161
Cactus Avatar answered Nov 02 '22 05:11

Cactus