什么是Haskell的严格点? [英] What are Haskell's strictness points?

查看:132
本文介绍了什么是Haskell的严格点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道(或者应该知道)Haskell默认是懒惰的。必须评估之前没有任何评估。那么什么时候必须评估一下?有些地方Haskell必须严格。我称之为严格点,尽管这个特定术语并不像我想象的那样广泛。根据我的观点:


仅在Haskell 中的缩减(或评估)出现在严格点处。


所以问题是: 精确是Haskell的严格点吗?我的直觉说 main seq / bang模式,模式匹配以及任何 IO 通过 main 执行的操作是主要的严格点,但我不知道为什么我知道。



(另外,如果他们没有被称为严格点,他们称之为



我想象一个很好的答案将包括关于WHNF等的一些讨论。我也想象它可能触及lambda微积分。






编辑:关于这个问题的其他想法。



正如我在这个问题上所反映的,我认为在严格点的定义中增加一些内容会更清楚。严格点可以有不同的上下文和不同的深度(或严格性)。回到我的定义,减少Haskell只发生在严格点,让我们在这个定义中增加这个条款:一个严格点只有当它的周围上下文被评估或减少时才被触发。

因此,让我试着让你开始我想要的那种答案。 main 是一个严格点。它被特别指定为其上下文的主要严格点:程序。当程序( main 的上下文)被评估时,main的严格点被激活。 Main的深度是最大的:它必须被充分评估。 Main通常由IO操作组成,这些操作也是严格点,其上下文是 main



现在您尝试:讨论这些术语中的 seq 和模式匹配。解释功能应用的细微差别:它是如何严格的?它怎么没有?那么 deepseq 呢? 个案陈述? unsafePerformIO Debug.Trace ?顶级定义?严格的数据类型?爆炸模式?等等。这些项目中有多少可以用seq或模式匹配来描述?

解决方案

通过理解本文:懒惰评估的自然语义(Launchbury)。这会告诉你什么时候针对与GHC核心类似的小语言评估表达式。然后剩下的问题是如何将完整的Haskell映射到Core,并且大部分翻译是由Haskell报告本身给出的。在GHC中,我们把这个过程叫做desugaring,因为它消除了语法糖。

那么,这不是全部的情况,因为GHC包含了一系列的优化,代码生成,其中许多转换将重新安排Core,以便在不同的时间进行评估(特别是严格分析会导致事先评估)。所以要真正理解你的
程序是如何评估的,你需要看看由GHC生成的Core。

答案似乎对你有点抽象(我没有具体提到爆炸模式或seq),但你要求的是精确的 ,这是我们能做的最好的。


We all know (or should know) that Haskell is lazy by default. Nothing is evaluated until it must be evaluated. So when must something be evaluated? There are points where Haskell must be strict. I call these "strictness points", although this particular term isn't as widespread as I had thought. According to me:

Reduction (or evaluation) in Haskell only occurs at strictness points.

So the question is: what, precisely, are Haskell's strictness points? My intuition says that main, seq / bang patterns, pattern matching, and any IO action performed via main are the primary strictness points, but I don't really know why I know that.

(Also, if they're not called "strictness points", what are they called?)

I imagine a good answer will include some discussion about WHNF and so on. I also imagine it might touch on lambda calculus.


Edit: additional thoughts about this question.

As I've reflected on this question, I think it would be clearer to add something to the definition of a strictness point. Strictness points can have varying contexts and varying depth (or strictness). Falling back to my definition that "reduction in Haskell only occurs at strictness points", let us add to that definition this clause: "a strictness point is only triggered when its surrounding context is evaluated or reduced."

So, let me try to get you started on the kind of answer I want. main is a strictness point. It is specially designated as the primary strictness point of its context: the program. When the program (main's context) is evaluated, the strictness point of main is activated. Main's depth is maximal: it must be fully evaluated. Main is usually composed of IO actions, which are also strictness points, whose context is main.

Now you try: discuss seq and pattern matching in these terms. Explain the nuances of function application: how is it strict? How is it not? What about deepseq? let and case statements? unsafePerformIO? Debug.Trace? Top-level definitions? Strict data types? Bang patterns? Etc. How many of these items can be described in terms of just seq or pattern matching?

解决方案

A good place to start is by understanding this paper: A Natural Semantics for Lazy Evalution (Launchbury). That will tell you when expressions are evaluated for a small language similar to GHC's Core. Then the remaining question is how to map full Haskell to Core, and most of that translation is given by the Haskell report itself. In GHC we call this process "desugaring", because it removes syntactic sugar.

Well, that's not the whole story, because GHC includes a whole raft of optimisations between desugaring and code generation, and many of these transformations will rearrange the Core so that things get evaluated at different times (strictness analysis in particular will cause things to be evaluated earlier). So to really understand how your program will be evaluated, you need to look at the Core produced by GHC.

Perhaps this answer seems a bit abstract to you (I didn't specifically mention bang patterns or seq), but you asked for something precise, and this is about the best we can do.

这篇关于什么是Haskell的严格点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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