seq force功能如何? [英] How does seq force functions?

查看:117
本文介绍了seq force功能如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来自于Brent Yorgey在OPLSS中提出的一个挑战:写一个函数 f ::(Int - > Int) - > Bool 区分 f undefined f(\ x - >未定义)。我们所有的答案要么使用 seq 或者类似于爆炸< seq 的爆炸模式。例如:

  f ::(Int  - > Int) - > Bool 
f g = g`seq` True

* Main> f undefined
***例外:Prelude.undefined
* Main> f(\ x - > undefined)
True

seq 的评论

  e1`seq` e2 

用于解析

  case {e1 of {_  - > e2} 

所以我尝试了手动解除绑定。它不起作用:

  f'g = case g of {_  - > True} 

* Main> f'undefined
True
* Main> f'(\ x - >未定义)
True



问题



这种行为是否依赖于更复杂的 seq 描述在评论结尾,如果是这样,它是如何工作的?这样的 f 可以写出没有这些原语吗? `e2 ==> (#x,_#)的情况下序号#x RW - > e2 - 注意阴影!
e1`seq` e2 ==> (#_,_#) - >的情况seq#x RW e2


解决方案

STG有更高层次的描述 - 如何制作快速咖喱:推/输入与eval / apply



图2包含适用于函数的规则CASEANY。在本文中,是一个值的提法意味着:


  • 它是一个饱和的构造函数应用程序

  • 它是一个函数

  • 它是一个部分函数应用程序(它仍然是一个函数,在语义上)



取消装箱值(包括文字)专门处理,更多信息可在作为一等公民的无箱值

所有这些都是实现细节,并隐藏在编译器(GHC)中。 Haskell的案例表达式并不强制它是监察者,只是模式匹配和seq。


Background

This question arises from a challenge Brent Yorgey posed at OPLSS: write a function f :: (Int -> Int) -> Bool that distinguishes f undefined from f (\x -> undefined). All of our answers either used seq or something like bang patterns that desugar into seq. For example:

f :: (Int -> Int) -> Bool
f g = g `seq` True

*Main> f undefined
*** Exception: Prelude.undefined
*Main> f (\x -> undefined)
True

The GHC commentary on seq says that

e1 `seq` e2 

used to desugar into

case e1 of { _ -> e2 }

so I tried desugaring manually. It didn't work:

f' g = case g of { _ -> True }

*Main> f' undefined
True
*Main> f' (\x -> undefined)
True

Question

Does this behavior depend on the more complex seq described at the end of the commentary, and if so, how does it work? Could such an f be written without these primitives?

x  `seq` e2 ==> case seq# x RW of (# x, _ #) -> e2    -- Note shadowing!
e1 `seq` e2 ==> case seq# x RW of (# _, _ #) -> e2

解决方案

There is a more higher-level description of STG-machine in How to make a fast curry: push/enter vs eval/apply

Figure 2 contains rule CASEANY that works for functions. In this paper "is a value" proposition means either:

  • it is a saturated constructor application
  • it is a function
  • it is a partial function application (which is still a function, semantically)

Unboxed values, including literals are treated specially, more information can be found in Unboxed values as first class citizens

All these are implementation details and are hidden inside compiler (GHC). Haskell's case expression doesn't force it's scrutineer, only pattern-matching and seq do.

这篇关于seq force功能如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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