纯函数式语言中调用函数和实例化对象之间有关系吗?

发布于 2024-09-24 09:11:49 字数 818 浏览 3 评论 0原文

想象一种简单的(虚构的)语言,其中的函数如下所示:(

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦忆晨望 2024-10-01 09:11:49

没有比 lambda 演算更纯粹的了: http://en.wikipedia.org/wiki/Lambda_calculus 。 Lambda 演算实际上非常纯粹,它只有函数!

在 lambda 演算中实现pair的标准方法如下:

pair = fn a: fn b: fn x: x a b
first = fn a: fn b: a
second = fn a: fn b: b

所以pair a b,您可能称之为“结构体”,实际上是一个函数(fn x:xa b) >)。但它是一种特殊类型的函数,称为闭包。闭包本质上是一个函数 (fn x: xa b) 加上所有“自由”变量的值(在本例中为 ab代码>)。

所以是的,实例化一个“结构”就像调用一个函数,但更重要的是,实际的“结构”本身就像一种特殊类型的函数(闭包)。

如果您考虑如何实现 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:

pair = fn a: fn b: fn x: x a b
first = fn a: fn b: a
second = fn a: fn b: b

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 and b).

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...

葬シ愛 2024-10-01 09:11:49

fmake-s 都是函数,但相似之处并不多。应用 f 调用该函数并执行其代码;应用make-s创建一个结构。

在大多数语言实现和建模中,make-s 是与 f 不同类型的对象:f 是一个闭包,而 make -s 是构造函数(在函数式语言和逻辑含义上,这与面向对象语言的含义很接近)。

如果你喜欢以面向对象的方式思考,fmake-s 都有一个 apply 方法,但他们对该方法有完全不同的实现。

如果您喜欢考虑底层逻辑,fmake-s 具有基于 samme 类型构造函数(函数类型构造函数)的类型构建,但它们是以不同的方式构造并具有不同的销毁规则(函数应用程序与构造函数应用程序)。

如果您想了解最后一段,我建议您类型和编程语言 作者:本杰明·C·皮尔斯。结构在第 11.8 节中讨论。

Both f and make-s are functions, but the resemblance doesn't go much further. Applying f calls the function and executes its code; applying make-s creates a structure.

In most language implementations and modelizations, make-s is a different kind of object from f: f is a closure, whereas make-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 and make-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 and make-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.

乖乖 2024-10-01 09:11:49

我的玩具模型是否过于简单以至于毫无用处?

本质上是的。您的简化模型基本上可以归结为每个操作都涉及执行计算并将结果放在某处。但这太笼统了,它涵盖了计算机所做的任何事情。如果你不执行计算,你就不会做任何有用的事情。如果你没有将结果放在某个地方,那么你的工作就白费了,因为你无法获得结果。因此,您使用计算机所做的任何有用的事情,从将两个寄存器添加到一起,到获取网页,都可以建模为执行计算并将结果放在以后可以访问的地方。

Is my toy model so oversimplified that it's useless?

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.

硬不硬你别怂 2024-10-01 09:11:49

对象和闭包之间存在关系。 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)

(define (make-account balance)
  (define (withdraw amount)
    (if (>= balance amount)
        (begin (set! balance (- balance amount))
               balance)
        "Insufficient funds"))
  (define (deposit amount)
    (set! balance (+ balance amount))
    balance)
  (define (dispatch m)
    (cond ((eq? m 'withdraw) withdraw)
          ((eq? m 'deposit) deposit)
          (else (error "Unknown request -- MAKE-ACCOUNT"
                       m))))
  dispatch)

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 )

(define (make-account balance)
  (define (withdraw amount)
    (if (>= balance amount)
        (begin (set! balance (- balance amount))
               balance)
        "Insufficient funds"))
  (define (deposit amount)
    (set! balance (+ balance amount))
    balance)
  (define (dispatch m)
    (cond ((eq? m 'withdraw) withdraw)
          ((eq? m 'deposit) deposit)
          (else (error "Unknown request -- MAKE-ACCOUNT"
                       m))))
  dispatch)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文