如何获得有关Haskell错误发生位置的更多信息?例如,昨天我正在研究一个Haskell程序,它解析输入文件,转换数据然后打印出报告信息.
有一次,我跑了"主"并回来了
*** Prelude.read: parse error
Run Code Online (Sandbox Code Playgroud)
没有其他信息.幸运的是,我知道我只是在一个地方打电话阅读并且能够修复它,但对于未来:
谢谢!
使用GHC 编辑.
如果你可以在ghci中运行代码,那么调试器可以做你想做的一切.这是一个引发异常的程序
foo s i
| i == 57 = read s
| otherwise = i
main = mapM_ (print . foo "") [1..100]
Run Code Online (Sandbox Code Playgroud)
现在将其加载到ghci并使用调试器,如下所示:http: //www.haskell.org/ghc/docs/latest/html/users_guide/ghci-debugger.html#ghci-debugger-exceptions
> ghci test.hs
*Main> :set -fbreak-on-error
*Main> :trace main
1
2
... snipped 3 through 55 ...
56
Stopped at <exception thrown>
_exception :: e = _
[<exception thrown>] *Main> :back
Logged breakpoint at test.hs:2:15-20
_result :: a
s :: String
[-1: test.hs:2:15-20] *Main> :list
1 foo s i
2 | i == 57 = **read s**
3 | otherwise = i
[-1: test.hs:2:15-20] *Main> s
""
[-1: test.hs:2:15-20] *Main>
Run Code Online (Sandbox Code Playgroud)
它允许您在评估历史记录中四处走动,突出显示引发异常的实际表达式(粗体而不是在终端上加星标),并允许您检查局部变量.
另一种选择是使用分析和一些标记重新编译以标记适当的成本中心,并使用-xc profiling选项运行,该选项在未捕获的异常上打印成本中心堆栈 http://www.haskell.org/ghc/docs/latest/html /users_guide/prof-time-options.html
> ghc -prof -auto-all test.hs
> ./test +RTS -cs
1
2
... snipped 3 through 55 ...
56
*** Exception (reporting due to +RTS -xc): (THUNK_2_0), stack trace:
Main.foo,
called from Main.main,
called from Main.CAF
--> evaluated by: Main.main,
called from Main.CAF
test: Prelude.read: no parse
Run Code Online (Sandbox Code Playgroud)
有点困难的原因在调试器页面上稍早描述 http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-debugger.html#tracing 基本上,高效的Haskell执行没有'使用类似于普通调用堆栈的任何东西,所以要获得异常的那种信息,你必须在某种特殊模式(调试或分析)中运行,这样可以保留那种信息.
您可以通过导入Debug.Trace
和更改调用来获取导致解析错误的字符串
import Debug.Trace (trace) --change myRead s = read s --to myRead s = trace s (read s) --or myRead s = trace (take 100 s) (read s)
归档时间: |
|
查看次数: |
3421 次 |
最近记录: |