Haskell“源减少”
我正在复习即将到来的 Haskell 考试,但我不明白过去试卷上的一个问题。 Google 出现 没什么用
fst(x, y) = x
square i = i * i
i) 源reduce,使用Haskells惰性求值,表达式:
fst(square(3+4), square 8)
ii) 源归约,使用严格求值,相同的表达式
iii) 说明惰性求值的一项优点和严格求值的一项优点
我不明白什么是源归约?
I'm revising for an upcoming Haskell exam and I don't understand one of the questions on a past paper. Google turns up nothing useful
fst(x, y) = x
square i = i * i
i) Source reduce, using Haskells lazy evaluation, the expression:
fst(square(3+4), square 8)
ii) Source reduce, using strict evaluation, the same expression
iii) State one advantage of lazy evaluation and one advantage of strict evaluation
What I don't understand is what is source reduction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
归约是 lambda 演算中的一个术语,它涉及一种语义保留转换,即用等效术语替换一个术语。对于您给出的示例,最重要的简化是
Beta 约简是 lambda 演算的基本规则,在像 Haskell 这样的纯粹、惰性语言中,它总是保留语义。 beta 规则是这样的:
可以用
e
替换,用m
替换x
。 (替换必须避免在m
中“捕获”x
的自由实例。)您的讲师很可能希望您按如下方式组合归约:
请注意,您经常可以选择减少哪个应用程序;例如,在您给出的术语中,有两个
square
应用程序和一个fst
应用程序可以通过这种方式减少。 (+的应用也可以减少,但是涉及常量的减少需要不同的规则。)从问题中我看到你的老师希望你重复减少每个术语,直到它达到正常形式并且你的讲师希望您展示您对不同减少策略的理解。 “源缩减”中的“源”一词是多余的;缩减意味着在某些语言中对源术语进行操作。我会将问题表述如下:
使用与 Haskell 的惰性求值相对应的归约策略,将以下表达式归约为弱头范式。显示归约序列中的每个步骤。
使用与严格函数语言中的求值相对应的归约策略,将以下表达式归约为 head 范式。
使用与严格函数语言中的求值相对应的归约策略
我可能会选择不那么含糊其辞,只说出减少策略:按需求减少呼叫策略和按价值呼叫减少策略。
Reduction is a term from the lambda calculus which involves a semantics-preserving transformation that replaces one term with an equivalent term. For the examples you've given, the most important kind of reductions are
Beta-reduction is the fundamental rule in the lambda calculus, and in a pure, lazy language like Haskell, it always preserves semantics. The beta rule is the one that says:
can be replaced by
e
withm
substituted forx
. (The substitution must avoid "capturing" free instances ofx
inm
.)It's quite possible that your instructor wants you to combine reductions as follows:
Notice you often have a choice about which application to reduce; for example, in the term you give, there are two applications of
square
and one offst
that could be reduced in this fashion. (The application of + can also be reduced, but reduction involving constants requires different rules.)From the questions I see that your instructor wants you to reduce each term repeatedly until it reaches a normal form and that your instructor wants you to demonstrate your understanding of different reduction strategies. The word "source" in "source reduce" is superfluous; reduction means manipulation of source terms in some language. I would have phrased the questions as follows:
Using the reduction strategy that corresponds to Haskell's lazy evaluation, reduce the following expression to weak head normal form. Show each step in the sequence of reductions.
Using the reduction strategy that corresponds to evaluation in a strict functional language, reduce the following expression to head normal form.
I probably would have chosen to be less coy and just name the reduction strategies: a call-by-need reduction strategy and a call-by-value reduction strategy.
从问题的结构来看,它可能只是意味着“手动评估表达式”,例如
在惰性评估中(仅在需要时评估),
在严格评估中(首先评估所有内容) )评估
我能找到的唯一相关的地方是 http:// www.cs.bham.ac.uk/internal/modules/2009/11582.html 其中“源代码缩减”被列为“编程技术”。 (O_O)
From the structure of the question, it probably just mean "evaluate the expression by hand", e.g.
in lazy (evaluate only when needed) evaluation,
in strict (evaluate everything first) evaluation
The only relevant place I could find is http://www.cs.bham.ac.uk/internal/modules/2009/11582.html where "source reducation" is listed as a "Programming technique". (O_O)