我通过 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
至少告诉你
main
→ foo
链。
关于haskell - 来自 GHC 的更多描述性错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34688983/