# 延续

延续是一种数据结构或过程，表示从某一给定点起剩余的计算，即该点之后仍需完成的一切。在 [Scheme](https://www.edgepedia.org/zh/scheme-programming-language) 编程语言中，延续是一等值：它们可以被捕获、存入变量、作为参数传递，并在之后被调用，从而使程序能够放弃当前的执行路线，转而恢复先前的某条路线。<sup>[1](https://www.scheme.com/tspl3/control.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 延续表示从某一给定点起剩余的计算；在 Scheme 中，它是一个接受一个参数的过程（在多值上下文中也可接受零个或多个参数）。<sup>[1](https://www.scheme.com/tspl3/control.html)</sup> |
| 捕获 | `call-with-current-continuation`（缩写为 `call/cc`）将当前延续打包为一个逃逸过程，并将其传给自己的过程参数。<sup>[2](https://man.scheme.org/call-with-current-continuation.3scm)</sup> |
| 范围 | 逃逸过程具有无限范围：它可以被存储并任意次调用，这与许多其他语言中的逃逸过程不同，后者只能调用一次。<sup>[2](https://man.scheme.org/call-with-current-continuation.3scm)</sup> |
| 动态范围 | 由于 `call/cc` 的存在，Scheme 中一次调用的动态范围并不总是一段单一、连续的时间；它可以通过调用被捕获的延续而重新进入。<sup>[3](https://standards.scheme.org/corrected-r7rs/r7rs-Z-H-8.html)</sup> |
| 定界控制 | Danvy 和 Filinski 提出的 shift 与 reset 算子捕获定界延续，后者表示剩余计算的一部分而非全部。<sup>[4](https://www.kurims.kyoto-u.ac.jp/~hassei/papers/icfp03.html)</sup> |
| 一次性变体 | 一次性延续只能被调用一次，它消除了多次性延续固有的复制开销。<sup>[5](https://www.cs.tufts.edu/comp/150VM/modules/archive/kent-dybvig/one-shot-continuations.pdf)</sup> |
| 历史地位 | Scheme 是第一个广泛使用并接纳一等逃逸过程的编程语言，此前已知的所有顺序控制结构均可由之合成。<sup>[6](https://small.r7rs.org/attachment/r7rs.pdf)</sup> |

## 在 Scheme 中捕获延续

每当一个 Scheme 表达式被求值时，总有一个延续在等待它的结果，而这个延续代表了该计算完整的默认未来。<sup>[7](https://small.r7rs.org/attachment/r7rs-draft-5-ebook.pdf)</sup> 过程 `call-with-current-continuation`（通常缩写为 `call/cc`）将这个隐含的未来显式化。它接受一个单参数过程，捕获当前延续，并以该延续作为逃逸过程调用这个参数过程。<sup>[2](https://man.scheme.org/call-with-current-continuation.3scm)</sup> 如果逃逸过程之后被调用，它会放弃彼时生效的延续，并恢复逃逸过程创建时生效的那个延续。<sup>[2](https://man.scheme.org/call-with-current-continuation.3scm)</sup>

**常见用途。** `call/cc` 的一个常见用途是从循环或过程体中进行结构化的非局部退出，但同一原语还实现了多种多样的高级控制结构，包括非局部退出、回溯和协程。<sup>[7](https://small.r7rs.org/attachment/r7rs-draft-5-ebook.pdf)</sup><sup> • </sup><sup>[6](https://small.r7rs.org/attachment/r7rs.pdf)</sup> R7RS 标准指出，其 `raise` 和 `guard` 形式为非局部退出提供了更为结构化的机制。<sup>[7](https://small.r7rs.org/attachment/r7rs-draft-5-ebook.pdf)</sup> 从概念上说，Scheme 中相当于活动栈的是一条部分延续链，这条链被称为完整延续；通常，延续树中不在当前链上的部分是垃圾，可以被回收，但若将延续存入一个存活变量，则会使整条链保持存活。<sup>[8](https://docs.scheme.org/schintro/schintro_141.html)</sup>

## 动态范围与 dynamic-wind

一次过程调用的动态范围，从调用开始的那一刻起，到调用返回时结束。在大多数语言中，这是一段单一连续的时间，但在 Scheme 中，由于 `call/cc` 的存在，情况并不总是如此。R7RS 标准将动态范围定义为：当被调用过程体的执行开始时进入；当执行位于该范围之外、而调用了一个在该范围内捕获的延续时，也进入；当被调用过程返回时退出；当执行位于该范围之内、而调用了一个在该范围外捕获的延续时，也退出。<sup>[3](https://standards.scheme.org/corrected-r7rs/r7rs-Z-H-8.html)</sup>

`dynamic-wind` 过程的存在，正是为了让这种可重入性变得可控。它接受三个形实转换程序：每当执行进入对其 `thunk` 调用的动态范围时，就调用 `before`；每当执行离开该范围时，就调用 `after`；两者的调用环境均与调用 `dynamic-wind` 时的动态环境相同。<sup>[3](https://standards.scheme.org/corrected-r7rs/r7rs-Z-H-8.html)</sup> 标准规定了多个 `dynamic-wind` 调用通过延续跳转相互作用时的顺序：如果两个调用的 after 都将被调用，则内层的先运行；如果两个调用的 before 都将被调用，则外层的先运行；如果调用一个延续需要调用某个 `dynamic-wind` 的 before 和另一个 `dynamic-wind` 的 after，则先调用 after。<sup>[9](https://small.r7rs.org/attachment/r7rs-draft-2.pdf)</sup>

## 定界延续

由 `call/cc` 捕获的不定界延续表示整个剩余计算。定界延续（也称部分延续）只表示剩余计算的一部分，直到程序员选定的定界符为止。<sup>[4](https://www.kurims.kyoto-u.ac.jp/~hassei/papers/icfp03.html)</sup><sup> • </sup><sup>[10](https://scholarworks.iu.edu/dspace/bitstreams/45bdcee6-f5fc-4877-bed7-5d2e19ba43b9/download)</sup> 最著名的算子是 Olivier Danvy 和 Andrzej Filinski 提出的 shift 与 reset；reset 界定当前求值上下文，shift 捕获直到该定界符为止的定界延续，并将其具体化为一个函数。<sup>[4](https://www.kurims.kyoto-u.ac.jp/~hassei/papers/icfp03.html)</sup><sup> • </sup><sup>[11](https://link.springer.com/chapter/10.1007/978-3-031-91121-7_12)</sup>

**静态算子与动态算子。** 定界控制算子分为静态和动态两类。静态算子 shift（也写作 \( \mathcal{S} \)）与动态算子 control（也写作 \( \mathcal{F} \)）的区别在于所捕获延续包含多少上下文，这一差异被广度优先遍历所利用，而此前广度优先遍历仅用单行玩具示例加以说明。静态定界延续自然引出控制栈的概念，而动态定界延续则可以解释控制队列的概念。<sup>[12](https://www.sciencedirect.com/science/article/pii/S0167642306000177)</sup> 针对动态算子 control 与 prompt 的类型系统已被给出，且不使用递归类型。<sup>[13](https://www.jstage.jst.go.jp/article/imt/3/2/3_2_316/_article)</sup>

定界延续还被赋予逻辑与等式基础。Kameyama 和 Hasegawa 给出了带 shift 与 reset 的演算的直接公理化，证明了它相对于 CPS 翻译的可靠性与完备性，并给出了一个控制算子演算，其表达能力与 shift 和 reset 相当，且在 Sabry 与 Felleisen 的一等延续理论之上是保守的。<sup>[4](https://www.kurims.kyoto-u.ac.jp/~hassei/papers/icfp03.html)</sup> 在理论之外，定界延续还被应用于编程语言实现之外的领域，例如将量化和极性敏感等自然语言现象作为计算副作用来分析。<sup>[14](https://arxiv.org/abs/cs/0404006)</sup>

## 一次性延续与多次性延续

传统的一等延续机制允许一个被捕获的延续被多次调用；这样的延续称为多次性延续。实践中，大多数延续只被调用一次，这促使了一次性延续的引入：它只能被调用一次，第二次调用即为错误。<sup>[5](https://www.cs.tufts.edu/comp/150VM/modules/archive/kent-dybvig/one-shot-continuations.pdf)</sup> 一次性延续足以满足多次性延续的大多数用途，包括非局部退出、非盲回溯、协程以及用用户代码实现的线程系统，但它们无法实现 Prolog 风格的非确定性，即通过多次调用一个延续来产生额外的值。<sup>[5](https://www.cs.tufts.edu/comp/150VM/modules/archive/kent-dybvig/one-shot-continuations.pdf)</sup> 当一门语言同时支持两种延续时，一次性延续在被捕获为多次性延续的一部分时，必须被提升为多次性状态。<sup>[5](https://www.cs.tufts.edu/comp/150VM/modules/archive/kent-dybvig/one-shot-continuations.pdf)</sup>

## 实现策略

由于延续的存活时间可能超过其捕获时的上下文，指向普通控制栈的指针是不够的：如果控制离开了创建延续的上下文，栈可能被覆盖，恢复延续所需的信息便会丢失。<sup>[5](https://www.cs.tufts.edu/comp/150VM/modules/archive/kent-dybvig/one-shot-continuations.pdf)</sup> 因此，实现采用了若干策略：

- **栈复制。** 在基于栈的 Scheme 系统中，延续通过在捕获时复制栈来实现。惰性栈复制策略则只在延续所需的内容即将丢失时才复制栈，当延续先变成垃圾时，这既减少了运行时间也减少了内存消耗。<sup>[15](https://www.jstage.jst.go.jp/article/jssstconference/19/0/19_0_7F1/_article/-char/en)</sup>
- **栈/堆策略。** 采用栈/堆策略时，延续帧永远只有一份副本，因此被赋值变量的存储可以分配在帧内；而在纯栈复制下，帧在捕获后不得被副作用修改，这就是许多 Scheme 编译器将可赋值变量分配在堆上的原因。<sup>[16](https://citeseerx.ist.psu.edu/document?doi=d995e99d9179e671f6543ef2f22f4743718a1955&repid=rep1&type=pdf)</sup>
- **分段栈。** 为实现 `call/cc`，Chez Scheme 将现有的调用栈本身变为延续，并分配一块新的栈块，该栈块在下溢时调用该延续；虽然对多次性延续而言复制在原理上不可避免，但实现可以将复制从捕获时刻推迟到延续被调用时。<sup>[17](https://ghc-proposals.readthedocs.io/en/latest/proposals/0313-delimited-continuation-primops.html)</sup> 在 Bruggeman、Waddell 和 Dybvig 的分段栈模型中，捕获多次性延续会封存当前栈段中已占用的部分而不复制它，而调用延续则复制所保存的段，并通过拆分大段来加以限制。<sup>[5](https://www.cs.tufts.edu/comp/150VM/modules/archive/kent-dybvig/one-shot-continuations.pdf)</sup>
- **堆分配的延续闭包。** 编译到延续传递风格并使用堆分配的延续闭包，可使捕获为常数时间；一个支持此风格的基于 LLVM 的后端已在 Parallel ML 编译器中实现，实现了常数时间的 `call/cc` 和非常轻量的多线程。<sup>[18](https://arxiv.org/abs/1805.08842)</sup>

**定界算子更廉价。** shift 与 reset 的直接实现避免通过 `call/cc` 来模拟它们。Scheme 48 系统中的一个直接实现基于表示延续的增量栈/堆策略，对于这些算子的典型应用，它显示出空间节省和显著的总体性能提升。<sup>[19](https://www.researchgate.net/publication/2546665_Final_Shift_for_Callcc_Direct_Implementation_of_Shift_and_Reset)</sup>

## References

1. [Control Operations, Section 5.5, The Scheme Programming Language, 3rd ed.](https://www.scheme.com/tspl3/control.html)
2. [Manual page for call-with-current-continuation(3scm)](https://man.scheme.org/call-with-current-continuation.3scm)
3. [Revised^7 Report on the Algorithmic Language Scheme, dynamic-wind section](https://standards.scheme.org/corrected-r7rs/r7rs-Z-H-8.html)
4. [Yukiyoshi Kameyama and Masahito Hasegawa (2003). A sound and complete axiomatization of delimited continuations. ICFP 2003.](https://www.kurims.kyoto-u.ac.jp/~hassei/papers/icfp03.html)
5. [Carl Bruggeman, Oscar Waddell, and R. Kent Dybvig (1996). Representing Control in the Presence of One-Shot Continuations. PLDI 1996.](https://www.cs.tufts.edu/comp/150VM/modules/archive/kent-dybvig/one-shot-continuations.pdf)
6. [Revised^7 Report on the Algorithmic Language Scheme (R7RS Small), full text](https://small.r7rs.org/attachment/r7rs.pdf)
7. [Revised^7 Report on the Algorithmic Language Scheme, draft 5 ebook](https://small.r7rs.org/attachment/r7rs-draft-5-ebook.pdf)
8. [An Introduction to Scheme and its Implementation, call-with-current-continuation](https://docs.scheme.org/schintro/schintro_141.html)
9. [Revised^7 Scheme, draft 2](https://small.r7rs.org/attachment/r7rs-draft-2.pdf)
10. [Yukiyoshi Kameyama. Towards Logical Understanding of Delimited Continuations.](https://scholarworks.iu.edu/dspace/bitstreams/45bdcee6-f5fc-4877-bed7-5d2e19ba43b9/download)
11. [Context-Dependent Effects in Guarded Interaction Trees, Springer Nature](https://link.springer.com/chapter/10.1007/978-3-031-91121-7_12)
12. [Dariusz Biernacki, Olivier Danvy, and Chung-chieh Shan (2006). On the static and dynamic extents of delimited continuations. Science of Computer Programming 60(3): 274-297.](https://www.sciencedirect.com/science/article/pii/S0167642306000177)
13. [A Type System for Dynamic Delimited Continuations, Information and Media Technologies](https://www.jstage.jst.go.jp/article/imt/3/2/3_2_316/_article)
14. [Delimited continuations in natural language: quantification and polarity sensitivity, arXiv](https://arxiv.org/abs/cs/0404006)
15. [Tomoharu Ugawa and colleagues (2002). Lazy Stack Copying for Efficient Implementation of Continuations.](https://www.jstage.jst.go.jp/article/jssstconference/19/0/19_0_7F1/_article/-char/en)
16. [Implementation Strategies for Continuations (Clinger and colleagues)](https://citeseerx.ist.psu.edu/document?doi=d995e99d9179e671f6543ef2f22f4743718a1955&repid=rep1&type=pdf)
17. [Delimited continuation primops, GHC proposals](https://ghc-proposals.readthedocs.io/en/latest/proposals/0313-delimited-continuation-primops.html)
18. [Kavon Farvardin and John Reppy (2018). Compiling with Continuations and LLVM. EPTCS 285.](https://arxiv.org/abs/1805.08842)
19. [Martin Gasbichler and Michael Sperber (2002). Final Shift for Call/cc: Direct Implementation of Shift and Reset. ACM SIGPLAN Notices.](https://www.researchgate.net/publication/2546665_Final_Shift_for_Callcc_Direct_Implementation_of_Shift_and_Reset)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Programming language concepts

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/continuation

English original: https://www.edgepedia.org/continuation
