环境,(en)闭包和框架如何相关? [英] How are environments, (en)closures, and frames related?

查看:154
本文介绍了环境,(en)闭包和框架如何相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更好地了解环境关闭框架相关。我理解函数闭包包含一个环境,环境包含一个框架和一个框架,框架包含变量,但我有点模糊,如何彼此交互。

I want to better understand how environments, closures, and frames are related. I understand function closures contain an environment, environments contain a frame and an enclosure, and frames contain variables, but I'm a bit fuzzy on how they interact with one another.

也许在函数调用期间发生的一个例子会有帮助吗?或者可能是图表?

Perhaps an example of what's going on during a function call would help? Or maybe a diagram?

推荐答案

UPDATE R-lang定义环境具有框架。我倾向于将框架视为堆栈框架,而不是从名称到值的映射 - 但是当然还有 data.frame 列名称向量(然后一些...)。我认为大部分的混乱来自于原来的S语言(还是S-Plus)没有环境对象,所以所有的框架本质上是什么环境对象现在,除了它们只能作为部分的调用堆栈。

UPDATE R-lang defines an environment as having a frame. I tend to think about frames as stack frames, not as mapping from name to value - but then there is of course the data.frame which maps column names to vectors (and then some...). I think most of the confusion comes from the fact that the original S-language (and still S-Plus) did not have environment objects, so all "frames" were essentially what environment objects are now, except that they could only exists as part of the call stack.

例如,在S-Plus中, sys.nframe sys.nframe返回所有帧列表中当前帧的数字索引。 ...这听起来像一个很多很多像堆栈帧对我...你可以阅读更多关于堆栈帧在这里: a href =http://en.wikipedia.org/wiki/Call_stack#Structure> http://en.wikipedia.org/wiki/Call_stack#Structure

For instance, in S-Plus the doc for sys.nframe says "sys.nframe returns the numerical index of the current frame in the list of all frames." ...that sounds an awful lot like stack frames to me... You can read more about stack frames here: http://en.wikipedia.org/wiki/Call_stack#Structure

我扩展了下面的一些解释,并一致地使用术语堆栈框架(我希望)。

I expanded some of the explanations below and use the term "stack frame" consistently (I hope).

END UPDATE

我会这样解释:


  1. 是将变量名映射到值的对象。每个映射称为绑定。该值可以是实值或promise。环境具有父环境(除了空环境之外)。

  1. An environment is an object that maps variable names to values. Each mapping is called a binding. The value can be either a real value or a promise. An environment has a parent environment (except for the empty environment). When you look up a symbol in an environment and it isn't found, the parent environments are also searched.

承诺是一个未经评估的表达式和环境其中评价表达式。当promise被求值时,它被替换为生成的值。

A promise is an unevaluated expression and an environment in which to evaluate the expression. When the promise is evaluated it is replaced with the generated value.

闭包是一个函数和函数定义的环境。 code> lm 将具有stats命名空间环境,并且用户定义的函数将具有全局环境 - 但是另一个函数 f 函数 g 将具有 g 作为其环境的本地环境。

A closure is a function and the environment that the function was defined in. A function like lm would have the stats namespace environment and a user defined function would have the global environment - but a function f defined within another function g would have the local environment for g as its environment.

栈帧(或激活记录)表示调用堆栈中的条目。每个栈框架都有执行函数的局部环境和函数调用的表达式(使 sys.call 可以工作)。

A stack frame (or activation record) is what represents the entries on the call stack. Each stack frame has the local environment that the function is executed in, and the function call's expression (so that sys.call works).

当执行函数调用时,创建一个局部环境,将其父级设置为闭包的环境,将参数与函数的形式参数进行匹配,并将这些绑定添加到局部环境中承诺)。不匹配的形式参数被赋予函数的默认值(promises)(如果有的话)并标记为missing。然后使用此本地环境和调用表达式创建堆栈帧。

When a function call is executed, a local environment is created with it's parent set to the closure's environment, the arguments are matched against the function's formal arguments and those bindings are added to the local environment (as promises). The unmatched formal arguments are assigned the default values (promises) of the function (if any) and marked as missing. A stack frame is then created with this local environment and the call expression. The stack frame is pushed on the call stack and then the body of the function is evaluated in this local environment.

请注意,在这种情况下,父堆栈框架的环境是 NOT parent.frame sys.frame 函数获取调用堆栈上的环境 - 即调用者的环境和调用者的调用者的环境等...

Note that the parent stack frame's environment is NOT searched in this case. The parent.frame, sys.frame functions gets the environments on the call stack - that is, the caller's environment and the caller's caller's environment etc...

# Here match.fun needs to look in the caller's caller's environment to find what "x" is...
f <- function(FUN) match.fun(FUN)(1:10)
g <- function() { x=sin; y="x"; f(y) }
g() # same as sin(1:10)

# Here we see that the stack frames must also contain the actual call expression
f <- function(...) sys.call()
g <- function(...) f(..., x=42)
g(a=2) # f(..., x = 42)

这篇关于环境,(en)闭包和框架如何相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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