柯里化和编译器设计

发布于 2024-08-09 19:03:28 字数 205 浏览 3 评论 0原文

这是一个家庭作业问题:

解释类型的转换 例程的部分经历 参数化。

到目前为止我了解柯里化。但我找不到任何关于编译器如何在内存中实现这​​样的函数的资源。我能否指出正确的方向,也许是要搜索的关键字或资源链接,或者可能是编译器如何生成类型和符号表以及与问题相关的其他内容的解释。

谢谢。

This is a homework question:

Explain the transformations the type
of a routine undergoes in partial
parameterization.

So far I understand currying. But I cannot find any resources on how a function like this is implemented by the compiler in memory. Could I be pointed in the right direction, maybe keywords to search for or links to resources or possibly an explanation here of how the compiler generates the type and symbol table among other things thats related to the question.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

dawn曙光 2024-08-16 19:03:28

柯里化是将 n 个参数函数转换为 n 个一元函数:

例如,如果您有一个三元函数 f :: t1 x t2 x t3 -> t 您可以将此函数表示为

f1 :: t1 -> (t2 -> (t3 -> t))

换句话说,f1 是一个函数,它接受类型 t1 的参数并返回类型 f2 的函数。

f2 :: t2 -> (t3 -> t)

f2 是一个函数,它接受类型 t2 的参数并返回类型 f3 的函数。

f3 :: t3 -> t

f3 是一个函数,它接受类型 t3 的参数并返回类型 t。

例如,如果 f(a,b,c) = a+b*c 则:

f3(C) == c1+c2*C where c1 and c2 are constant.
f2(B) == f3(C) where c1 is constant and c2 is replaced with B.
f1(A) == f2(B) where c1 is replaced with A.

在函数式语言中,函数是一等公民,因此通常将它们作为返回类型。

Currying is the conversion of n argument functions into n unary functions:

Example, if you have a ternary function f :: t1 x t2 x t3 -> t you can represent this function as

f1 :: t1 -> (t2 -> (t3 -> t))

In other words, f1 is a function which takes an argument of type t1 and returns a function of type f2.

f2 :: t2 -> (t3 -> t)

f2 is a function which takes an argument of type t2 and returns a function of type f3.

f3 :: t3 -> t

f3 is a function which takes an argument of type t3 and returns type t.

Example, if f(a,b,c) = a+b*c then:

f3(C) == c1+c2*C where c1 and c2 are constant.
f2(B) == f3(C) where c1 is constant and c2 is replaced with B.
f1(A) == f2(B) where c1 is replaced with A.

In functional languages, functions are first class citizens so its common to have them as return type.

吖咩 2024-08-16 19:03:28

柯里化就像固定函数的参数。你真正需要修改的是被调用函数的原型..如果你有例如 retn_type function(param1, param2) 并在第一个参数上柯里化它,你将它设置为一个固定值,然后你获取一个新函数retn_type(param2),该函数可以以与原始函数不同的方式调用和传递。

实际上,您可以通过多种方式或黑客在编译器中获取它,但其简单性的核心只是重新定义链接到第一个版本的新版本。因此,当您调用 retn_type(param2) 时,您将执行与第一个函数相同的代码,假设parameter1 是通过柯里化指定的。

Currying is like fixing a parameter of the function. What you really need to modify is the prototype of the function called.. if you have for example retn_type function(param1, param2) and you currying it on first parameter you set it to a fixed value and you obtain a new function retn_type(param2) that can be called and passed in a different way from the original one.

Actually you can obtain it in a compiler in many ways or hacks but the core of everything to its simplicity is just to redefine a new version that is linked to first one. So when you call retn_type(param2) you execute the same code of first function assuming that parameter1 is specified by curryfication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文