理解(子、部分、完整、一次性)延续(在过程语言中)

Understanding (sub,partial,full,one-shot) continuations (in procedural languages)

在阅读了我能找到的关于延续的几乎所有内容之后,我仍然无法理解它们。可能是因为所有解释都与 lambda 演算密切相关,我很难理解。

一般来说,延续是对下一步做什么的某种表示,在你完成当前的事情之后,即剩余的计算。

但是,各种各样的事情变得更加棘手。也许你们中的一些人可以帮我解决我在这里的习惯类比,并指出我在理解上的错误。

假设我们的函数表示为对象,为了简单起见:

  1. 我们的解释器有一堆函数调用。
  2. 每个函数调用都有一个用于本地数据和参数的堆栈。
  3. 每个函数调用都有一个要执行的 "instructions" 队列,该队列对本地数据堆栈和队列本身进行操作(并且,也可能对调用者的堆栈进行操作)。

这个比喻类似于XY concatenative language

所以,据我了解:

如有错误请指正

A continuation is the rest of the whole computation (this unfinished queue of instructions + stack of all subsequent computations: queues of callers)

通俗地说,你的理解是正确的。但是,延续作为一个概念是控制状态的抽象,如果调用延续,流程应该达到的状态。它不需要显式包含整个堆栈,只要可以达到状态即可。这是延续的经典定义之一。 Refer to Rhino JavaScript doc.

A partial continuation is the current unfinished queue + some delimited part of the caller stack, up until some point (not the full one, for the whole program)

再一次,非正式地,你的理解是正确的。它也被称为 delimited continuation or composable continuation. In this case, not only the process needs to attain the state represented by the delimited continuation, but it also needs to limit the invocation of the continuation to the limit specified. This is in contrast to full continuation, which starts from the point specified and continues to the end of the control flow. Delimited continuation starts from this point and ends in another identified point only. It is a partial control flow and not the full one, which is precisely why delimited continuation needs to return a value (see the Wikipedia article)。该值通常表示部分执行的结果。

A sub-continuation is the rest of the current instruction queue for currently "active" function

你的理解有点模糊。理解这一点的一种方法是考虑多线程环境。如果有一个主线程,并且在某个时候从它启动了一个新线程,那么一个延续(从主线程或子线程的上下文),它应该代表什么?从那一点开始的子线程和主线程的整个控制流(在大多数情况下没有多大意义)或者只是子线程的控制流? Sub-continuation是一种delimited continuation,从一个点到另一个点的控制流状态表示,是子流程或子流程树的一部分。 Refer to this paper.

A one-shot continuation is such a continuation that can be executed only once, after being reified into an object

As per this paper,你的理解是正确的。该论文似乎没有明确说明这是 full/classical 延续还是定界延续;然而,在接下来的部分中,论文指出完整的延续是有问题的,所以我假设这种类型的延续是定界延续。