简体   繁体   English

haskell序曲:seq的定义

[英]haskell prelude : definition of seq

In the definition of the haskell prelude we see that ... is reserved for expressions that cannot be implemented in Haskell. haskell序言定义中,我们看到...保留给Haskell无法实现的表达式。 Now the IO monad for example can't be implemented in haskell. 例如,现在无法在haskell中实现IO monad。

What surprised me is that seq is defined as follows in the prelude 令我惊讶的是seq在序言中的定义如下

seq :: a -> b -> b
seq = ...       -- Primitive

Why not the following? 为什么不以下? what am i missing? 我想念什么?

seq _ b = b

As you can see in the Haskell Wiki entry on seq , the seq function must fulfil the following two equations: 如您seqHaskell Wiki条目中所见, seq函数必须满足以下两个方程式:

⊥ `seq` b = ⊥
a `seq` b = b

(where is the undefined value, which is what non-terminating function applications or things like hd [] and undefined evaluate to logically) (其中是未定义的值,这是非终止函数应用程序或诸如hd []undefined逻辑求值)

Your definition clearly does not fulfil the first equation. 您的定义显然不满足第一个方程式。

The typical use case for seq is to force evaluation of the first parameter (to weak head normal form) before evaluating the second one. seq的典型用例是在评估第二个参数之前强制评估第一个参数(以弱头法线形式)。 (although, strictly speaking, seq does not guarantee that; see again the wiki article) (尽管严格来讲, seq不能保证这一点;请再次参见Wiki文章)

Such a function is, to my knowledge, not definable in pure Haskell without any compiler extensions like -XBangPatterns . 据我所知,在没有任何编译器扩展(例如-XBangPatterns情况下,无法在纯Haskell中定义此-XBangPatterns

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

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