纯函数式语言中调用函数和实例化对象之间有关系吗?
想象一种简单的(虚构的)语言,其中的函数如下所示:(
function f(a, b) = c + 42
where c = a * b
假设它是 Lisp 的子集,包含“defun”和“let”。)
还想象它包含如下所示的不可变对象:
struct s(a, b, c = a * b)
再次类比 Lisp(这次是superset),说这样的结构定义将生成函数:
make-s(a, b)
s-a(s)
s-b(s)
s-c(s)
现在,考虑到简单的设置,很明显,当您调用“f”或“make-s”时,幕后发生的事情之间有很多相似之处'。一旦在调用/实例化时提供了“a”和“b”,就有足够的信息来计算“c”。
您可以将实例化结构体视为调用函数,然后存储生成的符号环境以供以后调用生成的访问器函数时使用。或者,您可以将计算函数视为创建一个隐藏结构,然后将其用作计算最终结果表达式的符号环境。
我的玩具模型是否过于简单以至于毫无用处?或者它实际上是思考真实语言如何工作的有用方法吗?是否有任何真正的语言/实现是没有 CS 背景但对编程语言感兴趣的人(即我)应该更多地了解以探索这个概念?
谢谢。
编辑:感谢到目前为止的回答。详细一点,我想我想知道是否存在任何真正的语言,学习该语言的人被告知例如“你应该将对象视为本质上的闭包”。或者,如果存在任何真实的语言实现,则实例化对象和调用函数实际上共享一些公共(非平凡的,即不仅仅是库调用)代码或数据结构。
我所做的类比(我知道其他人以前也做过)是否比真实情况下的类比更深入?
Imagine a simple (made up) language where functions look like:
function f(a, b) = c + 42
where c = a * b
(Say it's a subset of Lisp that includes 'defun' and 'let'.)
Also imagine that it includes immutable objects that look like:
struct s(a, b, c = a * b)
Again analogizing to Lisp (this time a superset), say a struct definition like that would generate functions for:
make-s(a, b)
s-a(s)
s-b(s)
s-c(s)
Now, given the simple set up, it seems clear that there is a lot of similarity between what happens behind the scenes when you either call 'f' or 'make-s'. Once 'a' and 'b' are supplied at call/instantiate time, there is enough information to compute 'c'.
You could think of instantiating a struct as being like a calling a function, and then storing the resulting symbolic environment for later use when the generated accessor functions are called. Or you could think of a evaluting a function as being like creating a hidden struct and then using it as the symbolic environment with which to evaluate the final result expression.
Is my toy model so oversimplified that it's useless? Or is it actually a helpful way to think about how real languages work? Are there any real languages/implementations that someone without a CS background but with an interest in programming languages (i.e. me) should learn more about in order to explore this concept?
Thanks.
EDIT: Thanks for the answers so far. To elaborate a little, I guess what I'm wondering is if there are any real languages where it's the case that people learning the language are told e.g. "you should think of objects as being essentially closures". Or if there are any real language implementations where it's the case that instantiating an object and calling a function actually share some common (non-trivial, i.e. not just library calls) code or data structures.
Does the analogy I'm making, which I know others have made before, go any deeper than mere analogy in any real situations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有比 lambda 演算更纯粹的了: http://en.wikipedia.org/wiki/Lambda_calculus 。 Lambda 演算实际上非常纯粹,它只有函数!
在 lambda 演算中实现pair的标准方法如下:
所以pair a b,您可能称之为“结构体”,实际上是一个函数(fn x:xa b) >)。但它是一种特殊类型的函数,称为闭包。闭包本质上是一个函数 (
fn x: xa b
) 加上所有“自由”变量的值(在本例中为a
和b
代码>)。所以是的,实例化一个“结构”就像调用一个函数,但更重要的是,实际的“结构”本身就像一种特殊类型的函数(闭包)。
如果您考虑如何实现 lambda 演算解释器,您可以从另一面看到对称性:您可以将闭包实现为一个表达式加上一个包含所有自由变量值的结构体。
抱歉,如果这一切都很明显,而您只是想要一些现实世界的例子......
You can't get much purer than lambda calculus: http://en.wikipedia.org/wiki/Lambda_calculus. Lambda calculus is in fact so pure, it only has functions!
A standard way of implementing a pair in lambda calculus is like so:
So
pair a b
, what you might call a "struct", is actually a function (fn x: x a b
). But it's a special type of function called a closure. A closure is essentially a function (fn x: x a b
) plus values for all of the "free" variables (in this case,a
andb
).So yes, instantiating a "struct" is like calling a function, but more importantly, the actual "struct" itself is like a special type of function (a closure).
If you think about how you would implement a lambda calculus interpreter, you can see the symmetry from the other side: you could implement a closure as an expression plus a struct containing the values of all the free variables.
Sorry if this is all obvious and you just wanted some real world example...
f
和make-s
都是函数,但相似之处并不多。应用f
调用该函数并执行其代码;应用make-s
创建一个结构。在大多数语言实现和建模中,
make-s
是与f
不同类型的对象:f
是一个闭包,而make -s
是构造函数(在函数式语言和逻辑含义上,这与面向对象语言的含义很接近)。如果你喜欢以面向对象的方式思考,
f
和make-s
都有一个 apply 方法,但他们对该方法有完全不同的实现。如果您喜欢考虑底层逻辑,
f
和make-s
具有基于 samme 类型构造函数(函数类型构造函数)的类型构建,但它们是以不同的方式构造并具有不同的销毁规则(函数应用程序与构造函数应用程序)。如果您想了解最后一段,我建议您类型和编程语言 作者:本杰明·C·皮尔斯。结构在第 11.8 节中讨论。
Both
f
andmake-s
are functions, but the resemblance doesn't go much further. Applyingf
calls the function and executes its code; applyingmake-s
creates a structure.In most language implementations and modelizations,
make-s
is a different kind of object fromf
:f
is a closure, whereasmake-s
is a constructor (in the functional languages and logic meaning, which is close to the object oriented languages meaning).If you like to think in an object-oriented way, both
f
andmake-s
have an apply method, but they have completely different implementations of this method.If you like to think in terms of the underlying logic,
f
andmake-s
have a type build on the samme type constructor (the function type constructor), but they are constructed in different ways and have different destruction rules (function application vs. constructor application).If you'd like to understand that last paragraph, I recommend Types and Programming Languages by Benjamin C. Pierce. Structures are discussed in §11.8.
本质上是的。您的简化模型基本上可以归结为每个操作都涉及执行计算并将结果放在某处。但这太笼统了,它涵盖了计算机所做的任何事情。如果你不执行计算,你就不会做任何有用的事情。如果你没有将结果放在某个地方,那么你的工作就白费了,因为你无法获得结果。因此,您使用计算机所做的任何有用的事情,从将两个寄存器添加到一起,到获取网页,都可以建模为执行计算并将结果放在以后可以访问的地方。
Essentially, yes. Your simplified model basically boils down to saying that each of these operations involves performing a computation and putting the result somewhere. But that is so general, it covers anything that a computer does. If you didn't perform a computation, you wouldn't be doing anything useful. If you didn't put the result somewhere, you would have done work for nothing as you have no way to get the result. So anything useful you do with a computer, from adding two registers together, to fetching a web page, could be modeled as performing a computation and putting the result somewhere that it can be accessed later.
对象和闭包之间存在关系。 http://people.csail.mit.edu/ gregs/ll1-discuss-archive-html/msg03277.html
下面创建的内容有些人可能称之为函数,而另一些人可能称之为对象:
摘自 SICP(http://mitpress.mit。 edu/sicp/full-text/book/book-ZH-21.html)
There is a relationship between objects and closures. http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html
The following creates what some might call a function, and others might call an object:
Taken from SICP ( http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-21.html )