简体   繁体   English

打印出Haskell的评估(重写)步骤,用于教育/学习目的。 可能吗?

[英]Printing out Haskell's evaluation (rewriting) steps for educational/learning purposes. Is it possible?

I describe this question by using an example from a book. 我通过使用书中的一个例子来描述这个问题。

In Simon Thompson's book "HASKELL the craft of functional programming" on page 82 (see images below) are shown the evaluation steps for fac 4 . 在Simon Thompson的书“HASKELL函数式编程工艺”(第82页)(见下图)中显示了fac 4的评估步骤。

QUESTION: 题:

Is it possible to use some tool or some "Haskell debugger" that would write out the evaluation steps that GHCi uses when it would evaluate the value of fac 4 ? 有可能使用一些工具或一些“Haskell调试器”来写出GHCi在评估fac 4的价值时使用的评估步骤吗?

Preferably in a human readable format, for educational and learning purposes. 优选地,以人类可读的格式,用于教育和学习目的。

It would be also good to have some automatic explanation for each evaluation step, for example which equation was used in the rewriting step. 对每个评估步骤进行一些自动解释也是很好的,例如在重写步骤中使用了哪个等式。

My main purpose is to gain a deeper understanding of how the rewriting steps are carried out when I run simple educational Haskell example programs (like fac ). 我的主要目的是更深入地了解当我运行简单的教育Haskell示例程序(如fac )时如何执行重写步骤。

Is there a way to do this ? 有没有办法做到这一点 ? If yes, how ? 如果有,怎么样?

在此输入图像描述

在此输入图像描述

There is a tool called Lambda bubble pop where you can click on the expression to see how the expression is getting reduced. 有一个名为Lambda bubble pop的工具,您可以在其中单击表达式以查看表达式如何减少。 Note that the tool only supports Integers and Lists as of now, but nevertheless is a good educational tool. 请注意,该工具仅支持整数和列表,但仍然是一个很好的教育工具。

Snapshot of the tool in action: 实际工具的快照:

在此输入图像描述

Yes and No. I haven't seen a tool yet that does this line-by-line evaluation that is depicted in your textbook - mostly because a Haskell programm does no "rewriting" of expressions. 是和否。我还没有看到一个工具,它在你的教科书中描述了这种逐行评估 - 主要是因为Haskell程序没有“重写”表达式。

However, there is a tool that does visualize Haskell's actual evaluation strategy, step by step: ghc-vis . 但是,有一个工具可以逐步可视化Haskell的实际评估策略: ghc-vis Instead of just evaluating the result and displaying it on the console like ghci does, it displays a graphical representation of the unevaluated result - and you can force the evaluation of it thunk by thunk, until you arrive at the primitive values and structures. 而不是仅仅评估的结果,就像在控制台上显示它ghci呢,它显示未计算结果的图形表示-你可以强制评估它咚由thunk的,直到你在原始的价值观和结构到达。

As an example of what it can do, here's the evaluation until the third list member of the infinite fibonacci sequence: 作为它可以做什么的一个例子,这里的评估直到无限斐波那契序列的第三个列表成员:

0, 1 and 1 are evaluated, the rest of the list is a thunk referring back to parts of the list itself http://felsin9.de/nnis/ghc-vis/fib1.svg 评估0,1和1,列表的其余部分是回顾列表本身部分的thunk http://felsin9.de/nnis/ghc-vis/fib1.svg

Source: examples section of the project website . 资料来源: 项目网站的示例部分 You should have a look at all of them! 你应该看看他们所有人!

This is a much-requested and highly useful feature — which, as best as I know, is not available anywhere. 这是一个非常有用且非常有用的功能 - 据我所知,它在任何地方都无法使用。 :-( :-(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM