我通过GHCI运行了一些代码,并收到此错误:

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

一段时间后,我继续修复该错误(正如您可能想像的那样,它是由太大的索引引起的),但我希望GHC会告诉我该大索引正在评估的行。

有没有办法
  • A)使GHCI更详细,或
  • B)使用避免
    某种程度上的此错误(当然,避免使用较小的索引)
  • 最佳答案

    例如,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链。

    09-03 23:01
    查看更多