理解 R 中的词法范围 [英] Understanding lexical scoping in R

查看:28
本文介绍了理解 R 中的词法范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这篇论文(ungated copy) 在评估 R 编程语言的设计时,我无法理解关于词法范围(或不存在词法范围)的一个特定示例.

在第 4 页,作者提供了以下使用 with 函数的示例:

with(formaldehyde, carb*optden)

他们接着说:

<块引用>

精明的读者会注意到上面的例子有冲突我们声称 R 是词法范围的.通常情况下,R 是词法范围直到它不是.R首先是一个动态的具有对运行程序数据的完全反射访问的语言和表示.在上面的例子中,执行 with通过反思性地操纵环境.这是通过结合惰性求值、动态名称查找,以及将代码转换为文本并返回的能力:

with.default <- function(env, expr, ...)eval(substitute(expr),env, enclose=parent.frame())

<块引用>

该函数使用 substitute 来检索未评估的解析树它的第二个参数,然后在环境中使用 eval 对其进行评估通过将第一个参数与词法上的封闭环境.‘...’ 用于丢弃任何额外的参数.

在这种情况下使用 with 函数如何违反词法范围的原则?

解决方案

通常在 R 词法范围的上下文中讨论时,自由变量在函数中(即在函数中使用的变量)但未在函数中定义)在函数的父环境中查找,而不是在调用者的环境(也称为父框架)中查找,但 with.default 所以这个例子没有没有说明在这个意义上违反了词法范围.

例如,这说明了词法范围:

x <- 1f <- 函数() xg <- function() { x <- 0;F() }g() # 1

答案是 1,因为 1 是在 f定义 的环境中定义的.如果 R 使用动态范围而不是词法范围,答案应该是 0 (使用调用者的环境).我们可以说明 R 如何像这样模拟动态范围:

f <- function() eval.parent(quote(x))g() # 0

添加:

在下面的评论中@hadley 暗示作者可能已经提到了这样一个事实,即 with.default 的第二个实际参数没有在词法上进行评估,这种解释似乎很可能.with.default 的第二个实参被读入 with.default 函数作为使用 substitute 然后使用 eval 相对于第一个参数进行评估.关于词法范围的定义应该是什么存在一些问题,因为即使在广泛讨论时也很少定义它,但与 R 相关的典型讨论 将其称为对自由变量的处理.参见例如 Gentleman &伊哈卡.

I am reading this paper (ungated copy) on evaluating the design of the R programming language, and am not able to understand a particular example on lexical scoping (or the absence thereof).

On page 4, the authors provide the following example of the use of the with function:

with(formaldehyde, carb*optden)

They go on to say:

The astute reader will have noticed that the above example clashes with our claim that R is lexically scoped. As is often the case, R is lexically scoped up to the point it is not. R is above all a dynamic language with full reflective access to the running program’s data and representation. In the above example, the implementation of with sidesteps lexical scoping by reflectively manipulating the environment. This is done by a combination of lazy evaluation, dynamic name lookup, and the ability turn code into text and back:

with.default <- function(env, expr, ...)
  eval(substitute(expr),env, enclose=parent.frame())

The function uses substitute to retrieve the unevaluated parse tree of its second argument, then evaluates it with eval in the environment constituted by composing the first argument with the lexically enclosing environment. The ‘...’ is used to discard any additional arguments.

How is the use of the with function in this case a violation of the principles of lexical scoping?

解决方案

Normally when discussed in the context of R lexical scoping means that free variables in a function (i.e. variables that are used in a function but not defined in the function) are looked up in the parent environment of the function, as opposed to the environment of the caller (also referred to as the parent frame) but there are no free variables in with.default so the example does not illustrate a violation of lexical scoping in that sense.

For example, this illustrates lexical scoping:

x <- 1
f <- function() x
g <- function() { x <- 0; f() }
g() # 1

The answer is 1 because 1 is defined in the environment that f is defined in. Had R used dynamic scoping rather than lexical scoping the answer would have been 0 (using the environment of the caller). We can illlustrate how R can emulate dynamic scoping like this:

f <- function() eval.parent(quote(x))
g() # 0

ADDED:

In a comment below @hadley suggested that the authors may have been referring to the fact that the second actual argument to with.default is not evaluated lexically and this interpretation seems likely. Instead of being evaluated relative to the surrounding lexical environment the second actual argument of with.default is read into the with.default function as an expression using substitute and then evaluated relative to the first argument using eval. There is some question of what the definition of lexical scoping ought to be as it is rarely defined even when extensively discussed but typical discussions in relation to R refer to it as the treatment of free variables. See for example Gentleman & Ihaka.

这篇关于理解 R 中的词法范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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