使用 GHCi 在 Haskell 程序中调试无限循环 [英] Debugging infinite loops in Haskell programs with GHCi

查看:34
本文介绍了使用 GHCi 在 Haskell 程序中调试无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次在编写的 Haskell 程序中遇到无限循环.我已经把它缩小到一个非常具体的代码部分,但我似乎无法准确指出我有一个非终止递归定义的确切位置.我对 GHCi 中的 :trace 和 :history 有点熟悉,但问题是我的代码的某些分支涉及对 Data.Map.Map 的相当多的递归修改,因为地图 x 是通过根据 x' 根据另一个地图中的值对地图 x' 中的某些内容进行调整获得的>.具体细节在这里无关紧要,但正如您可能知道的那样,如果这种情况以交织的递归方式发生,我的通话记录将完全陷入地图 lookup 中涉及的所有各种比较中,adjustments 和 insertions.

For the first time I've encountered an infinite loop in a Haskell program I'm writing. I've narrowed it down to a quite specific section of code, but I cannot seem to pinpoint exactly where I have a non-terminating recursive definition. I'm vaguely familiar with :trace and :history in GHCi, but the problem is that some branches of my code involve quite a bit of recursive modifications of a Data.Map.Map in the sense that the map x is obtained by adjusting something in the map x' based on values in another map depending on x'. The specifics don't matter here, but as you can probably tell, if this happens in an intertwined recursive way, my call history gets completely bogged down in all the various comparisons involved in map lookups, adjustments and insertions.

谁能推荐一种更有效的方法来定位无限循环?例如,将呼叫历史限制为来自单个源文件的呼叫会大有帮助.

Can anyone recommend a more efficient way to locate infinite loops? It would, for example, help a lot to restrict the call history to calls from a single source file.

推荐答案

请确保您已充分使用 GHCi 调试器,包括设置 -fbreak-on-exception(如果您获得 <<loop>>,是吗?)并确保您已经尝试过斯蒂芬关于使用 GHC 警告的建议.

Be sure you've used the GHCi debugger to it's full extent, including setting -fbreak-on-exception (useful if you're getting <<loop>>, are you?) and be sure you've tried Stephen's advice of using GHC's warnings.

如果这些失败(GHCi 调试器真的不应该失败",这只是解释数据的问题)然后尝试运行 HPC 在循环情况下,因此您可以直观地看到没有被评估的分支和值,如果它在循环,那么应该完成的事情可能甚至没有被评估,这将显示在标记的 HTML 中.

If these fail (GHCi debugger really shouldn't 'fail', it's just a matter of interpreting the data) then try to run HPC on the looping case so you can visually see branches and values that aren't being evaluated, if it's looping then something that should be getting done probably isn't even being evaluated and that will show up in the marked up HTML.

这篇关于使用 GHCi 在 Haskell 程序中调试无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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