如何调试Haskell代码? [英] How to debug Haskell code?

查看:109
本文介绍了如何调试Haskell代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我写了一个大的Haskell程序,它总是适用于小的输入。现在,当我想测试它并产生更大的输入时,我总是得到消息:

  HsProg:Prelude.head:空列表

我使用 Prelude.head 次我可以做些什么来找出更多的内容或获得更好的错误输出来获取代码行。

解决方案

GHCi选项 -fbreak-on-exception 可能很有用。这是一个调试会话的例子。首先我们将文件加载到GHCi中。

  $ ghci Broken.hs 
GHCi,版本7.0.2:http://www.haskell.org/ghc/:?为了帮助
加载包ghc-prim ... linked ... done。
加载package integer-gmp ... linking ... done。
加载包基...链接...完成。
加载包ffi-1.0 ...链接...完成。
[1 of 1]编译Main(Broken.hs,解释)
好​​的,加载模块:Main。

现在,我们打开 -fbreak-on-exceptions 并跟踪我们的表达式(在这种情况下,整个程序的 main

  *主> :set -fbreak-on-exception 
* Main> :trace main
停止在<抛出异常>
_exception :: e = _

我们已经停止了异常。我们试着看一下:list 的代码。

  [<异常抛出>] *主> :list 
无法列出< exception throwwn的源代码>
尝试:然后:列表

因为 Prelude.head ,我们不能直接看源码。但是,GHCi通知我们,我们可以去:返回并尝试列出跟踪中发生的事情。

  [<异常抛出>] *主> :back 
Broken.hs记录的断点:2:23-42
_result :: [Integer]
[-1:Broken.hs:2:23-42] *主>列表
1
2 main = print $ head $ 过滤器奇数[2,4,6]
3

在终端中,违规表达式 filter odd [2,4,6] 突出显示在粗体。所以这是在这种情况下评估为空列表的表达式。



有关如何使用GHCi调试器的更多信息,请参阅GHC用户指南


I have a problem. I wrote a big Haskell program, and it always works with small input. Now, when I want to test it and generate a bigger input, I always get the message:

HsProg: Prelude.head: empty list

I use Prelude.head many times. What can I do to find out more or get a better error output to get the code line in which it happens?

解决方案

The GHCi option -fbreak-on-exception can be useful. Here's an example debugging session. First we load our file into GHCi.

$ ghci Broken.hs
GHCi, version 7.0.2: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 1] Compiling Main             ( Broken.hs, interpreted )
Ok, modules loaded: Main.

Now, we turn on -fbreak-on-exceptions and trace our expression (main in this case for the whole program).

*Main> :set -fbreak-on-exception
*Main> :trace main
Stopped at <exception thrown>
_exception :: e = _

We've stopped at an exception. Let's try to look at the code with :list.

[<exception thrown>] *Main> :list
Unable to list source for <exception thrown>
Try :back then :list

Because the exception happened in Prelude.head, we can't look at the source directly. But as GHCi informs us, we can go :back and try to list what happened before in the trace.

[<exception thrown>] *Main> :back
Logged breakpoint at Broken.hs:2:23-42
_result :: [Integer]
[-1: Broken.hs:2:23-42] *Main> :list
1  
2  main = print $ head $ filter odd [2, 4, 6]
3  

In the terminal, the offending expression filter odd [2, 4, 6] is highlighted in bold font. So this is the expression that evaluated to the empty list in this case.

For more information on how to use the GHCi debugger, see the GHC User's Guide.

这篇关于如何调试Haskell代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆