# 功能集成（形式化方法）

形式化方法中的功能集成是一种程序开发技术，其中规格说明、实现和正确性证明作为同一个产物一起产生，因此交付的程序是先验正确的，而不是在完成后再做后验检查。构造式立场问的是：给定所需行为的规格说明，如何推导出满足它的算法，而不是在事后拿完成的算法去对照规格说明进行证明。<sup>[1](https://dl.acm.org/doi/10.1007/BF01933419)</sup> 关于命名有一点需要说明：没有标准的形式化方法教材使用或定义“功能集成”这一确切说法；这里将该词视为构造式、基于精化的开发流派的标签，而它与卡珀斯·琼斯（Capers Jones）同名的软件工程度量之间可能存在的同形异义，在已发表文献中尚未解决。

| 关键事实 | 详情 |
|---|---|
| 核心原则 | 正确性是精化的一个特例：被程序精化的规格说明按构造就是正确的<sup>[2](https://lara.epfl.ch/w/_media/sav08:backwright98refinementcalculus.pdf)</sup> |
| 精化定义 | \( P \sqsubseteq Q \) 当且仅当对所有后置条件，\( \mathit{wp}(P, \mathit{post}) \Rightarrow \mathit{wp}(Q, \mathit{post}) \)<sup>[3](https://www.cs.ox.ac.uk/files/3391/PRG70.pdf)</sup> |
| seL4规模 | 8,700行C代码和600行汇编代码在Isabelle/HOL中通过精化验证了功能正确性；约25人年的工作量<sup>[4](https://trustworthy.systems/publications/nicta_full_text/1852.pdf)</sup><sup> • </sup><sup>[5](https://trustworthy.systems/publications/nicta_full_text/6416.pdf)</sup> |
| SHOLIS缺陷证据 | Z证明发现了50个规格说明错误，而在相近工作量下生成测试用例只发现了4个；项目总工作量为19人年<sup>[6](https://userweb.cs.txstate.edu/~rp31/papers/KingHammondChapmanPryor.pdf)</sup> |
| B方法在巴黎地铁14号线（Paris Métro Line 14）上的应用 | 安全关键软件自1998年10月起投入服务；未执行单元测试，而由成功的全局测试取代<sup>[7](https://www.jucs.org/jucs_13_5/formal_methods_theory_becoming/jucs_13_5_0619_0628_abrial.pdf)</sup> |
| CbC与事后证明的证明规模对比 | 对CbC构造的算法所做的证明，其证明节点数少于相应的事后证明；模式匹配耗时近一分钟，而事后证明耗时超过24分钟<sup>[8](https://link.springer.com/chapter/10.1007/978-3-030-16722-6_2)</sup> |

## 工作原理

其机制是把规格说明和可执行代码放在同一种具有最弱前置条件语义的语言中（即用保证其结果的条件来定义一个语句），使开发在同一个演算内通过变换推进。精化演算扩展了迪杰斯特拉（Dijkstra）的卫式命令语言，加入规格语句 \( [\mathit{pre}, \mathit{post}] \)，用来指称程序中尚待开发的部分，并赋予它们与原有语言同样精确的最弱前置条件含义。<sup>[3](https://www.cs.ox.ac.uk/files/3391/PRG70.pdf)</sup> 随后以语义方式定义精化：\( P \sqsubseteq Q \) 当且仅当对所有后置条件，\( \mathit{wp}(P, \mathit{post}) \Rightarrow \mathit{wp}(Q, \mathit{post}) \)。<sup>[3](https://www.cs.ox.ac.uk/files/3391/PRG70.pdf)</sup>

巴克（Back）和冯·赖特（von Wright）把程序语句建模为谓词变换器，并在高阶逻辑中形式化该演算，将程序和规格说明统一视为契约；正确性就是规格说明被程序精化这一特例，而每一步精化都必须保持前一版本的正确性。<sup>[2](https://lara.epfl.ch/w/_media/sav08:backwright98refinementcalculus.pdf)</sup> 一种互补观点直接把验证与综合统一起来：通过在证明中引入元变量，程序从元变量开始，每条证明规则进一步展开其结构，于是验证与综合成为同一活动；同一机制也概括了Veritas小组（Veritas group）的形式综合方法。<sup>[9](https://pure.mpg.de/rest/items/item_1834286/component/file_1857797/content)</sup> 在“证明即程序”传统中，构造性证明可以提取出经过验证的程序。<sup>[10](https://www.cl.cam.ac.uk/%7elp15/MLbook/PDF/chapter6.pdf)</sup>

## 如何实施

实践者从规格说明推导程序，方法是找到一条精化步骤链 \( S \sqsubseteq M_{0} \sqsubseteq M_{1} \sqsubseteq \cdots \sqsubseteq C \)，其中 \( C \) 是可执行代码，并以精化引理目录为指引；在Coq这样的证明助手中，推导以交互方式构造并由机器检查。<sup>[11](https://webspace.science.uu.nl/~swier004/publications/2017-scp-draft.pdf)</sup> 验证精化步骤和霍尔三元组把证明任务拆成更小的问题，从而降低证明复杂度。<sup>[8](https://link.springer.com/chapter/10.1007/978-3-030-16722-6_2)</sup>

B方法遵循同样的模式，但有自己的结构：开发被组织为机器、精化和实现，其基础是带选择公理的策梅洛-弗兰克尔（Zermelo-Fraenkel）集合论和广义替换；精化步骤引入把具体数据与抽象数据联系起来的粘合不变式，由此产生的证明义务由自动和交互式证明过程消解。<sup>[12](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)</sup> ForSyDe把这一模式应用于系统设计：抽象的功能规格说明模型通过保持语义的变换加上非语义设计决策逐步精化，例如把无限缓冲区精化为有限缓冲区；由于两个模型共享同一语义，同样的验证技术贯穿始终。<sup>[13](https://liacs.leidenuniv.nl/~stefanovtp/courses/ES/papers/Ch4_ForSyDe.pdf)</sup>

## 起源

构造式方法由E. W. 迪杰斯特拉在《一种关于程序正确性问题的构造式方法》（"A constructive approach to the problem of program correctness"，《BIT数值数学》，1968）中提出，该文主张控制程序生成过程，以产生先验正确的程序，作为后验正确性方法的替代。<sup>[1](https://dl.acm.org/doi/10.1007/BF01933419)</sup> C. A. R. 霍尔（Hoare）的《计算机程序设计的公理基础》（"An axiomatic basis for computer programming"，《ACM通讯》，1969）提供了公理框架。<sup>[14](https://doi.org/10.1145/363235.363259)</sup> 迪杰斯特拉还论证说，程序测试“可以非常有效地用来表明缺陷的存在，但永远不能表明缺陷的不存在”，因此证明是唯一足够强大的替代方案；他还指出证明长度关键取决于程序结构，这使得缩短证明成为结构化的一项正当目标。<sup>[15](https://www.cs.utexas.edu/~EWD/transcriptions/EWD02xx/EWD317.html)</sup>

精化学派谱系从迪杰斯特拉经巴克、摩根（Morgan）和阿布里亚尔（Abrial）传下，他们的方法从规格说明导出证明义务<sup>[16](http://www.imm.dtu.dk/~dibj/2014/fm40-paper.pdf)</sup>；<sup>[11](https://webspace.science.uu.nl/~swier004/publications/2017-scp-draft.pdf)</sup> 许多算法可以通过从其规格说明进行系统计算而推导出来，游程编码的例子说明了这一点。<sup>[17](https://www.cs.ox.ac.uk/files/3384/PRG64.pdf)</sup> 作为一种有名称的方法论，德里克·G. 库里（Derrick G. Kourie）和布鲁斯·W. 沃森（Bruce W. Watson）于2012年出版了《构造即正确的编程方法》（"The Correctness-by-Construction Approach to Programming"）。<sup>[18](https://doi.org/10.1007/978-3-642-27919-5)</sup>

## 变体

若干有名称的变体实现了这种集成风格。构造即正确（Correctness by construction，CbC）通过一组固定的精化规则开发程序，每条规则都保持正确性。<sup>[18](https://doi.org/10.1007/978-3-642-27919-5)</sup> B方法在Event-B中定型，它带着证明构造模型，使软件按构造就是正确的，建模和形式推理在编码之前进行<sup>[7](https://www.jucs.org/jucs_13_5/formal_methods_theory_becoming/jucs_13_5_0619_0628_abrial.pdf)</sup>；Event-B本身由让-雷蒙·阿布里亚尔（Jean-Raymond Abrial）和斯特凡·哈勒施泰德（Stefan Hallerstede）于2007年在一篇关于离散模型的精化、分解和实例化的论文中提出。<sup>[19](https://doi.org/10.5555/2366448.2366450)</sup> 伊莎贝尔·佩尔塞伊（Isabelle Perseil）和洛朗·波泰（Laurent Pautet）于2010年在《系统与软件工程创新》（Innovations in Systems and Software Engineering）上发表了《软件工程中的形式化方法集成》（"Formal methods integration in software engineering"）。<sup>[20](https://doi.org/10.1007/s11334-009-0115-2)</sup> 一种用于形式化方法集成的轻量级元方法组合异构记号；Z加上谓词式编程的组合同时支持数据变换和过程精化。<sup>[21](http://www.cse.yorku.ca/~paige/Writing/AMAST027.pdf)</sup>

现代验证感知语言把同样的思想带入日常工具。Dafny是一种验证感知编程语言，带有编译器和静态验证器<sup>[22](https://research.chalmers.se/publication/550353/file/550353_Fulltext.pdf)</sup>；Verus使用线性幽灵类型验证[Rust](https://www.edgepedia.org/zh/rust-programming-language)程序，由安德烈亚·拉图阿达（Andrea Lattuada）及其同事于2023年报告。<sup>[23](https://doi.org/10.1145/3586037)</sup>

## 应用

据其验证者所知，seL4微内核是第一个被完整形式化验证功能正确性的通用操作系统内核：它包含8,700行C代码和600行汇编代码，Isabelle/HOL中一个交互式、机器检查的精化证明把抽象规格说明与C实现连接起来，前提是编译器、汇编和硬件正确。<sup>[4](https://trustworthy.systems/publications/nicta_full_text/1852.pdf)</sup> 该项目在规格说明、开发和验证上花费约25人年<sup>[5](https://trustworthy.systems/publications/nicta_full_text/6416.pdf)</sup>，后来又在超过8年的时间里扩展了二进制验证和IPC快速路径功能正确性，所有这些都形式化地连接到同一内核版本。<sup>[24](https://www.sel4.systems/Research/pdfs/comprehensive-formal-verification-os-microkernel.pdf)</sup>

在铁路信号领域，B方法被应用于[巴黎地铁](https://www.edgepedia.org/zh/paris-metro)14号线，该线自1998年10月起运行，也被应用于鲁瓦西—夏尔·戴高乐机场摆渡线（Roissy Charles-de-Gaulle shuttle）；在第二个案例中，自动生成的代码行数大约翻倍，而证明时间减半，并且两个案例都未执行单元测试，而由成功的全局测试取代。<sup>[7](https://www.jucs.org/jucs_13_5/formal_methods_theory_becoming/jucs_13_5_0619_0628_abrial.pdf)</sup> 在SHOLIS项目中——该项目是第一个在1991年版英国国防部临时国防标准00-55和00-56（UK MOD Interim Defence Standards 00-55 and 00-56）下完成的项目——Z证明覆盖约500页、约150个证明，SPARK证明生成超过9,000个验证条件，其中6,800个被自动消解，而Z证明阶段发现了50个规格说明错误，在相近工作量下生成测试用例只发现了4个。<sup>[6](https://userweb.cs.txstate.edu/~rp31/papers/KingHammondChapmanPryor.pdf)</sup>

安东尼·霍尔（Anthony Hall）在构造即正确方面的工业经验报告称，开发明显具有成本效益且缺陷率极低；在一个项目中，规格说明中引入的57个错误在架构阶段被检测并移除。<sup>[25](https://www.jucs.org/jucs_13_5/realising_the_benefits_of/jucs_13_5_0669_0678_hall.pdf)</sup> 已发表的比较结果支持这种集成风格：对CbC构造的算法所做的证明，其证明节点数少于相应的事后证明，模式匹配耗时近一分钟，而事后证明超过24分钟；一项威尔科克森检验（Wilcoxon test）否定了CbC验证与事后验证在验证时间上无差异（\( p = 0.007813 \)）。<sup>[8](https://link.springer.com/chapter/10.1007/978-3-030-16722-6_2)</sup>

## 局限与替代方案

主要成本是证明工作量和规格说明风险。seL4的25人年说明了内核规模上的负担。<sup>[5](https://trustworthy.systems/publications/nicta_full_text/6416.pdf)</sup> CbC风格的设计有一种典型的失效模式：如果从需求导出的性质无法由设计模型满足，就必须放弃设计或修改需求，从而带来额外成本。<sup>[26](https://www-verimag.imag.fr/~sifakis/papers_pdfs/CSSPjournalPaper.pdf)</sup> 经典CbC也很僵硬，把构造限制为一组固定的精化规则、一次应用一条，并且需要CorC之类的特殊工具支持<sup>[27](https://potanin.github.io/files/RungeBordisPotaninThumSchaeferLMCS2023.pdf)</sup>；它仍未进入大规模开发，原因包括缺少工具支持，以及程序员的心态更适合事后验证。<sup>[8](https://link.springer.com/chapter/10.1007/978-3-030-16722-6_2)</sup> 工具的健全性并非免费：Dafny的编译器和验证器都没有被证明为正确，而且两者都发现过健全性缺陷。<sup>[22](https://research.chalmers.se/publication/550353/file/550353_Fulltext.pdf)</sup>

与替代方案相比，静态分析工具被广泛采用，但不能检查功能正确性，而测试对于真实规模的应用程序仍是最实用的方法；今天的程序员很少编写规格说明，即使编写也很少拿代码来验证它们。<sup>[16](http://www.imm.dtu.dk/~dibj/2014/fm40-paper.pdf)</sup> 自动综合的可扩展性很差：完整的LTL综合是2EXPTIME完全的，因此一种混合方法先应用构造即正确的性质强制，只对无法通过构造强制的性质使用模型检验。<sup>[26](https://www-verimag.imag.fr/~sifakis/papers_pdfs/CSSPjournalPaper.pdf)</sup>

近期工作推动证明与其他工作流的集成。Laurel使用大语言模型（LLM）生成的断言来解除自动验证的阻塞，由埃里克·穆尼耶（Eric Mugnier）及其同事于2025年报告。<sup>[28](https://doi.org/10.1145/3720499)</sup> 丹尼尔·内扎马巴迪（Daniel Nezamabadi）、马格努斯·O. 米伦（Magnus O. Myreen）和谭永谦（Yong Kiam Tan）提出了一个经过验证的验证条件生成器和一个经过验证的编译器，用于Dafny的一个命令式子集，在HOL4中机械化，并以CakeML为目标。<sup>[22](https://research.chalmers.se/publication/550353/file/550353_Fulltext.pdf)</sup> 与传统工作流的集成也在成熟：为部署在安卓（Android）中的pKVM虚拟机监控器的内存分配器编写的CN分离逻辑规格说明，与运行时断言检查和SMT驱动的基于属性的测试相结合，而证明工具与测试工具之间的一个空指针差异是由测试发现的。<sup>[29](https://zain-k-aamer.com/papers/codeSpecifyTestDebugProve.pdf)</sup>

## References

1. [A constructive approach to the problem of program correctness (BIT, DOI/publisher page; merged with EWD 209 transcription excerpts)](https://dl.acm.org/doi/10.1007/BF01933419)
2. [Refinement Calculus: A Systematic Introduction (Back & von Wright, 1998, Springer)](https://lara.epfl.ch/w/_media/sav08:backwright98refinementcalculus.pdf)
3. [The specification statement and program refinement (Morgan, TOPLAS 10(3), July 1988; PRG-70)](https://www.cs.ox.ac.uk/files/3391/PRG70.pdf)
4. [seL4: Formal Verification of an OS Kernel (SOSP 2009)](https://trustworthy.systems/publications/nicta_full_text/1852.pdf)
5. [Formal Specifications Better Than Function Points for Code Sizing (seL4 / L4.verified)](https://trustworthy.systems/publications/nicta_full_text/6416.pdf)
6. [Is proof more cost-effective than testing? (SHOLIS project, IEEE Transactions on Software Engineering)](https://userweb.cs.txstate.edu/~rp31/papers/KingHammondChapmanPryor.pdf)
7. [Formal Methods: Theory Becoming Practice (Abrial, JUCS 13(5), 2007)](https://www.jucs.org/jucs_13_5/formal_methods_theory_becoming/jucs_13_5_0619_0628_abrial.pdf)
8. [Tool Support for Correctness-by-Construction (CorC, Springer 2019)](https://link.springer.com/chapter/10.1007/978-3-030-16722-6_2)
9. [Unifying program verification and synthesis (Isabelle-based schematic proofs)](https://pure.mpg.de/rest/items/item_1834286/component/file_1857797/content)
10. [Reasoning About Functional Programs (Paulson, ML for the Working Programmer, ch. 6)](https://www.cl.cam.ac.uk/%7elp15/MLbook/PDF/chapter6.pdf)
11. [Embedding the Refinement Calculus in Coq (Science of Computer Programming draft, 2017)](https://webspace.science.uu.nl/~swier004/publications/2017-scp-draft.pdf)
12. [Foundations of the B Method](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)
13. [ForSyDe: System Design Using a Functional Language and Models of Computation](https://liacs.leidenuniv.nl/~stefanovtp/courses/ES/papers/Ch4_ForSyDe.pdf)
14. [C. A. R. Hoare (1969). An axiomatic basis for computer programming. Communications of the ACM.](https://doi.org/10.1145/363235.363259)
15. [E.W. Dijkstra Archive: On a methodology of design (EWD 317)](https://www.cs.utexas.edu/~EWD/transcriptions/EWD02xx/EWD317.html)
16. [40 Years of Formal Methods (Havelund et al., DTU)](http://www.imm.dtu.dk/~dibj/2014/fm40-paper.pdf)
17. [A Calculus of Functions for Program Derivation (Richard Bird, PRG-64, 1987)](https://www.cs.ox.ac.uk/files/3384/PRG64.pdf)
18. [Derrick G. Kourie, Bruce W. Watson (2012). The Correctness-by-Construction Approach to Programming. .](https://doi.org/10.1007/978-3-642-27919-5)
19. [Jean-Raymond Abrial, Stefan Hallerstede (2007). Refinement, Decomposition, and Instantiation of Discrete Models: Application to Event-B. .](https://doi.org/10.5555/2366448.2366450)
20. [Isabelle Perseil, Laurent Pautet (2010). Formal methods integration in software engineering. Innovations in Systems and Software Engineering.](https://doi.org/10.1007/s11334-009-0115-2)
21. [Case studies in a meta-method for formal method integration (Paige)](http://www.cse.yorku.ca/~paige/Writing/AMAST027.pdf)
22. [Verified VCG and Verified Compiler for Dafny (CPP '26)](https://research.chalmers.se/publication/550353/file/550353_Fulltext.pdf)
23. [Andrea Lattuada and colleagues (2023). Verus: Verifying Rust Programs using Linear Ghost Types. Proceedings of the ACM on Programming Languages.](https://doi.org/10.1145/3586037)
24. [Comprehensive Formal Verification of an OS Microkernel](https://www.sel4.systems/Research/pdfs/comprehensive-formal-verification-os-microkernel.pdf)
25. [Realising the Benefits of Formal Methods (Anthony Hall, JUCS 13(5), 2007)](https://www.jucs.org/jucs_13_5/realising_the_benefits_of/jucs_13_5_0669_0678_hall.pdf)
26. [Early Validation of System Requirements and Design Through Correctness-by-Construction (Sifakis et al.)](https://www-verimag.imag.fr/~sifakis/papers_pdfs/CSSPjournalPaper.pdf)
27. [Flexible Correct-by-Construction Programming (LMCS, 2023)](https://potanin.github.io/files/RungeBordisPotaninThumSchaeferLMCS2023.pdf)
28. [Eric Mugnier and colleagues (2025). Laurel: Unblocking Automated Verification with Large Language Models. Proceedings of the ACM on Programming Languages.](https://doi.org/10.1145/3720499)
29. [Code-Specify-Test-Debug-Prove: Flexibly Integrating Separation Logic Specification into Conventional Workflows (PLDI)](https://zain-k-aamer.com/papers/codeSpecifyTestDebugProve.pdf)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software testing and quality

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/functional-integration-formal-methods

English original: https://www.edgepedia.org/functional-integration-formal-methods
