非严格和懒惰有何区别? [英] How does non-strict and lazy differ?

查看:86
本文介绍了非严格和懒惰有何区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常读到懒惰非严格并不相同,但是我很难理解它们之间的区别.它们似乎可互换使用,但我了解它们具有不同的含义.我希望能帮助您理解其中的区别.

I often read that lazy is not the same as non-strict but I find it hard to understand the difference. They seem to be used interchangeably but I understand that they have different meanings. I would appreciate some help understanding the difference.

关于此帖子,我有几个分散的问题.我将在本文末尾总结这些问题.我有一些示例代码片段,我没有对其进行测试,仅将它们作为概念进行了介绍.我添加了引号,以免您查找引号.也许会在以后对相同的问题有所帮助.

I have a few questions which are scattered about this post. I will summarize those questions at the end of this post. I have a few example snippets, I did not test them, I only presented them as concepts. I have added quotes to save you from looking them up. Maybe it will help someone later on with the same question.

如果将函数f应用于非终止符,则该函数f是严格的表达式,它也无法终止.换句话说,f是严格的如果f bot的值为 | .对于大多数编程语言,所有功能很严格.但这在Haskell中并非如此.作为一个简单的例如,考虑const1,常量1函数,定义如下:

A function f is said to be strict if, when applied to a nonterminating expression, it also fails to terminate. In other words, f is strict iff the value of f bot is |. For most programming languages, all functions are strict. But this is not so in Haskell. As a simple example, consider const1, the constant 1 function, defined by:

const1 x = 1

const1 x = 1

Haskell中const1 bot的值是1.const1不需要需要".它的参数值,它从不尝试对其进行评估,因此永远不会陷入不确定的境地计算.因此,非严格函数也被称为惰性函数",并且据说是懒惰地"评估其参数,或根据需要".

The value of const1 bot in Haskell is 1. Operationally speaking, since const1 does not "need" the value of its argument, it never attempts to evaluate it, and thus never gets caught in a nonterminating computation. For this reason, non-strict functions are also called "lazy functions", and are said to evaluate their arguments "lazily", or "by need".

- Haskell简介:函数

我真的很喜欢这个定义.这似乎是我了解严格的最佳选择. const1 x = 1 也是懒惰的吗?

I really like this definition. It seems the best one I could find for understanding strict. Is const1 x = 1 lazy as well?

非严格意味着减少(数学术语为评估)从外部进来,

Non-strictness means that reduction (the mathematical term for evaluation) proceeds from the outside in,

因此,如果您有(a +(b c)),则首先减小+,然后减小内部(b c).

so if you have (a+(bc)) then first you reduce the +, then you reduce the inner (bc).

- Haskell Wiki:懒惰与非严格

Haskell Wiki确实使我感到困惑.我了解他们在说什么订单,但是我看不到(a +(b * c))如果通过了 _ | _ ,将如何进行非严格评估?

The Haskell Wiki really confuses me. I understand what they are saying about order but I fail to see how (a+(b*c)) would evaluate non-strictly if it was pass _|_?

在非严格评估中,不评估函数的参数除非它们实际用于功能主体的评估中.

In non-strict evaluation, arguments to a function are not evaluated unless they are actually used in the evaluation of the function body.

在Church编码下,运算符的惰性评估映射为非严格功能评估;因此,非严格评估是通常被称为懒惰".许多语言中的布尔表达式都使用一种非严格评估形式,称为短路评估,其中一旦确定明确,评估就返回布尔值将产生-例如,一个析取表达式,其中遇到true,或者在合取表达式中遇到false遇到的,依此类推.条件表达式通常也使用懒惰的评估,评估会在明确的情况下尽快返回分支.

Under Church encoding, lazy evaluation of operators maps to non-strict evaluation of functions; for this reason, non-strict evaluation is often referred to as "lazy". Boolean expressions in many languages use a form of non-strict evaluation called short-circuit evaluation, where evaluation returns as soon as it can be determined that an unambiguous Boolean will result — for example, in a disjunctive expression where true is encountered, or in a conjunctive expression where false is encountered, and so forth. Conditional expressions also usually use lazy evaluation, where evaluation returns as soon as an unambiguous branch will result.

-维基百科:评估策略

另一方面,懒惰评估意味着仅评估一个需要结果的表达式(注意从减少"指的是减少".(评估").因此,当评估引擎看到表达式将构建一个包含任何值的thunk数据结构需要对表达式求值,再加上指向表达本身.当实际需要结果时,评估引擎调用表达式,然后将thunk替换为结果供将来参考....

Lazy evaluation, on the other hand, means only evaluating an expression when its results are needed (note the shift from "reduction" to "evaluation"). So when the evaluation engine sees an expression it builds a thunk data structure containing whatever values are needed to evaluate the expression, plus a pointer to the expression itself. When the result is actually needed the evaluation engine calls the expression and then replaces the thunk with the result for future reference. ...

很明显,一个重击和一个重击之间有很强的对应关系.部分评估的表达式.因此,在大多数情况下,术语懒惰"是指.和非严格的"是指非限制性的.是同义词.但不完全是.

Obviously there is a strong correspondence between a thunk and a partly-evaluated expression. Hence in most cases the terms "lazy" and "non-strict" are synonyms. But not quite.

- Haskell Wiki:懒惰与非严格

这似乎是Haskell的特定答案.我认为 lazy (懒惰)表示重击,而 non-strict (非严格)则表示部分评估.比较是否简化了? lazy 总是表示重击,而 non-strict 总是表示部分评估.

This seems like a Haskell specific answer. I take that lazy means thunks and non-strict means partial evaluation. Is that comparison too simplified? Does lazy always mean thunks and non-strict always mean partial evaluation.

在编程语言理论中,懒惰求值或按需调用 1 是延迟对表达式进行评估的评估策略直到实际需要它的值(非严格评估),并且避免重复评估(共享).

In programming language theory, lazy evaluation or call-by-need1 is an evaluation strategy which delays the evaluation of an expression until its value is actually required (non-strict evaluation) and also avoid repeated evaluations (sharing).

-维基百科:惰性评估

我知道大多数人在学习功能语言时都会说忘记命令式编程.但是,我想知道这些是否属于非严格,懒惰,或者两者兼而有之?至少它会提供一些熟悉的东西.

I know most people say forget imperative programming when learning a functional language. However, I would like to know if these qualify as non-strict, lazy, both or neither? At the very least it would provide something familiar.

短路

f1() || f2()

C#,Python和其他具有"yield"效果的语言

public static IEnumerable Power(int number, int exponent)
{
    int counter = 0;
    int result = 1;
    while (counter++ < exponent)
    {
        result = result * number;
        yield return result;
    }
}

- MSDN:产量(c#)

回调

int f1() { return 1;}
int f2() { return 2;}

int lazy(int (*cb1)(), int (*cb2)() , int x) {
    if (x == 0)
        return cb1();
    else
        return cb2();
}

int eager(int e1, int e2, int x) {
    if (x == 0)
         return e1;
    else
         return e2;
}

lazy(f1, f2, x);
eager(f1(), f2(), x);

问题

我知道拥有所有这些资源,答案就摆在我面前,但我无法把握.似乎所有定义似乎很容易被隐含或显而易见.

Questions

I know the answer is right in front of me with all those resources, but I can't grasp it. It all seems like the definition is too easily dismissed as implied or obvious.

我知道我有很多问题.随时回答您认为相关的任何问题.我添加了这些问题以供讨论.

I know I have a lot of questions. Feel free to answer whatever questions you feel are relevant. I added those questions for discussion.

  • const1 x = 1 也很懒吗?
  • 如何从内部"评估?非严格的?是因为向内允许减少不必要的表达式,例如在 const1 x = 1 中?减少似乎符合惰性的定义.
  • 懒惰总是表示暴徒,而非严格总是表示部分评估吗?这只是一个概括吗?
  • 以下强制性概念是懒",不严格",两者"还是都不是?
    • 短路
    • 使用产量
    • 传递回调以延迟或避免执行
    • Is const1 x = 1 also lazy?
    • How is evaluating from "inward" non-strict? Is it because inward allows reductions of unnecessary expressions, like in const1 x = 1? Reductions seem to fit the definition of lazy.
    • Does lazy always mean thunks and non-strict always mean partial evaluation? Is this just a generalization?
    • Are the following imperative concepts Lazy, Non-Strict, Both or Neither?
      • Short Circuiting
      • Using yield
      • Passing Callbacks to delay or avoid execution

      谢谢你!

      推荐答案

      非严格和懒惰虽然可以非正式地互换,但它们适用于不同的讨论领域.

      Non-strict and lazy, while informally interchangeable, apply to different domains of discussion.

      非严格是指 语义:表达式的数学含义.不受严格限制的世界没有功能的运行时间,内存消耗甚至计算机的概念.它仅讨论域中的哪些类型的值映射到共域中的哪些类型的值.特别是 strict 函数必须映射值⊥.(底部"-有关更多信息,请参见上面的语义链接)到&perp ;;.不允许使用非严格功能.

      Non-strict refers to semantics: the mathematical meaning of an expression. The world to which non-strict applies has no concept of the running time of a function, memory consumption, or even a computer. It simply talks about what kinds of values in the domain map to which kinds of values in the codomain. In particular, a strict function must map the value ⊥ ("bottom" -- see the semantics link above for more about this) to ⊥; a non strict function is allowed not to do this.

      Lazy 是指操作行为:在真实计算机上执行代码的方式.大多数程序员在操作上都考虑程序,因此这可能就是您所想的.惰性评估是指使用thunk的实现-指向代码的指针,这些指针在首次执行时会被替换为一个值.注意这里的非语义词:指针",第一次",已执行".

      Lazy refers to operational behavior: the way code is executed on a real computer. Most programmers think of programs operationally, so this is probably what you are thinking. Lazy evaluation refers to implementation using thunks -- pointers to code which are replaced with a value the first time they are executed. Notice the non-semantic words here: "pointer", "first time", "executed".

      惰性评估引起非严格的语义,这就是为什么这些概念看起来如此紧密的原因.但是正如FUZxxl指出的那样,懒惰并不是实现非严格语义的唯一方法.

      Lazy evaluation gives rise to non-strict semantics, which is why the concepts seem so close together. But as FUZxxl points out, laziness is not the only way to implement non-strict semantics.

      如果您想了解有关此区别的更多信息,我强烈建议您使用上面的链接.阅读它是我对计算机程序含义的转折点.

      If you are interested in learning more about this distinction, I highly recommend the link above. Reading it was a turning point in my conception of the meaning of computer programs.

      这篇关于非严格和懒惰有何区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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