简体   繁体   English

Haskell“源减少”

[英]Haskell “Source reduction”

I'm revising for an upcoming Haskell exam and I don't understand one of the questions on a past paper. 我正在修改即将到来的Haskell考试,我不理解过去论文中的一个问题。 Google turns up nothing useful 谷歌没有任何用处

fst(x, y) = x
square i = i * i

i) Source reduce, using Haskells lazy evaluation, the expression: i)源减少,使用Haskells懒惰评估,表达式:

fst(square(3+4), square 8)

ii) Source reduce, using strict evaluation, the same expression ii)使用严格的评估,源减少相同的表达式

iii) State one advantage of lazy evaluation and one advantage of strict evaluation iii)说明懒惰评估的一个优点和严格评估的一个优点

What I don't understand is what is source reduction? 我不明白什么源减少?

Reduction is a term from the lambda calculus which involves a semantics-preserving transformation that replaces one term with an equivalent term. 缩减是lambda演算中的一个术语,它涉及一个语义保留变换,用一个等价项代替一个术语。 For the examples you've given, the most important kind of reductions are 对于你给出的例子,最重要的减少是

  • Replacement of a name by its definition (an instance of substituting equals for equals). 根据其定义替换名称(将equals替换为equals的实例)。
  • Beta-reduction of a function application. 功能应用程序的Beta减少。

Beta-reduction is the fundamental rule in the lambda calculus, and in a pure, lazy language like Haskell, it always preserves semantics. Beta减少是lambda演算中的基本规则,在像Haskell这样的纯粹,懒惰的语言中,它总是保留语义。 The beta rule is the one that says: 测试版规则是:

(\x. e) m

can be replaced by e with m substituted for x . 可以用e代替,用m代替x (The substitution must avoid "capturing" free instances of x in m .) (替换必须避免“捕获” mx自由实例。)

It's quite possible that your instructor wants you to combine reductions as follows: 您的教练很可能希望您将减少量组合如下:

  1. Find a function application where the function being applied is a name. 查找正在应用的函数是名称的函数应用程序。
  2. Replace the name with its definition, which will be a lambda abstraction. 将名称替换为其定义,这将是lambda抽象。
  3. Beta-reduce the application. Beta-减少应用程序。
  4. Do this all in one step. 一步完成这一切。

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 of fst that could be reduced in this fashion. 例如,在你给出的术语中,有两个squarefst应用程序可以这种方式减少。 (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. 使用与Haskell的惰性求值相对应的约简策略,将以下表达式减少为弱头正态形式。 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. 我可能会选择不那么腼腆,只是命名减少策略:按需求减少策略和逐个减值策略。

From the structure of the question, it probably just mean "evaluate the expression by hand", eg 从问题的结构来看,它可能只是意味着“手工评估表达”,例如

head (map primeTest (enumFromTo 1000 2000))

in lazy (evaluate only when needed) evaluation, 在懒惰(仅在需要时评估)评估,

  head (map primeTest (enumFromTo 1000 2000))
= head (map primeTest (1000 : enumFromTo 1001 2000))
= head (primeTest 1000 : map primeTest (enumFromTo 1001 2000))
= primeTest 1000
= False

in strict (evaluate everything first) evaluation 严格(先评价一切)评价

  head (map primeTest (enumFromTo 1000 2000))
= head (map primeTest (1000 : enumFromTo 1001 2000))
= ...
= head (map primeTest [1000, 1001, ..., 2000])
= head (primeTest 1000 : map primeTest [1001, 1002, ..., 2000])
= head (False : map primeTest [1001, 1002, ..., 2000])
= ...
= head [False, False, ..., False]
= False

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". 我能找到的唯一相关的地方是http://www.cs.bham.ac.uk/internal/modules/2009/11582.html ,其中“source reducation”被列为“编程技术”。 (O_O) (O_O)

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

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