为什么内置函数适用于被认为处于弱头范式的参数太少? [英] Why is a built-in function applied to too few arguments considered to be in weak head normal form?

查看:178
本文介绍了为什么内置函数适用于被认为处于弱头范式的参数太少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell 定义说:


表达式采用弱头标准格式(WHNF),如果它是:


  • a构造函数(最终应用于参数),如True,Just(square 42)或(:) 1

  • 内置函数应用于太少参数(可能是none),如(+)2或sqrt。
  • 或lambda抽象\ x - >表达式。

为什么内置功能会得到特殊待遇?根据lambda微积分,一个部分应用函数和其他函数没有区别,因为最后我们只有一个参数函数。

解决方案

一个正常的函数应用于一个参数,如下所示:

 (\xy  - > x + 1:y)1 

可以减少,给:

  \y  - > 1 + 1:y 

在第一个表达式中,最外层是一个应用程序,所以它不在WHNF中。第二,最外层的是lambda抽象,所以它在WHNF中(尽管我们可以在函数体内进行更多的缩减)。

现在让我们考虑一下应用内置(原始)函数:

 (+)1 

因为这是一个内置的函数体,所以我们可以用 1 替代第一个参数。评估者知道如何评估(+)的完全饱和应用程序,如(+)1 2 。但是,使用 partial 应用的内置函数没有什么可以完成的;我们所能做的就是生成一个描述适用(+)1并等待多一个参数的数据结构,但这正是我们试图减少的地方。所以我们什么都不做。

内置函数是特殊的,因为它们不是由lambda微积分表达式定义的,所以reduce过程不能看到内部的定义。因此,与普通函数应用程序不同,内置函数应用程序必须通过累积参数来减少,直到它们完全饱和(在这种情况下,减少WHNF是通过运行任何内置的魔术实现) 。不能再减少不饱和的内置应用程序,所以已经在WHNF中。


The Haskell definition says:

An expression is in weak head normal form (WHNF), if it is either:

  • a constructor (eventually applied to arguments) like True, Just (square 42) or (:) 1
  • a built-in function applied to too few arguments (perhaps none) like (+) 2 or sqrt.
  • or a lambda abstraction \x -> expression.

Why do built-in functions receive special treatment? According to lambda calculus, there is no difference between a partially applied function and any other function, because at the end we have only one argument functions.

解决方案

A normal function applied to an argument, like the following:

(\x y -> x + 1 : y) 1

Can be reduced, to give:

\y -> 1 + 1 : y

In the first expression, the "outermost" thing was an application, so it was not in WHNF. In the second, the outermost thing is a lambda abstraction, so it is in WHNF (even though we could do more reductions inside the function body).

Now lets consider the application of a built-in (primitive) function:

(+) 1

Because this is a built-in, there's no function body in which we can substitute 1 for the first parameter. The evaluator "just knows" how to evaluate fully "saturated" applications of (+), like (+) 1 2. But there's nothing that can be done with a partially applied built-in; all we can do is produce a data structure describing "apply (+) to 1 and wait for one more argument", but that's exactly what the thing we're trying to reduce is. So we do nothing.

Built-ins are special because they're not defined by lambda calculus expressions, so the reduction process can't "see inside" their definition. Thus, unlike normal functions applications, built-in function applications have to be "reduced" by just accumulating arguments until they are fully "saturated" (in which case reduction to WHNF is by running whatever the magic implementation of the built-in is). Unsaturated built-in applications cannot be reduced any further, and so are already in WHNF.

这篇关于为什么内置函数适用于被认为处于弱头范式的参数太少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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