More Detailed GHC Error Messages

I ran the code through GHCI and got this error:

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

After some time, I continued to correct the error (which, as you might imagine, was caused by a too large index), but I would like the GHC to tell me which row this large index is evaluated in.

Is there any way either

  • A) make the GHCI more detailed, or
  • B) use normal practice that avoids this error somehow (shy, using smaller indices, of course)
+7
indexing haskell error-handling ghci
source share
1 answer

You can use the GHC profiling tools to get the stack trace for 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 it with

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

then run it like

 ./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 main β†’ foo chain.

+5
source share

All Articles