一流延续的缺点

发布于 2024-08-05 21:32:47 字数 170 浏览 5 评论 0原文

针对将延续暴露为第一类对象提出了哪些批评?

我觉得有一流的延续很好。它允许完全控制指令的执行流程。高级程序员可以为某些类型的问题开发直观的解决方案。例如,延续用于管理 Web 服务器上的状态。语言实现可以在延续之上提供有用的抽象。例如,绿色线程。

尽管如此,是否有强烈的理由反对继续一流的课程?

What are some of the criticisms leveled against exposing continuations as first class objects?

I feel that it is good to have first class continuations. It allow complete control over the execution flow of instructions. Advanced programmers can develop intuitive solutions to certain kind of problems. For instance, continuations are used to manage state on web servers. A language implementation can provide useful abstractions on top of continuations. For example, green threads.

Despite all these, are there strong arguments against first class continuations?

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

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

发布评论

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

评论(7

彩虹直至黑白 2024-08-12 21:32:47

现实情况是,许多可以使用延续的有用情况已经被专门的语言结构所涵盖:抛出/捕获、返回、C#/Python 产量。因此,语言实现者实际上并没有太多的动力以可用于推出自己的解决方案的通用形式提供它们。

在某些语言中,广义延续很难有效地实现。基于堆栈的语言(即大多数语言)基本上必须在每次创建延续时复制整个堆栈。

这些语言可以实现某些类似延续的功能,这些功能不会破坏基于堆栈的基本模型,比一般情况更有效,但实现通用延续要困难得多,而且不值得。

函数式语言更有可能实现延续,原因如下:

  1. 它们经常以延续传递方式实现,这意味着“调用堆栈”可能是分配在堆上的链表。这使得将指针作为延续传递到堆栈变得很简单,因为当您弹出当前帧并推送新帧时不需要覆盖堆栈上下文。 (我从未实现过 CPS,但这就是我对它的理解。)
  2. 它们喜欢不可变的数据绑定,这使您的旧延续更加有用,因为您不会更改堆栈在创建它时指向的变量的内容。

由于这些原因,延续很可能主要停留在函数式语言领域。

The reality is that many of the useful situations where you could use continuations are already covered by specialized language constructs: throw/catch, return, C#/Python yield. Thus, language implementers don't really have all that much incentive to provide them in a generalized form usable for roll-your-own solutions.

In some languages, generalized continuations are quite hard to implement efficiently. Stack-based languages (i.e. most languages) basically have to copy the whole stack every time you create a continuation.

Those languages can implement certain continuation-like features, those that don't break the basic stack-based model, a lot more efficiently than the general case, but implementing generalized continuations is quite a bit harder and not worth it.

Functional languages are more likely to implement continuations for a couple of reasons:

  1. They are frequently implemented in continuation passing style, which means the "call stack" is probably a linked list allocated on the heap. This makes it trivial to pass a pointer to the stack as a continuation, since you don't need to overwrite the stack context when you pop the current frame and push a new one. (I've never implemented CPS but that's my understanding of it.)
  2. They favor immutable data bindings, which make your old continuation a lot more useful because you will not have altered the contents of variables that the stack pointed to when you created it.

For these reasons, continuations are likely to remain mostly just in the domain of functional languages.

鸢与 2024-08-12 21:32:47

首先,当涉及到延续时,除了 call/cc 之外还有更多内容。我建议从 Mark Feely 的论文开始:A better API for First Class Continuations

我建议阅读有关控制运算符移位和重置的内容,这是表示连续的不同方式。

First up, there is more then just call/cc when it comes to continuation. I suggest starting with Mark Feelys paper: A better API for first class continuations

Next up I suggest reading about the control operators shift and reset, which is a different way of representing contunations.

野心澎湃 2024-08-12 21:32:47

一个重要的反对意见是实施成本。如果运行时使用堆栈,则一流的延续在某些时候需要堆栈副本。复制成本是可以控制的(参见在存在一流的情况下代表控制延续是一个好的策略),但这也意味着可变变量不能在堆栈上分配。对于函数式或主要函数式(例如,Scheme)语言来说,这不是问题,但是对于 OO 语言来说,这会增加大量开销。

A significant objection is implementation cost. If the runtime uses a stack, then first-class continuations require a stack copy at some point. The copy cost can be controlled (see Representing Control in the Presence of First-Class Continuations for a good strategy), but it also means that mutable variables cannot be allocated on the stack. This isn't an issue for functional or mostly-functional (e.g., Scheme) languages, but this adds significant overhead for OO languages.

紅太極 2024-08-12 21:32:47
  1. 大多数程序员不理解它们。如果您有使用它们的代码,则很难找到能够使用它们的替代程序员。
  2. 延续在某些平台上很难实现。例如,JRuby 不支持延续。
  1. Most programmers don't understand them. If you have code that uses them, it's harder to find replacement programmers who will be able to work with it.
  2. Continuations are hard to implement on some platforms. For example, JRuby doesn't support continuations.
冷心人i 2024-08-12 21:32:47

一流的延续削弱了推理代码的能力,特别是在允许将延续强制分配给变量的语言中,因为闭包的内部可以以毛茸茸的方式再次活跃起来。

比照。 Kent Pitman 对延续的抱怨, unwind-protect 与 call/cc 交互的棘手方式

First-class continuations undermine the ability to reason about code, especially in languages that allow continuations to be imperatively assigned to variables, because the insides of closures can be brought alive again in hairy ways.

Cf. Kent Pitman's complaint about continuations, about the tricky way that unwind-protect interacts with call/cc

赠我空喜 2024-08-12 21:32:47

Call/cc 是高级函数式编程的“goto”(例如此处 )。

Call/cc is the 'goto' of advanced functional programming (a la the example here).

み格子的夏天 2024-08-12 21:32:47

在 ruby​​ 1.8 中,执行速度非常慢。 1.9 中更好,当然大多数方案都内置了它们,并且从一开始就表现良好。

in ruby 1.8 the implementation was extremely slow. better in 1.9, and of course most schemes have had them built in and performing well from the outset.

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