# 部分求值

部分求值是一种程序特化技术，它能自动将通用程序连同其部分输入转换为一个残差程序——即一个特化版本，该版本预先计算所有仅依赖于已知输入的部分，而将其余部分保留为生成的代码。将残差程序作用于剩余输入所得到的结果，与将原程序作用于全部输入所得到的结果相同，其优势在于执行速度。<sup>[1](https://raspi.itu.dk/~sestoft/pebook/pebook.html)</sup> 具体而言，给定一个源程序 \( p(s,d) = (1+s)+d \)，其静态输入为 \( s = 2 \)，特化过程会化简 \( 1+2 \) 并将第二个加法残差化，从而产生 \( p_{2}(d) = 3+d \)。<sup>[2](https://www.kb.ecei.tohoku.ac.jp/~sumii/pub/onpe-hosc.pdf)</sup> 同样的机制支撑着一条引人注目的编译路径：针对某个源程序对解释器进行特化，即可得到一个目标程序，因此无需手写编译器便能实现编译。<sup>[1](https://raspi.itu.dk/~sestoft/pebook/pebook.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 产生什么 | 一个残差（特化）程序，其在剩余输入上计算的结果与原程序相同<sup>[1](https://raspi.itu.dk/~sestoft/pebook/pebook.html)</sup> |
| 正确性标准 | 对于 \( P(x,y) \) 且已知 \( x=c \)，残差程序 \( P_{c}(y) \) 必须满足 \( \forall y \in Y,\; P(c,y) = P_{c}(y) \)<sup>[3](https://www.cambridge.org/core/services/aop-cambridge-core/content/view/297804C171F96758B178B748358B9672/S0956796800001453a.pdf/div-class-title-on-line-and-off-line-partial-evaluation-semantic-specifications-and-correctness-proofs-a-href-fn01-ref-type-fn-a-div.pdf)</sup> |
| 二村投影 | 第一投影针对源程序特化解释器以产生目标程序；第二投影针对解释器特化特化器以产生编译器；第三投影针对自身特化特化器以产生编译器生成器<sup>[1](https://raspi.itu.dk/~sestoft/pebook/pebook.html)</sup><sup> • </sup><sup>[18](https://iitd.github.io/col728/lec/intro.html)</sup> |
| 核心分析 | 绑定时间分析将操作分类为静态的（在特化时完成）或动态的（在运行时生成代码）<sup>[4](https://hjemmesider.diku.dk/~neil/FLOLAC2008/FlolacNDJslides2.pdf)</sup> |
| 实测加速比 | mix生成的编译器约20倍；RPC编码最高3.7倍；通过weval在SpiderMonkey上达2.17倍<sup>[5](https://doi.org/10.1145/988346.988358)</sup><sup> • </sup><sup>[6](https://imagine.enpc.fr/~marletr/LaBRI/papers/TCS00-Muller-et-al.pdf)</sup><sup> • </sup><sup>[7](https://arxiv.org/html/2411.10559)</sup> |
| 主要失效模式 | 不终止风险、绑定时间冲突、代码爆炸，以及命令式效应的处理<sup>[4](https://hjemmesider.diku.dk/~neil/FLOLAC2008/FlolacNDJslides2.pdf)</sup><sup> • </sup><sup>[8](https://ar5iv.labs.arxiv.org/html/1109.0781)</sup> |

## 工作原理

静态计算与动态计算之间的区分是核心。部分求值器在遍历程序的同时携带已知输入的值；仅依赖于这些值的表达式在特化期间被求值，其中的调用可能被展开。依赖于未知输入的表达式则被残差化：求值器生成代码，在稍后执行该计算。<sup>[1](https://raspi.itu.dk/~sestoft/pebook/pebook.html)</sup><sup> • </sup><sup>[2](https://www.kb.ecei.tohoku.ac.jp/~sumii/pub/onpe-hosc.pdf)</sup> 这一思想被视为递归函数论中克林s-m-n定理的一种实现。<sup>[3](https://www.cambridge.org/core/services/aop-cambridge-core/content/view/297804C171F96758B178B748358B9672/S0956796800001453a.pdf/div-class-title-on-line-and-off-line-partial-evaluation-semantic-specifications-and-correctness-proofs-a-href-fn01-ref-type-fn-a-div.pdf)</sup>

二村投影将此与编译联系起来。写作 \( \mathit{target} = [[\mathit{mix}]]\; \mathit{int}\; \mathit{source} \) 即为第一投影：针对源程序特化解释器产生目标程序。第二投影 \( \mathit{compiler} = [[\mathit{mix}]]\; [\mathit{mix}, \mathit{int}] \)，针对一个固定解释器特化部分求值器本身，产生一个编译器。第三投影 \( \mathit{cogen} = [[\mathit{mix}]]\; [\mathit{mix}, \mathit{mix}] \)，针对自身特化它，产生一个编译器生成器——一个将解释器转换为编译器的程序。<sup>[1](https://raspi.itu.dk/~sestoft/pebook/pebook.html)</sup>

一个忠实的特化器所提供的保证是外延相等：对于程序 \( P(x,y) \) 且已知第一个参数为 \( c \)，残差 \( P_{c}(y) \) 必须对所有 \( y \) 满足 \( P(c,y) = P_{c}(y) \)。<sup>[3](https://www.cambridge.org/core/services/aop-cambridge-core/content/view/297804C171F96758B178B748358B9672/S0956796800001453a.pdf/div-class-title-on-line-and-off-line-partial-evaluation-semantic-specifications-and-correctness-proofs-a-href-fn01-ref-type-fn-a-div.pdf)</sup> 终止性在一般情况下无法保证：特化器不可能既全定义又完备，因为一个完备的特化器在被特化的程序不终止时也无法终止，而完全解决这一问题等同于解决停机问题。<sup>[4](https://hjemmesider.diku.dk/~neil/FLOLAC2008/FlolacNDJslides2.pdf)</sup>

## 实现方式

一个实用的部分求值器分两个主要步骤工作。首先，绑定时间分析（BTA）接受关于哪些程序输入已知的静态/动态描述，并分析这些绑定时间如何在程序中传播，将变量、表达式和调用分类为静态的——在特化时完成——或动态的——为其在运行时生成代码。<sup>[4](https://hjemmesider.diku.dk/~neil/FLOLAC2008/FlolacNDJslides2.pdf)</sup> 其次，特化本身对静态表达式求值、展开静态调用，并为动态表达式生成残差代码。<sup>[4](https://hjemmesider.diku.dk/~neil/FLOLAC2008/FlolacNDJslides2.pdf)</sup>

如此分工将计算从变换阶段转移出去，使特化更简单、更高效。<sup>[3](https://www.cambridge.org/core/services/aop-cambridge-core/content/view/297804C171F96758B178B748358B9672/S0956796800001453a.pdf/div-class-title-on-line-and-off-line-partial-evaluation-semantic-specifications-and-correctness-proofs-a-href-fn01-ref-type-fn-a-div.pdf)</sup> 在实践中，分析者还会提供手工标注以降低不终止的风险；一些系统则使用在线不终止检查，监控静态计算并在终止性面临风险时强制进行不那么彻底的特化。<sup>[4](https://hjemmesider.diku.dk/~neil/FLOLAC2008/FlolacNDJslides2.pdf)</sup> 残差代码的处理同样重要：为防止代码重复，函数和序对等结构数据必须通过插入let绑定来残差化，这一步称为let插入；离线特化器只需对动态表达式插入let。<sup>[2](https://www.kb.ecei.tohoku.ac.jp/~sumii/pub/onpe-hosc.pdf)</sup>

## 起源

一个里程碑式的实践实验是尼尔·D·琼斯（Neil D. Jones）、彼得·塞斯托夫特（Peter Sestoft）和哈拉尔德·森德高（Harald Søndergaard）的《部分求值实验》（"An experiment in partial evaluation"），发表于1985年的ACM SIGPLAN Notices，报告了导向1984年版mix的一系列工作——这显然是最早成功的自应用部分求值器实现，建立在二村（Futamura）对该方法早期表述的基础之上。<sup>[5](https://doi.org/10.1145/988346.988358)</sup><sup> • </sup><sup>[9](https://link.springer.com/article/10.1007/BF01806312)</sup> 标准专著是尼尔·D·琼斯、卡斯滕·K·戈马德（Carsten K. Gomard）和彼得·塞斯托夫特的《部分求值与自动程序生成》（Partial Evaluation and Automatic Program Generation，1993）。<sup>[1](https://raspi.itu.dk/~sestoft/pebook/pebook.html)</sup> 理论背景则更为古老：有工作描述了对计算过程中仅依赖于已赋定的值的部分进行求值，提出了将解释器转换为编译器的算法，并陈述了两个投影，第三个投影出现较晚；这三个等式后来被称为“二村投影”。<sup>[10](https://static.aminer.org/pdf/PDF/001/006/665/partial_evaluation_of_computation_process_an_approach_to_a_compiler.pdf)</sup><sup> • </sup><sup>[11](https://raspi.itu.dk/~sestoft/papers/sestoft-sondergaard-pepm2024-final.pdf)</sup>

## 变体

在线与离线是主要的分野。在线部分求值器在特化过程中根据环境中的值做决策；它在发现更多可归约表达式方面更精确，但效率较低，因为它在特化期间多次检查绑定时间。离线部分求值器首先运行绑定时间分析，决定哪些变量是已知的，然后进行特化；离线方法被认为更简单，但在在线环境中终止性可能更难保证。<sup>[2](https://www.kb.ecei.tohoku.ac.jp/~sumii/pub/onpe-hosc.pdf)</sup><sup> • </sup><sup>[8](https://ar5iv.labs.arxiv.org/html/1109.0781)</sup> 离线特化放弃即时展开决策，转而采用绑定时间标注。<sup>[11](https://raspi.itu.dk/~sestoft/papers/sestoft-sondergaard-pepm2024-final.pdf)</sup> 混合方法结合两者：一种方法使用基于状态的let插入、用cogen方法代替自应用，以及基于类型的表示分析，在[Standard ML](https://www.edgepedia.org/zh/standard-ml)的一个子集上产生的残差程序与在线部分求值一样快，至少是其他方法的两倍。<sup>[2](https://www.kb.ecei.tohoku.ac.jp/~sumii/pub/onpe-hosc.pdf)</sup> 部分求值与分阶段变换密切相关但有所不同；1996年的一份综述将其描述为一种程序优化技术，或许称为程序特化更为恰当。<sup>[12](https://dl.acm.org/doi/10.1145/243439.243447)</sup>

## 应用

经典应用是从解释器推导编译器。1985年的mix实验产生了编译器和编译器生成器（cocom），其运行速度约为未经部分求值的对应物的20倍，且生成的编译器自动执行窥孔优化和常量折叠，将“X :=X+1; Y :=Y+1; X :=X-1”编译为与“Y :=Y+1”相同的代码。在科学计算中，超级计算机工具包项目将部分求值与并行编译相结合，在单个数值运算层面暴露出极细粒度的并行性，使得相比常规编译的程序获得约三个数量级的加速。<sup>[13](https://bitsavers.org/pdf/mit/ai/aim/AIM-1487.pdf)</sup> 在系统软件中，对Sun商业RPC协议的编码层进行特化，在Sun平台上代码速度最高达到原来的3.7倍，在PC平台上最高达到原来的3.5倍。<sup>[6](https://imagine.enpc.fr/~marletr/LaBRI/papers/TCS00-Muller-et-al.pdf)</sup>

机器学习直接使用第一投影：由于朴素推理算法类似于解释器，SIMPL应用第一二村投影将概率推理算法特化为编译代码，<sup>[14](https://ar5iv.labs.arxiv.org/html/1604.04729)</sup> 而将通用神经网络模拟器特化到固定网络拓扑则可改善训练时间。<sup>[1](https://raspi.itu.dk/~sestoft/pebook/pebook.html)</sup> 对于动态语言，基于部分求值的运行时（GraalVM/Truffle）在其基准测试集上相对于V8 JavaScript虚拟机达到0.83倍的加速比，相对于JRuby达3.8倍，相对于GNU R达5倍。<sup>[15](https://dl.acm.org/doi/10.1145/3140587.3062381)</sup> 更近期地，weval部分求值器从编译为[WebAssembly](https://www.edgepedia.org/zh/webassembly)的未经修改的解释器出发，对整个客体语言函数进行提前编译，在SpiderMonkey上获得2.17倍加速，在PUC-Rio Lua解释器上获得1.84倍加速。<sup>[7](https://arxiv.org/html/2411.10559)</sup>

## 局限与替代方案

终止性是结构性的限制。绑定时间划分可能是一致的但不安全，导致部分求值即使对于总是终止的程序也无法终止；实际的解决办法是手工标注和在线不终止检查。<sup>[16](https://pages.cs.wisc.edu/~horwitz/CS704-NOTES/9.PARTIAL-EVALUATION.html)</sup><sup> • </sup><sup>[4](https://hjemmesider.diku.dk/~neil/FLOLAC2008/FlolacNDJslides2.pdf)</sup> 当静态信息与动态信息纠缠在一起时，静态信息丢失，特化过早停止；修复此问题需要称为绑定时间改进的程序重写，而离线部分求值需要更多此类重写，因为它保守地将分析无法推断为静态的一切视为动态。<sup>[8](https://ar5iv.labs.arxiv.org/html/1109.0781)</sup><sup> • </sup><sup>[3](https://www.cambridge.org/core/services/aop-cambridge-core/content/view/297804C171F96758B178B748358B9672/S0956796800001453a.pdf/div-class-title-on-line-and-off-line-partial-evaluation-semantic-specifications-and-correctness-proofs-a-href-fn01-ref-type-fn-a-div.pdf)</sup> 代码爆炸是另一种失效模式：特化器可能生成大量代码而无实际收益，活跃变量分析可识别哪些变量是活跃的从而避免此问题。<sup>[8](https://ar5iv.labs.arxiv.org/html/1109.0781)</sup><sup> • </sup><sup>[16](https://pages.cs.wisc.edu/~horwitz/CS704-NOTES/9.PARTIAL-EVALUATION.html)</sup> 处理可变变量和输入/输出等命令式效应是一个重要问题。<sup>[8](https://ar5iv.labs.arxiv.org/html/1109.0781)</sup> 即便一个简单的常量折叠器也显示了该技术的边界：它将Y := 3+1简化为Y := 4，但不沿数据流传播常量，因此无法将X := 3; Y := X + 1简化为Y := 4。<sup>[17](https://softwarefoundations.cis.upenn.edu/plf-current/PE.html)</sup>

作为替代方案，GraalVM/Truffle系统可与经过重度优化的单语言生产系统竞争。<sup>[15](https://dl.acm.org/doi/10.1145/3140587.3062381)</sup> C++模板系统可被理解为部分求值的一种形式，但部分求值本身尚未被广泛采用，并带有深奥复杂之名。<sup>[8](https://ar5iv.labs.arxiv.org/html/1109.0781)</sup> 分阶段变换是一种密切相关但不同的技术。<sup>[12](https://dl.acm.org/doi/10.1145/243439.243447)</sup>

## References

1. [Jones, Gomard, Sestoft: Partial Evaluation and Automatic Program Generation (online book, 1993; merged with its preface page and full-book PDF copies)](https://raspi.itu.dk/~sestoft/pebook/pebook.html)
2. [A Hybrid Approach to Online and Offline Partial Evaluation (Higher-Order and Symbolic Computation)](https://www.kb.ecei.tohoku.ac.jp/~sumii/pub/onpe-hosc.pdf)
3. [On-line and off-line partial evaluation: semantic specifications and correctness proofs (Journal of Functional Programming)](https://www.cambridge.org/core/services/aop-cambridge-core/content/view/297804C171F96758B178B748358B9672/S0956796800001453a.pdf/div-class-title-on-line-and-off-line-partial-evaluation-semantic-specifications-and-correctness-proofs-a-href-fn01-ref-type-fn-a-div.pdf)
4. [Partial Evaluation: The Futamura Projections (Neil Jones, FLOLAC 2008 slides)](https://hjemmesider.diku.dk/~neil/FLOLAC2008/FlolacNDJslides2.pdf)
5. [An experiment in partial evaluation (Jones, 1985, extended abstract)](https://doi.org/10.1145/988346.988358)
6. [Accurate Program Analyses for Successful Specialization of Legacy System Software (Theoretical Computer Science, 2000; merged with the ACM copy of the Sun RPC specialization paper)](https://imagine.enpc.fr/~marletr/LaBRI/papers/TCS00-Muller-et-al.pdf)
7. [Partial Evaluation, Whole-Program Compilation (arXiv, PLDI 2025 paper)](https://arxiv.org/html/2411.10559)
8. [Tutorial on Online Partial Evaluation (arXiv; merged with William Cook's tutorial PDF copy)](https://ar5iv.labs.arxiv.org/html/1109.0781)
9. [Mix: A self-applicable partial evaluator for experiments in compiler generation (Springer, Higher-Order and Symbolic Computation; merged with the cs.tufts.edu copy of the same paper)](https://link.springer.com/article/10.1007/BF01806312)
10. [Partial Evaluation of Computation Process, An Approach to a Compiler-Compiler (Yoshihiko Futamura, 1971, reprint)](https://static.aminer.org/pdf/PDF/001/006/665/partial_evaluation_of_computation_process_an_approach_to_a_compiler.pdf)
11. [The Genesis of Mix: Early Days of Self-Applicable Partial Evaluation (Sestoft & Søndergaard, PEPM 2024)](https://raspi.itu.dk/~sestoft/papers/sestoft-sondergaard-pepm2024-final.pdf)
12. [An introduction to partial evaluation (ACM Computing Surveys, 1996)](https://dl.acm.org/doi/10.1145/243439.243447)
13. [Partial Evaluation for Scientific Computing (MIT AI Memo 1487, Supercomputer Toolkit project)](https://bitsavers.org/pdf/mit/ai/aim/AIM-1487.pdf)
14. [SIMPL: A DSL for Automatic Specialization of Inference Algorithms (arXiv)](https://ar5iv.labs.arxiv.org/html/1604.04729)
15. [Practical partial evaluation for high-performance dynamic language runtimes (ACM, GraalVM/Truffle)](https://dl.acm.org/doi/10.1145/3140587.3062381)
16. [Partial Evaluation (CS704 course notes, U. Wisconsin)](https://pages.cs.wisc.edu/~horwitz/CS704-NOTES/9.PARTIAL-EVALUATION.html)
17. [Software Foundations (PLF): Partial Evaluation chapter](https://softwarefoundations.cis.upenn.edu/plf-current/PE.html)
18. [iitd.github.io](https://iitd.github.io/col728/lec/intro.html)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/partial-evaluation

English original: https://www.edgepedia.org/partial-evaluation
