是否可以使用 WinHugs 打印 Haskell 中的所有约简?

发布于 2024-07-10 20:41:34 字数 257 浏览 9 评论 0原文

我编写了以下函数..并使用 WinHugs 执行

teneven =  [x | x <- [1..10], even x]

我的输出:

Main> teneven
[2,4,6,8,10] :: [Integer] 
(63 reductions, 102 cells)

是否有办法打印所有缩减..以便我可以了解 WinHugs 内部发生的核心评估?

I have written the following function.. and executed using WinHugs

teneven =  [x | x <- [1..10], even x]

My output:

Main> teneven
[2,4,6,8,10] :: [Integer] 
(63 reductions, 102 cells)

is there anyway to print all the reductions.. so I can learn the core evaluation happening inside WinHugs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微暖i 2024-07-17 20:41:34

一些想法:

  1. 调试命令行选项(您可以在 Hugs 中使用 :set +d 设置)提供了丰富的信息,但非常冗长,并且不会向您显示 Haskell 语法的减少。

  2. 尝试Haskell Tracer。 我只是在一个简单的程序上尝试了一下,效果非常酷。 不过,我不在 Windows 上,我不知道让它运行起来有多困难。 这可能相当困难,这很遗憾,因为它很酷并且本质上就是您想要的。 如果您确实运行了它,您可以从 Hat 获取类似以下信息:

    <前><代码>主要 = {IO}
    十偶 = [2,4,6,8,10]
    _foldr (\..) [1,2,3,4,5,6,7,8, ...] [] = [2,4,6,8,10]
    (\..) 1 [2,4,6,8,10] = [2,4,6,8,10]
    (\..) 2 [4,6,8,10] = [2,4,6,8,10]
    (\..) 3 [4,6,8,10] = [4,6,8,10]
    (\..) 4 [6,8,10] = [4,6,8,10]
    (\..) 5 [6,8,10] = [6,8,10]
    (\..) 6 [8,10] = [6,8,10]
    (\..) 7 [8,10] = [8,10]
    (\..) 8 [10] = [8,10]
    (\..) 9 [10] = [10]
    (\..) 10 [] = [10]

    那里的 lambda 是偶数。 此外,如果您愿意,Hat 还可以跟踪 foldr 的调用以及其他内部调用; 默认情况下,它不会这样做。

Some ideas:

  1. The debug command-line option (which you can set with :set +d in Hugs) is informative, but is very verbose and does not show you the reductions in Haskell syntax.

  2. Try Hat - the Haskell Tracer. I just tried it on a simple program and it's pretty cool. I'm not on Windows, though, and I don't know how difficult it would be to get it running. It's likely fairly difficult, which is a shame since it's cool and essentially what you want. If you do get it running, you can get something like this information from Hat:

    main = {IO}
    teneven = [2,4,6,8,10]
    _foldr (\..) [1,2,3,4,5,6,7,8, ...] [] = [2,4,6,8,10]
    (\..) 1 [2,4,6,8,10] = [2,4,6,8,10]
    (\..) 2 [4,6,8,10] = [2,4,6,8,10]
    (\..) 3 [4,6,8,10] = [4,6,8,10]
    (\..) 4 [6,8,10] = [4,6,8,10]
    (\..) 5 [6,8,10] = [6,8,10]
    (\..) 6 [8,10] = [6,8,10]
    (\..) 7 [8,10] = [8,10]
    (\..) 8 [10] = [8,10]
    (\..) 9 [10] = [10]
    (\..) 10 [] = [10]
    

    The lambda there is even. Also, if you want, Hat can trace into calls of foldr and other internal calls; by default, it doesn't do that.

雨巷深深 2024-07-17 20:41:34

这里有一些使用 Debug.Trace 和 Hugs.Observe 的示例。

import Debug.Trace
fact :: Integer -> Integer
fact 0 = trace "fact 0 ->> 1" 1
fact n = trace ("fact " ++ show n) (n * fact (n-1))


import Hugs.Observe
fact :: Integer -> Integer
fact 0 = observe "fact 0" 1
fact n = observe "fact n" (n *  fact (n-1))

希望这可以帮助您了解如何使用 WinHungs 打印所有缩减。

Here you have a few examples of the use of Debug.Trace and Hugs.Observe.

import Debug.Trace
fact :: Integer -> Integer
fact 0 = trace "fact 0 ->> 1" 1
fact n = trace ("fact " ++ show n) (n * fact (n-1))


import Hugs.Observe
fact :: Integer -> Integer
fact 0 = observe "fact 0" 1
fact n = observe "fact n" (n *  fact (n-1))

Hope this helps you to figure out how to print all reductions using WinHungs.

恋你朝朝暮暮 2024-07-17 20:41:34

相信我,你不想走这条路。

每个特定情况下使用的缩减集(和顺序)将取决于特定的语言实现(hugs 可以以一种方式实现,ghci - 以其他方式实现,jhc - 以另一种方式实现,等等)。

更好地阅读一些关于实现函数式语言的编译器/解释器/虚拟机的一般方法的内容 - 例如 SECD 机等。

几个链接:

Believe me, you dont want to go this way.

Set (and order) of reductions used in each particular case would depend on particular language implementation (hugs could do it one way, ghci - in other way, jhc - in yet another, etc).

Better read something about general ways to implement compiler/interpreter/virual machine for functional language - like SECD machine, etc.

Several links:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文