# 形式化规范

形式化规范是对系统应当做什么的描述，用具有数学定义的语法、语义和证明论的语言写成，因而关于该描述的论断可以通过证明而非直觉来检验。<sup>[1](https://webperso.info.ucl.ac.be/~avl/files/FormalSpec-avl.pdf)</sup> 当一种方法的技术和工具可以用数学来解释时，该方法就算作形式化的。<sup>[2](https://havelund.com/Publications/fm-50-2022.pdf)</sup> 所产生的产物是行为的数学模型，通常充当模块使用者与实现者之间的契约，双方无需了解对方的设计即可验证正确性。<sup>[3](https://lamport.org/pubs/spec.pdf)</sup> 形式化规范支撑着所谓形式化方法的活动：规范分析与证明、变换式开发以及程序验证，所有这些都以集合论、逻辑和代数等离散数学为基础。<sup>[4](https://ifs.host.cs.st-andrews.ac.uk/Books/SE9/WebChapters/PDF/Ch_27_Formal_spec.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 使规范成为形式化的因素 | 一种语言，具备语法合式性（语法）、解释（语义）和推理（证明论）的规则 <sup>[1](https://webperso.info.ucl.ac.be/~avl/files/FormalSpec-avl.pdf)</sup> |
| 典型的系统规范形式 | TLA+：Init ∧ □[Next]v ∧ L，包含初始谓词、下一状态的动作析取以及公平性条件 <sup>[5](https://members.loria.fr/Stephan.Merz/talks/argentina2005/handout.pdf)</sup> |
| 主要风格 | 模型导向（具体数据类型）与代数式（带公理的抽象载体集）；顺序系统语言与并发系统语言 <sup>[2](https://havelund.com/Publications/fm-50-2022.pdf)</sup><sup> • </sup><sup>[6](https://dl.acm.org/doi/10.1145/242223.242257)</sup> |
| seL4 内核验证 | 200,000 行 Isabelle 证明，约 20 人年，验证发现 144 个缺陷 <sup>[7](https://dl.acm.org/doi/10.1145/1629575.1629596)</sup> |
| AWS DynamoDB | 用几周时间写出 TLA+ 规范；在 10 个 EC2 实例上分布式运行 TLC，发现一个数据丢失缺陷 <sup>[8](https://fi.upm.es/docs/estudios/2461_aws-formal-methods.pdf)</sup> |
| 巴黎地铁 14 号线 | 约 100,000 行 B 规范，28,000 个引理自动证明，常规测试未发现错误 <sup>[1](https://webperso.info.ucl.ac.be/~avl/files/FormalSpec-avl.pdf)</sup> |
| LLM 生成的规范 | 在评估的 30 个模型中，语法正确率最高 26.6%，但语义正确率仅 8.6% <sup>[9](https://arxiv.org/abs/2606.05792)</sup> |

## 工作原理

大多数规范语言都建立在一个很小的数学核心之上。[TLA+](https://www.edgepedia.org/zh/tla) 基于无类型 ZF 集合论、一阶逻辑和动作时态逻辑；典型的规范具有形式 \( Init \wedge \Box[Next]_{v} \wedge L \)，其中 \( Init \) 描述初始状态，\( Next \) 是动作的析取，\( L \) 是公平性条件的合取。<sup>[10](https://lamport.azurewebsites.net/pubs/spec-and-verifying.pdf)</sup> 动作公式用未加撇的变量表示一步的第一个状态，用加撇的变量表示其第二个状态。<sup>[10](https://lamport.azurewebsites.net/pubs/spec-and-verifying.pdf)</sup>

性质分为安全性性质——程序被允许做什么——和活性性质——程序最终必须做什么；<sup>[3](https://lamport.org/pubs/spec.pdf)</sup> 活性性质由 Bowen Alpern 和 [Fred B. Schneider](https://www.edgepedia.org/zh/fred-b-schneider) 于 1985 年给出形式化定义。<sup>[11](https://doi.org/10.1016/0020-0190%2885%2990056-0)</sup> 在 TLA 中，实现、满足和精化都表示同一件事，即逻辑蕴涵：\( S_{1} \) 实现 \( S_{2} \) 当且仅当 \( S_{1} \Rightarrow S_{2} \) 是有效公式。<sup>[12](https://lamport.azurewebsites.net/pubs/spec-book-chap.pdf)</sup>

## 如何完成

规范编写者对状态和下一状态关系进行建模，陈述不变式，并用归纳法证明它们。要证明谓词 \( I \) 是规范 \( S \) 的不变式，需通过找到一个 \( Next \) 的不变式 \( Inv \) 来证明 \( S \Rightarrow \Box I \)，该 \( Inv \) 满足 \( Init \Rightarrow Inv \)、\( Inv \wedge Next \Rightarrow Inv' \) 以及 \( Inv \Rightarrow I \)。<sup>[10](https://lamport.azurewebsites.net/pubs/spec-and-verifying.pdf)</sup> 基本证明规则从 \( I \wedge Next \Rightarrow I' \) 和 \( I \wedge v' = v \) 推导出 \( I \wedge \Box[Next]_{v} \Rightarrow \Box I \)；为使归纳证明通过，不变式通常必须被加强。<sup>[5](https://members.loria.fr/Stephan.Merz/talks/argentina2005/handout.pdf)</sup>

诸如 TLC 之类的[模型检验](https://www.edgepedia.org/zh/model-checking)器会探索所有可达状态以检验安全性性质，检查不变式和死锁，并在违反时打印一条保证最短长度的错误迹。<sup>[10](https://lamport.azurewebsites.net/pubs/spec-and-verifying.pdf)</sup> 随后，精化分两个阶段将规范推向代码：先是一份高层文档，再是一份用守卫命令语言写的低层文档，将数据精化与操作精化分开，并带有诸如 \( pre[A] \subseteq pre[B] \) 的证明义务。<sup>[13](https://www.cs.ox.ac.uk/files/3414/PRG101.pdf)</sup>

## 起源

形式化规范源于定义编程语言的工作。维也纳定义语言（VDL）用一种递归抽象解释器来描述语言语义，该解释器接受一个程序和起始状态并计算最终状态；维也纳开发方法（VDM）从这项工作演化而来，涵盖的范围远比语言语义广泛。<sup>[14](https://www.jucs.org/jucs_7_8/the_transition_from_VDL/Jones_C_B.pdf)</sup> 后来的一份综述将 VDM 归功于 Bjørner 和 Jones 1978 年的元语言工作，视其为最早的形式化方法之一。<sup>[15](https://ar5iv.labs.arxiv.org/html/1911.02564)</sup> VDM 中旨在用于形式化程序开发的部分最早以书的形式出现在 Cliff B. Jones 1980 年的《软件开发：一种严谨的方法》中。<sup>[14](https://www.jucs.org/jucs_7_8/the_transition_from_VDL/Jones_C_B.pdf)</sup> Z 围绕集合类型、关系、函数和模式来组织。<sup>[16](https://courses.cs.washington.edu/courses/cse503/00sp/zrm.pdf)</sup> Abrial 早期的 Z 论文确立了其原则：继承自数学实践的严格形式化、以集合论作为形式化的可靠基础，以及对形式文本的强结构化。<sup>[17](https://se.inf.ethz.ch/~meyer/publications/languages/Z_original.pdf)</sup> TLA+ 归功于 [Leslie Lamport](https://www.edgepedia.org/zh/leslie-lamport) 2002 年的著作《Specifying Systems》；<sup>[18](https://link.springer.com/chapter/10.1007/978-3-319-33600-8_13)</sup> Alloy 源自 Z，旨在解决其缺乏自动工具支持的问题；<sup>[19](https://people.csail.mit.edu/dnj/publications/alloy-journal.pdf)</sup> Event-B 归功于 Abrial 2010 年的《Modeling in Event-B》。<sup>[20](https://doi.org/10.1017/cbo9781139195881)</sup> 时态逻辑模型检验由 E. M. Clarke、E. A. Emerson 和 A. P. Sistla 于 1986 年报告，<sup>[21](https://doi.org/10.1145/5397.5399)</sup> SPIN 模型检验器由 G. J. Holzmann 于 1997 年报告。<sup>[22](https://doi.org/10.1109/32.588521)</sup>

## 变体

规范语言在若干维度上有所不同。模型导向的规范使用具体数据类型，如数、集合、列表和映射，操作定义为这些类型上的函数；代数式规范使用抽象载体集，并在操作签名上带有公理。<sup>[2](https://havelund.com/Publications/fm-50-2022.pdf)</sup> 面向顺序系统的语言，如 Z、VDM 和 Larch，使用集合、关系、函数以及前置条件和后置条件，而 CSP、CCS、Statecharts、时态逻辑和 I/O 自动机则针对并发行为。<sup>[6](https://dl.acm.org/doi/10.1145/242223.242257)</sup> Larch 由 John V. Guttag、James J. Horning 及其同事于 1993 年提出。<sup>[23](https://doi.org/10.1007/978-1-4612-2704-5)</sup> Z 规范由基于数学工具包的模式构建，并带有面向顺序系统的操作精化和数据精化。<sup>[16](https://courses.cs.washington.edu/courses/cse503/00sp/zrm.pdf)</sup> VDM 包括带模块的 [VDM-SL](https://www.edgepedia.org/zh/vdm-sl) 和面向对象的 VDM++，并使用一种称为具体化的精化过程。<sup>[15](https://ar5iv.labs.arxiv.org/html/1911.02564)</sup> Event-B 用集合和函数对状态建模，用事件对状态变化建模，要求证明精化保持不变式，已被用于工业交通、航空航天和汽车项目。<sup>[20](https://doi.org/10.1017/cbo9781139195881)</sup><sup> • </sup><sup>[15](https://ar5iv.labs.arxiv.org/html/1911.02564)</sup> TLA+ 是一种基于模型的反应式与分布式系统语言，与面向性质的公理化风格相对。<sup>[5](https://members.loria.fr/Stephan.Merz/talks/argentina2005/handout.pdf)</sup>

## 应用

量化程度最高的部署是 seL4，这是一个第三代 L4 血统的微内核，包含 8,700 行 C 代码和 600 行汇编代码，其 SOSP 2009 论文声称首次给出了一个完整的通用操作系统内核从抽象规范一直到 C 实现的功能正确性形式化证明，前提是编译器、汇编代码和硬件正确。<sup>[7](https://dl.acm.org/doi/10.1145/1629575.1629596)</sup> 该证明是 200,000 行 Isabelle 脚本，耗费约 20 人年，其中 11 人年为 seL4 专用；验证在早期使用和移植发现的 16 个缺陷之外，又发现了 144 个缺陷。<sup>[7](https://dl.acm.org/doi/10.1145/1629575.1629596)</sup><sup> • </sup><sup>[24](https://www.trustworthy.systems/publications/nicta_full_text/1842.pdf)</sup> 在亚马逊，一位工程师学习了 TLA+，并在几周内写出了一份详细的 DynamoDB 规范；分布式 TLC 模型检验器在 10 个 EC2 实例组成的集群上运行，发现了一个在特定故障与恢复交错下可能丢失数据的缺陷。<sup>[8](https://fi.upm.es/docs/estudios/2461_aws-formal-methods.pdf)</sup> IBM 20 世纪 80 年代与牛津大学使用 Z 进行的 CICS 工作，估计使总开发成本降低了 9%，并获得了女王技术成就奖。<sup>[6](https://dl.acm.org/doi/10.1145/242223.242257)</sup> TLA+ 在工业界被 [Amazon Web Services](https://www.edgepedia.org/zh/amazon-web-services)、微软的 Cosmos DB 和 MongoDB 使用，而[通用准则](https://www.edgepedia.org/zh/common-criteria)最高保证级别 EAL7 强制要求形式化验证，尽管即使在那里也只要求设计级验证。<sup>[25](https://link.springer.com/chapter/10.1007/978-3-032-32519-8_8)</sup><sup> • </sup><sup>[26](https://www.trustworthy.systems/publications/nicta_full_text/5396.pdf)</sup>

自 2023 年以来，LLM 辅助的规范编写已成为一个活跃领域。nl2spec 利用大语言模型以交互方式将非结构化自然语言翻译为时态逻辑。<sup>[27](https://doi.org/10.48550/arxiv.2303.04864)</sup> Specula 是一个一键式智能体系统，为系统代码生成 TLA+ 模型和不变式，用 TLC 检查它们，并通过迹验证来验证模型与代码的一致性，在验证失败时修复模型。<sup>[28](https://arxiv.org/html/2607.25333v1)</sup> 实测质量仍然很低：对八个家族 30 个 LLM 在 205 个 TLA+ 规范上的评估发现，语法正确率最高为 26.6%，但语义正确率仅为 8.6%，且模型规模并不能预测质量。<sup>[9](https://arxiv.org/abs/2606.05792)</sup>

## 局限与替代方案

形式化规范不能取代测试，因为抽象机器和目标机器可能行为不同；二者通过用规范生成测试用例并检查结果来结合。<sup>[29](https://www.kneuper.de/English/Publications/limits-formal-methods.pdf)</sup> 即使在认为存在正确性证明的情况下，动态测试仍然重要，而规范能够为观察到的行为提供可证明正确的测试预言机。<sup>[30](https://staffwww.dcs.shef.ac.uk/people/A.Simons/research/papers/landscapes.pdf)</sup> 一般而言，程序是否满足规范是不可判定的；Hoare 逻辑验证需要识别循环不变式，而这无法自动完成，<sup>[29](https://www.kneuper.de/English/Publications/limits-formal-methods.pdf)</sup> 自动测试生成则面临所选路径是否可执行的不可判定可行性问题。<sup>[30](https://staffwww.dcs.shef.ac.uk/people/A.Simons/research/papers/landscapes.pdf)</sup> 规范本身也可能出错：形式化方法提高了正确性的可能性，但并不提供绝对保证，因为规范可能不正确、环境描述可能不完整，或者方法可能被误用。<sup>[29](https://www.kneuper.de/English/Publications/limits-formal-methods.pdf)</sup> 采用障碍包括其他方法也在提升质量、上市时间压力、对用户界面适配不佳以及可扩展性有限，成功的项目大多是小型的关键内核系统。<sup>[4](https://ifs.host.cs.st-andrews.ac.uk/Books/SE9/WebChapters/PDF/Ch_27_Formal_spec.pdf)</sup> 维护成本高昂：对 seL4 代码库不到 5% 的改动，曾迫使证明返工量相当于整个原始证明工作量的 17%。<sup>[26](https://www.trustworthy.systems/publications/nicta_full_text/5396.pdf)</sup> 模型检验虽然完全自动并能产生反例用于调试，但受制于状态爆炸问题；基于 BDD 的符号模型检验可应对这一问题，检验器通常能处理 100 到 200 个状态变量，已检查过具有 \( 10^{120} \) 个可达状态的系统。<sup>[6](https://dl.acm.org/doi/10.1145/242223.242257)</sup>

## References

1. [Formal Specification: a Roadmap (Axel van Lamsweerde)](https://webperso.info.ucl.ac.be/~avl/files/FormalSpec-avl.pdf)
2. [A Half Century of Formal Methods (Bjørner & Havelund, 2022)](https://havelund.com/Publications/fm-50-2022.pdf)
3. [Specifying (concurrent program modules with temporal assertions)](https://lamport.org/pubs/spec.pdf)
4. [Chapter 27: Formal Specification (Sommerville, Software Engineering, 9th ed.)](https://ifs.host.cs.st-andrews.ac.uk/Books/SE9/WebChapters/PDF/Ch_27_Formal_spec.pdf)
5. [Modeling and Developing Systems Using TLA+ (Stephan Merz, course handout)](https://members.loria.fr/Stephan.Merz/talks/argentina2005/handout.pdf)
6. [Formal Methods: State of the Art and Future Directions (Clarke & Wing, ACM Computing Surveys 28(4), 1996)](https://dl.acm.org/doi/10.1145/242223.242257)
7. [seL4: Formal Verification of an OS Kernel (SOSP 2009, ACM DL page)](https://dl.acm.org/doi/10.1145/1629575.1629596)
8. [How AWS uses formal methods (CACM 2015)](https://fi.upm.es/docs/estudios/2461_aws-formal-methods.pdf)
9. [Can LLMs Write Correct TLA+ Specifications? Evaluating Natural-Language-to-TLA+ Generation](https://arxiv.org/abs/2606.05792)
10. [Specifying and Verifying Systems With TLA (Lamport, Microsoft Research)](https://lamport.azurewebsites.net/pubs/spec-and-verifying.pdf)
11. [Defining liveness (Information Processing Letters, 1985)](https://doi.org/10.1016/0020-0190%2885%2990056-0)
12. [Book chapter on TLA+ (comparison with Z, TLC, validation workflow)](https://lamport.azurewebsites.net/pubs/spec-book-chap.pdf)
13. [PRG-101: From Z to C, Illustration of a Rigorous Development Method (Oxford PRG)](https://www.cs.ox.ac.uk/files/3414/PRG101.pdf)
14. [The Transition from VDL to VDM (C. B. Jones)](https://www.jucs.org/jucs_7_8/the_transition_from_VDL/Jones_C_B.pdf)
15. [The role of formalism in system requirements (full version)](https://ar5iv.labs.arxiv.org/html/1911.02564)
16. [The Z Notation: A Reference Manual, 2nd edition (J. M. Spivey, 1992)](https://courses.cs.washington.edu/courses/cse503/00sp/zrm.pdf)
17. [Abrial's original Z specification language paper](https://se.inf.ethz.ch/~meyer/publications/languages/Z_original.pdf)
18. [How to Select the Suitable Formal Method for an Industrial Application: A Survey (Kossak & Mashkoor, ABZ 2016, LNCS 9675)](https://link.springer.com/chapter/10.1007/978-3-319-33600-8_13)
19. [Alloy: A Lightweight Object Modelling Notation (Daniel Jackson)](https://people.csail.mit.edu/dnj/publications/alloy-journal.pdf)
20. [Jean-Raymond Abrial (2010). Modeling in Event-B. Cambridge University Press eBooks.](https://doi.org/10.1017/cbo9781139195881)
21. [E. M. Clarke, E. A. Emerson, A. P. Sistla (1986). Automatic verification of finite-state concurrent systems using temporal logic specifications. ACM Transactions on Programming Languages and Systems.](https://doi.org/10.1145/5397.5399)
22. [G.J. Holzmann (1997). The model checker SPIN. IEEE Transactions on Software Engineering.](https://doi.org/10.1109/32.588521)
23. [John V. Guttag and colleagues (1993). Larch: Languages and Tools for Formal Specification. .](https://doi.org/10.1007/978-1-4612-2704-5)
24. [Mind the Gap: A Verification Framework for Low-Level C](https://www.trustworthy.systems/publications/nicta_full_text/1842.pdf)
25. [The TLA+ Model Checker Apalache](https://link.springer.com/chapter/10.1007/978-3-032-32519-8_8)
26. [Large-Scale Formal Verification in Practice: A Process Perspective](https://www.trustworthy.systems/publications/nicta_full_text/5396.pdf)
27. [Cosler, Matthias and colleagues (2023). nl2spec: Interactively Translating Unstructured Natural Language to Temporal Logics with Large Language Models. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2303.04864)
28. [Specula: Scaling formal specifications for autonomous model checking of system code](https://arxiv.org/html/2607.25333v1)
29. [Limits of Formal Methods (Kneuper)](https://www.kneuper.de/English/Publications/limits-formal-methods.pdf)
30. [Using Formal Specifications to Support Testing](https://staffwww.dcs.shef.ac.uk/people/A.Simons/research/papers/landscapes.pdf)

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

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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/formal-specification

English original: https://www.edgepedia.org/formal-specification
