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

标签 haskell indexing error-handling ghci

我通过 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链。

    关于haskell - 来自 GHC 的更多描述性错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34688983/

    相关文章:

    Haskell:函数组合刚刚损坏了我的大脑

    algorithm - 元组列表中的添加

    database - 使用签名树的生产应用程序的任何示例?

    php - php中openssl解密的异常处理

    haskell - 如何强制评估 `unsafePerformIO` 内的 IO 操作?

    haskell - 如何以中缀形式使用类型构造函数

    java - 使用 DFS 将参数传递给我的 main 时出现问题 - Java

    orderBy后Angularjs错误$index

    php - 错误逻辑并在php中显示?

    php - 删除错误抑制运算符