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

More Descriptive Error Messages From GHC

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

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

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

有没有办法

You can use GHC's profiling facilities to get a kind of stack trace on errors,例如,假设这是你的源文件:

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

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

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

如果你用

编译它
ghc --make -prof -fprof-auto  StackTrace.hs 

然后运行它作为

./StackTrace +RTS -xc

然后你得到

*** 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

这至少告诉你 mainfoo 链。