来自GHC的更多描述性错误消息

Mar*_*van 7 indexing error-handling haskell ghci

我通过GHCI运行了一些代码,并得到了这个错误:

*** Exception: Prelude.!!: index too large
Run Code Online (Sandbox Code Playgroud)

过了一会儿,我继续修复了这个错误(正如你可能想象的那样,由一个太大的索引引起),但是我希望GHC会告诉我这个大索引的评估线是什么.

还有办法吗?

  • A)使GHCI更加冗长,或者
  • B)使用一种常见的做法,以某种方式避免这种错误(当然,害羞使用较小的索引)

Cac*_*tus 5

您可以使用GHC的分析工具来获取错误的堆栈跟踪,例如,假设这是您的源文件:

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

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

main :: IO ()
main = mapM_ foo [1..10]
Run Code Online (Sandbox Code Playgroud)

如果你用它编译

ghc --make -prof -fprof-auto  StackTrace.hs 
Run Code Online (Sandbox Code Playgroud)

然后运行它

./StackTrace +RTS -xc
Run Code Online (Sandbox Code Playgroud)

然后你得到

*** 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
Run Code Online (Sandbox Code Playgroud)

至少告诉你mainfoo链.