# 程序合成

程序合成是指自动寻找一个可执行程序，使其满足用户以规范形式表达的意图，规范可以是输入输出示例、逻辑公式，或带有空洞的部分程序。<sup>[1](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/10/program_synthesis_now.pdf)</sup> 自20世纪50年代人工智能诞生以来，它一直被称为计算机科学的圣杯，<sup>[1](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/10/program_synthesis_now.pdf)</sup> 其现代形式立足于从规范系统地推导出程序。<sup>[2](https://doi.org/10.1145/357084.357090)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 自动构造某种语言中的程序，使其满足以规范形式给出的用户意图<sup>[1](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/10/program_synthesis_now.pdf)</sup> |
| 规范形式 | 公式、参考实现、输入输出对、执行轨迹、演示，或语法草图<sup>[3](https://www.cs.cmu.edu/~aldrich/courses/17-355-18sp/notes/notes13-synthesis.pdf)</sup> |
| 主要搜索技术 | 枚举搜索、约束求解、随机搜索和演绎<sup>[1](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/10/program_synthesis_now.pdf)</sup> |
| 标准问题格式 | SyGuS：背景理论、逻辑正确性公式，以及候选表达式的文法<sup>[4](https://acg.cis.upenn.edu/papers/fmcad13_sygus.pdf)</sup> |
| 大规模市场部署 | FlashFill随Microsoft Excel 2013发布；FlashExtract随Windows 10的PowerShell发布<sup>[5](https://doi.org/10.1145/2858965.2814310)</sup> |
| 符号求解器性能 | cvc5在10分钟内求解了148个SyGuS-IF 2.1基准中的137个，全部验证正确<sup>[6](https://arxiv.org/html/2603.20264)</sup> |
| 大语言模型性能 | 一个1370亿参数的模型在少样本设置下解决了59.6%的MBPP问题；微调额外带来约10个百分点的提升<sup>[7](https://doi.org/10.48550/arxiv.2108.07732)</sup> |

## 工作原理

合成器沿三个维度来描述：意图（规范）、程序空间（通常是领域特定语言，DSL），以及在该空间中寻找匹配意图的程序的搜索技术。<sup>[1](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/10/program_synthesis_now.pdf)</sup> 规范可以是公式、参考实现、输入输出对、执行轨迹、演示，或语法草图。<sup>[3](https://www.cs.cmu.edu/~aldrich/courses/17-355-18sp/notes/notes13-synthesis.pdf)</sup>

四种搜索范式占主导地位。枚举搜索是自底向上的，先产生较小的子表达式再产生较大的，并用示例进行剪枝；演绎搜索是自顶向下的，先确定表达式的顶部，再搜索其子表达式。<sup>[1](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/10/program_synthesis_now.pdf)</sup> 基于约束的（基于组件的）合成将组件及其连接编码为关于良构性和功能性的约束，调用SMT求解器，并从满足赋值中重构程序。<sup>[3](https://www.cs.cmu.edu/~aldrich/courses/17-355-18sp/notes/notes13-synthesis.pdf)</sup> [随机](https://www.edgepedia.org/zh/stochastic)搜索和演绎补全了这一集合。<sup>[1](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/10/program_synthesis_now.pdf)</sup>

正确性保证取决于范式：演绎合成的程序依构造即可证明正确，而归纳合成的程序只是与已见示例一致的假设。<sup>[8](https://arxiv.org/pdf/2508.00013)</sup> 反例引导的归纳合成（CEGIS）通过让归纳学习器与验证器交替工作来弥合这一差距，验证器返回当前候选失败的反例。<sup>[8](https://arxiv.org/pdf/2508.00013)</sup>

## 实现方法

最标准化的工作流程是语法引导合成（SyGuS）。一个问题实例包含四个部分：基础理论、函数体待合成的带类型的合成函数、约束语法的逐函数文法，以及必须普遍成立的语义约束公式。<sup>[9](https://sygus-org.github.io/assets/pdf/SyGuS-IF.pdf)</sup> 输入格式紧密模仿SMT-Lib2，使用诸如(set-logic LIA)、(synth-fun ...)、(constraint ...)和(check-synth)等命令；支持的逻辑包括LIA（线性整数算术）、BV（位向量）、Reals和Arrays。<sup>[9](https://sygus-org.github.io/assets/pdf/SyGuS-IF.pdf)</sup>

SyGuS论文描述了CEGIS的三种实例化：一种枚举技术，生成规模递增的候选并用输入输出示例剪枝；一种符号技术，将解析树编码为SMT求解器的约束；以及随机搜索。<sup>[4](https://acg.cis.upenn.edu/papers/fmcad13_sygus.pdf)</sup>

## 起源

将算法与证明等同起来的思想在构造主义数学中很早就被提出，自动合成系统在最早的定理证明器之后不久便出现了；C. Green和R. Waldinger的开创性工作紧随其后，约在1969年。<sup>[10](https://www.cs.cornell.edu/info/people/kreitz/PDF/98kluwer-synthesis.pdf)</sup> 随后，Zohar Manna和Richard J. Waldinger在一系列论文中发展了演绎框架：“迈向自动程序合成”（《ACM通讯》，1971年），<sup>[11](https://doi.org/10.1145/362566.362568)</sup> “合成：梦想 → 程序”（《IEEE软件工程汇刊》，1979年），<sup>[12](https://doi.org/10.1109/tse.1979.234198)</sup> 以及“程序合成的演绎方法”（ACM TOPLAS，1980年），后者将合成视为结合变换规则、合一和数学归纳的定理证明。<sup>[2](https://doi.org/10.1145/357084.357090)</sup> 他们于1981年将该方法应用于合一算法。<sup>[13](https://doi.org/10.21236/ada107328)</sup>

该领域一直非常活跃，直到20世纪80年代初，各种方法未能扩展，随着逻辑编程的出现，研究活动有所下降；后来，大规模合成随着KIDS系统成为可能，该系统接近商业突破。<sup>[10](https://www.cs.cornell.edu/info/people/kreitz/PDF/98kluwer-synthesis.pdf)</sup> 现代复兴始于有限程序的组合草图（Solar-Lezama及同事，2006年），<sup>[14](https://doi.org/10.1145/1168917.1168907)</sup> 以及2013年的SyGuS形式化。<sup>[4](https://acg.cis.upenn.edu/papers/fmcad13_sygus.pdf)</sup>

## 变体

Sketch让程序员编写一个部分程序，编码解决方案的结构，同时将底层细节留给合成器，这些细节由参考实现或测试例程指定；其三个显著的语言特性是未知常量、测试框架和生成器函数。<sup>[15](https://people.csail.mit.edu/asolar/papers/Solar-Lezama09.pdf)</sup> FlashFill从输入输出示例合成字符串处理程序（Gulwani，2011年），<sup>[16](https://doi.org/10.1145/1925844.1926423)</sup> FlashMeta/PROSE框架（Polozov和Gulwani，2015年）实现了数据驱动的领域特定演绎的D4方法论，其主要创新是见证函数，将DSL操作符上基于示例的规范向下传播为其参数上的规范。<sup>[5](https://doi.org/10.1145/2858965.2814310)</sup> Rosette是一个用于求解器辅助编程的虚拟机；与Sketch和PROSE一起，它是最流行的合成框架之一。<sup>[1](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/10/program_synthesis_now.pdf)</sup>

神经符号变体将学习与搜索配对。DeepCoder（Balog及同事，2016年）训练程序组件的神经预测器以加速符号搜索。<sup>[17](https://doi.org/10.48550/arxiv.1611.01989)</sup> SketchAdapt（Nye及同事，2019年）使用学习到的草图生成器引导束搜索，对于难以识别的部分回退到快速枚举，无需直接监督即可学习分工。<sup>[18](https://doi.org/10.48550/arxiv.1902.06349)</sup> 大语言模型也可以充当直接合成器：一个1370亿参数的模型使用少样本学习合成了59.6%的MBPP问题的解决方案。<sup>[7](https://doi.org/10.48550/arxiv.2108.07732)</sup> 混合方法将LLM调用集成到枚举搜索中：HySynth（Barke及同事，2024年）使用上下文无关的LLM近似来引导程序合成。<sup>[19](https://doi.org/10.48550/arxiv.2405.15880)</sup>

## 应用

FlashFill作为[Microsoft Excel](https://www.edgepedia.org/zh/microsoft-excel) 2013的一项功能发布，FlashExtract随Windows 10的PowerShell发布；开发一个健壮的FlashFill原型花了一年时间，而Excel产品团队又花了六个月才使其达到生产就绪状态。<sup>[5](https://doi.org/10.1145/2858965.2814310)</sup> 在过去十年中，若干编程示例系统已部署在大众市场工业产品中。<sup>[1](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/10/program_synthesis_now.pdf)</sup> SyGuS将一系列先前的工作统一在一个问题格式下：程序草图、无循环程序合成、从示例合成Excel宏、程序去混淆，以及超优化，后者根植于Massalin 1987年的超级优化器，用于暴力发现最小指令序列。<sup>[4](https://acg.cis.upenn.edu/papers/fmcad13_sygus.pdf)</sup><sup> • </sup><sup>[20](https://doi.org/10.1145/36205.36194)</sup>

## 局限与替代方案

歧义是编程示例的主要障碍：有限的I/O示例集不足以完全指定意图，因此许多一致的程序在未见输入上表现不同。<sup>[8](https://arxiv.org/pdf/2508.00013)</sup> 搜索随程序规模呈指数增长；合成器通过最小化、高度约束的DSL、观察等价剪枝和见证函数演绎来管理这一点。<sup>[8](https://arxiv.org/pdf/2508.00013)</sup> SyGuS将规范限制为SMT可表达的理论，限制了具有任意语义的领域，如CSS选择器。<sup>[5](https://doi.org/10.1145/2858965.2814310)</sup> LLM经常在语法上失败，增强测试在26个LLM上将pass@k降低了高达19.3至28.9%，而超过10%的原始[HumanEval](https://www.edgepedia.org/zh/humaneval)标准答案本身实现有误；pass@k无法反映代码质量、安全性或可维护性。<sup>[6](https://arxiv.org/html/2603.20264)</sup><sup> • </sup><sup>[21](https://proceedings.neurips.cc/paper_files/paper/2023/file/43e9d647ccd3e4b7b5baab53f0368686-Paper-Conference.pdf)</sup><sup> • </sup><sup>[22](https://link.springer.com/article/10.1007/s10489-026-07230-0)</sup>

归纳逻辑编程归纳出一个泛化示例和背景知识的逻辑程序，其基本问题是对大型假设空间的高效搜索；Popper在生成-测试-约束阶段工作，而大多数神经ILP方法需要元规则，且不支持谓词发明、递归和溯因。<sup>[23](https://link.springer.com/article/10.1007/s10994-021-06089-1)</sup> 模式引导合成在模式设计时将困难的证明义务移到离线，给出比演绎合成小得多的搜索空间；从示例进行归纳合成有过泛化的风险（全称谓词涵盖所有拼接示例），负例是通用的补救措施。<sup>[24](https://webperso.info.ucl.ac.be/~yde/Papers/lopstr04.pdf)</sup>

混合系统现在将验证和约束集成到生成中：约束引导解码将语法和语义约束注入令牌采样，ARCHCODE表明显式前置条件和后置条件作为提示约束可提高准确性，ROCODE将回溯与程序分析集成。<sup>[22](https://link.springer.com/article/10.1007/s10489-026-07230-0)</sup> 在SyGuS领域，将LLM调用集成到加权概率枚举搜索中的混合方法优于单独的cvc5和GPT-3.5。<sup>[6](https://arxiv.org/html/2603.20264)</sup> 一项对67篇关于基于搜索的合成的论文的综述发现，此类方法比LLM需要更少的训练数据并强制执行文法约束，但扩展性差且生成的代码可读性较低，这推动了LLM加搜索的组合。<sup>[25](https://www.mdpi.com/2078-2489/16/5/401)</sup> 关于代码的人类自然语言反馈将大型模型初始预测的错误率减半。<sup>[7](https://doi.org/10.48550/arxiv.2108.07732)</sup>

## References

1. [Program Synthesis (Gulwani, Polozov, Singh, Foundations and Trends in Programming Languages survey, full PDF)](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/10/program_synthesis_now.pdf)
2. [Zohar Manna, Richard Waldinger (1980). A Deductive Approach to Program Synthesis. ACM Transactions on Programming Languages and Systems.](https://doi.org/10.1145/357084.357090)
3. [Lecture Notes: Program Synthesis (CMU 17-355)](https://www.cs.cmu.edu/~aldrich/courses/17-355-18sp/notes/notes13-synthesis.pdf)
4. [Syntax-Guided Synthesis (Alur et al., FMCAD 2013)](https://acg.cis.upenn.edu/papers/fmcad13_sygus.pdf)
5. [Oleksandr Polozov, Sumit Gulwani (2015). FlashMeta: a framework for inductive program synthesis. ACM SIGPLAN Notices.](https://doi.org/10.1145/2858965.2814310)
6. [Can LLMs Perform Synthesis? (LLMs vs. symbolic tools on SyGuS, LTL, TLA+, ACL2s)](https://arxiv.org/html/2603.20264)
7. [Austin, Jacob and colleagues (2021). Program Synthesis with Large Language Models. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2108.07732)
8. [Comparative literature review of program synthesis paradigms (arXiv survey, 2025)](https://arxiv.org/pdf/2508.00013)
9. [Language to Specify Syntax-Guided Synthesis Problems (SyGuS-IF)](https://sygus-org.github.io/assets/pdf/SyGuS-IF.pdf)
10. [Program synthesis chapter (Kreitz, Kluwer volume, 1998)](https://www.cs.cornell.edu/info/people/kreitz/PDF/98kluwer-synthesis.pdf)
11. [Zohar Manna, Richard J. Waldinger (1971). Toward automatic program synthesis. Communications of the ACM.](https://doi.org/10.1145/362566.362568)
12. [Z. Manna, R. Waldinger (1979). Synthesis: Dreams → Programs. IEEE Transactions on Software Engineering.](https://doi.org/10.1109/tse.1979.234198)
13. [Zohar Manna, Richard Waldinger (1981). Deductive Synthesis of the Unification Algorithm,. .](https://doi.org/10.21236/ada107328)
14. [Armando Solar-Lezama and colleagues (2006). Combinatorial sketching for finite programs. ACM SIGOPS Operating Systems Review.](https://doi.org/10.1145/1168917.1168907)
15. [The Sketching Approach to Program Synthesis (Solar-Lezama, MIT)](https://people.csail.mit.edu/asolar/papers/Solar-Lezama09.pdf)
16. [Sumit Gulwani (2011). Automating string processing in spreadsheets using input-output examples. ACM SIGPLAN Notices.](https://doi.org/10.1145/1925844.1926423)
17. [Balog, Matej and colleagues (2016). DeepCoder: Learning to Write Programs. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1611.01989)
18. [Nye, Maxwell and colleagues (2019). Learning to Infer Program Sketches. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1902.06349)
19. [Barke, Shraddha and colleagues (2024). HYSYNTH: Context-Free LLM Approximation for Guiding Program Synthesis. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2405.15880)
20. [Henry Massalin (1987). Superoptimizer: a look at the smallest program. ACM SIGPLAN Notices.](https://doi.org/10.1145/36205.36194)
21. [Is Your Code Generated by ChatGPT Really Correct? (EvalPlus / HumanEval+, NeurIPS 2023)](https://proceedings.neurips.cc/paper_files/paper/2023/file/43e9d647ccd3e4b7b5baab53f0368686-Paper-Conference.pdf)
22. [Code generation with large language models: a survey from neural program synthesis to autonomous software development (Applied Intelligence, Springer)](https://link.springer.com/article/10.1007/s10489-026-07230-0)
23. [Inductive logic programming at 30 (Machine Learning journal)](https://link.springer.com/article/10.1007/s10994-021-06089-1)
24. [Synthesis of Programs in Computational Logic (LOPSTR 2004)](https://webperso.info.ucl.ac.be/~yde/Papers/lopstr04.pdf)
25. [Review and Mapping of Search-Based Approaches for Program Synthesis (Information, 2025)](https://www.mdpi.com/2078-2489/16/5/401)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/program-synthesis

English original: https://www.edgepedia.org/program-synthesis
