# B方法

B方法是一种通过数学建模来规约、设计和编码软件系统的形式化方法，<sup>[1](https://www.cambridge.org/core/books/bbook/D0FA939B9F72E49DBA14BA0F68491118)</sup> 在这种方法中，一次开发会产生若干证明义务，用以保证其正确性。<sup>[2](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)</sup> 其最终产物是一组经过证明的模型，其中最低层的精化以可实现的B0子集书写，并翻译为C、C++或Ada。<sup>[3](https://b-method.gitbook.io/training-resources-for-atelier-b/guides-and-tutorials/applying-the-b-method)</sup> B主要用于以证明取代测试的安全关键软件：巴黎地铁14号线和鲁瓦西机场摆渡车均以它为基础，而Atelier B目前为全球超过30%的基于CBTC的自动地铁系统所使用。<sup>[4](https://krin.gs/publication/butler-history-of-b-fmics20/butler-history-of-b-fmics20.pdf)</sup>

| 关键事实 | 内容 |
|---|---|
| 数学基础 | 带选择公理与广义替换的策梅洛-弗兰克尔集合论<sup>[2](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)</sup> |
| 最终产物 | 经过证明的B模型；B0实现翻译为C、C++或Ada<sup>[3](https://b-method.gitbook.io/training-resources-for-atelier-b/guides-and-tutorials/applying-the-b-method)</sup> |
| 正确性标准 | 只有当所有证明义务均已证明时，B项目才是正确的<sup>[3](https://b-method.gitbook.io/training-resources-for-atelier-b/guides-and-tutorials/applying-the-b-method)</sup> |
| 旗舰部署 | 巴黎地铁14号线：115,000行B代码，27,800个证明，8.1%为交互式，自1998年10月投入运营<sup>[5](https://www.jucs.org/jucs_13_5/formal_methods_theory_becoming/jucs_13_5_0619_0628_abrial.pdf)</sup> |
| 主要变体 | 经典B（精化至B0、代码生成）和Event-B（卫式事件、系统建模）<sup>[6](https://stups.hhu-hosting.de/downloads/pdf/history-of-b.pdf)</sup> |
| 主要工具 | Atelier B（经典B）、Rodin（Event-B）、ProB（动画、约束求解、模型检验）<sup>[4](https://krin.gs/publication/butler-history-of-b-fmics20/butler-history-of-b-fmics20.pdf)</sup> |
| 已知注意事项 | 生成的代码未经形式化证明；通过双代码生成器加运行时表决器来缓解<sup>[3](https://b-method.gitbook.io/training-resources-for-atelier-b/guides-and-tutorials/applying-the-b-method)</sup> |

## 工作原理

B以带选择公理的策梅洛-弗兰克尔集合论、一阶谓词演算和广义替换语言（GSL）为基础，后者的简单形式 `x := E(x)` 类似于赋值。<sup>[2](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)</sup> GSL扩展了迪杰斯特拉的卫式命令语言，并具有最弱前置条件语义：构造 [S]P 对应于迪杰斯特拉的 wp(S, P)。<sup>[7](https://eprints.soton.ac.uk/250549/1/bbook.html)</sup>

系统被建模为一组相互依赖的抽象机，用抽象机记法书写，每个抽象机包含一个带不变式的状态以及对该状态的若干操作。<sup>[8](https://edwardcrichton.github.io/BToolkit/BHELP/BMethod.html)</sup> 不变式表达安全性或完整性条件：初始状态必须满足它，且每个操作都必须保持它。前置条件是对调用者的义务，而不是运行时测试；没有非平凡前置条件的操作是全操作，或称为鲁棒操作。<sup>[9](https://cgi.cse.unsw.edu.au/~cs2111/ClassicalB/PDF/intro-b-method-article.pdf)</sup> 在语义上，一个抽象机是带不变式的标记迁移系统，其可靠性对应于对可达状态集合的归纳。<sup>[10](https://inria.hal.science/hal-01305026/file/paper.pdf)</sup>

精化会产生该方法特有的证明义务。抽象状态变量与具体状态变量由粘合不变式 J(x, y) 联结；每个抽象事件都必须被正确精化（即一种模拟），每个新事件都必须精化 skip，任何新事件都不得永远夺取控制权，并且必须保持相对无死锁性。<sup>[2](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)</sup> 精化保持已经证明的性质，包括安全性质和终止性。<sup>[2](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)</sup>

## 实施方法

一次开发分三个阶段：项目管理与建模、模型证明、代码生成。<sup>[3](https://b-method.gitbook.io/training-resources-for-atelier-b/guides-and-tutorials/applying-the-b-method)</sup> 实践者先写一个抽象机，然后使用结构化子句（includes、imports、promotes、extends 用于结构化；sees、definitions 用于共享；constants、properties、values 用于配置）进行逐层精化，以处理工业规模的软件。<sup>[4](https://krin.gs/publication/butler-history-of-b-fmics20/butler-history-of-b-fmics20.pdf)</sup> 工具会自动生成证明义务；开发者先用自动证明器来消解这些义务，剩余的用交互式证明器完成。<sup>[10](https://inria.hal.science/hal-01305026/file/paper.pdf)</sup>

最后一个精化层B0是替换语言的一个子集，限制为可实现的构造，工具由此翻译为C、C++或Ada。<sup>[2](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)</sup> 在当前Atelier B版本中，Ada代码生成是专业版的独家功能，而社区版提供C代码生成，并且自24.04版起提供一个实验性的Rust翻译器。<sup>[3](https://b-method.gitbook.io/training-resources-for-atelier-b/guides-and-tutorials/applying-the-b-method)</sup> 由于代码生成器本身并非形式化开发，生成的源代码也未经形式化证明；文献记载的缓解措施是软件多样性——在二取二架构中运行两个不同的代码生成器并配一个运行时表决器。<sup>[3](https://b-method.gitbook.io/training-resources-for-atelier-b/guides-and-tutorials/applying-the-b-method)</sup> CLEARSY安全平台将整个链条自动化：证明一个B模型，再通过Atelier B的C生成器和gcc将其编译为用于专用硬件的二进制文件。<sup>[11](https://arxiv.org/pdf/2005.10662v1.pdf)</sup>

## 起源

B方法归功于让-雷蒙·阿布里亚尔（Jean-Raymond Abrial），其著作《Event-B建模：系统与软件工程》于2010年问世。<sup>[12](https://www.atelierb.eu/wp-content/uploads/2023/07/B_EventB_JRA.pdf)</sup> 该方法主要由阿布里亚尔发展，并受他早期关于Z的工作影响；与Z不同，它覆盖从规约到编码的整个开发周期。<sup>[7](https://eprints.soton.ac.uk/250549/1/bbook.html)</sup>

B方法的工业应用历程与巴黎紧密相连：1988年投入运行的RER A线上的SACEM列车防护系统，使用的东西可以被视为B方法的草图；同年，“The B Tool”以未命名的语法亮相。<sup>[4](https://krin.gs/publication/butler-history-of-b-fmics20/butler-history-of-b-fmics20.pdf)</sup> 1989年，阿尔斯通（Alstom）、RATP和SNCF启动了一个部分由政府资助的项目，将该方法产业化；阿尔斯通于1993年在内部交付了B-Toolset，CLEARSY于2001年汇集了知识产权。<sup>[4](https://krin.gs/publication/butler-history-of-b-fmics20/butler-history-of-b-fmics20.pdf)</sup> 关于B的书和Event-B的书相继出版。<sup>[12](https://www.atelierb.eu/wp-content/uploads/2023/07/B_EventB_JRA.pdf)</sup>

## 变体

**经典B**面向软件：规约被精化至B0并生成代码。<sup>[6](https://stups.hhu-hosting.de/downloads/pdf/history-of-b.pdf)</sup> **Event-B**面向系统级建模，以集合论为记法，以精化处理抽象层次，并以数学证明验证各层次之间的一致性。<sup>[13](https://eprints.soton.ac.uk/271058/)</sup> 两者在机理上不同：B的操作带有前置条件，前置条件只能在精化中削弱，并且每个操作生成两条证明规则（前置条件和后置条件）；而Event-B的事件带有卫式条件，卫式条件只能加强，并且只生成一条规则；Event-B还省略了条件语句、选择、顺序执行和循环等编程构造，从而简化了证明义务。<sup>[12](https://www.atelierb.eu/wp-content/uploads/2023/07/B_EventB_JRA.pdf)</sup> Event-B深受动作系统的影响。<sup>[12](https://www.atelierb.eu/wp-content/uploads/2023/07/B_EventB_JRA.pdf)</sup>

工具按变体分流：B用Atelier B（ClearSy），Event-B用Rodin，二者各有一个证明义务生成器和若干证明器；Atelier B的证明器也在Rodin内部得到了成功使用。<sup>[12](https://www.atelierb.eu/wp-content/uploads/2023/07/B_EventB_JRA.pdf)</sup> Rodin集建模与证明于一体，但自身不包含证明器；它维护相继式演算证明树，并以插件形式接受推理器和策略，包括Atelier B证明器和SMT求解器。<sup>[13](https://eprints.soton.ac.uk/271058/)</sup><sup> • </sup><sup>[4](https://krin.gs/publication/butler-history-of-b-fmics20/butler-history-of-b-fmics20.pdf)</sup> ProB是一个动画器、约束求解器和模型检验器。<sup>[4](https://krin.gs/publication/butler-history-of-b-fmics20/butler-history-of-b-fmics20.pdf)</sup>

## 应用

巴黎地铁14号线的安全关键软件包含115,000行B代码，生成86,000行Ada代码，有27,800个证明，其中8.1%为交互式，耗时7.1人月；该系统自1998年10月起运行。<sup>[5](https://www.jucs.org/jucs_13_5/formal_methods_theory_becoming/jucs_13_5_0619_0628_abrial.pdf)</sup> 鲁瓦西摆渡车使用了183,000行B代码、158,000行Ada代码、43,610个证明，3.3%为交互式，耗时4.6人月。<sup>[5](https://www.jucs.org/jucs_13_5/formal_methods_theory_becoming/jucs_13_5_0619_0628_abrial.pdf)</sup> 在这两个项目中，B只覆盖了软件中安全关键的三分之一，且未进行单元测试，由成功的整体测试取而代之。<sup>[5](https://www.jucs.org/jucs_13_5/formal_methods_theory_becoming/jucs_13_5_0619_0628_abrial.pdf)</sup>

巴黎13号线的一个站台屏蔽门控制器使用了3,500行B代码、约1,000个证明义务，其中90%自动消解，其余在两天内用交互式证明器完成；一个四人的团队构建了它，一项为期8个月、控制约96,000列列车的实验未观察到任何故障。<sup>[14](https://www.atelierb.eu/wp-content/uploads/2023/02/Formal_Methods_in_Safety-Critical_Railway_Systems.pdf)</sup> 类似的基于B的列车控制系统已在纽约市、巴塞罗那和布拉格的地铁以及巴黎地铁1号线上开发。<sup>[5](https://www.jucs.org/jucs_13_5/formal_methods_theory_becoming/jucs_13_5_0619_0628_abrial.pdf)</sup>

## 局限与替代方案

阿布里亚尔本人将主要挑战归结为B在工业界推广不佳：它主要用于列车系统，而其他领域（能源、汽车、航空、航天）则反映既有的工程方法太难改动。<sup>[12](https://www.atelierb.eu/wp-content/uploads/2023/07/B_EventB_JRA.pdf)</sup> 一次开发会产生大量证明义务，因此控制证明的复杂性是核心的实际问题，而证明器的能力受逻辑理论上的经典结果所限。<sup>[2](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)</sup> 并发被建模为交错：在任何一步，任何已启用的迁移都可能被采用，而结巴步必须用skip替换显式写出。<sup>[15](https://csclub.uwaterloo.ca/~abandali/bandali-mmath-thesis-for-readers.pdf)</sup> 工具链本身未经机器检验；在Isabelle/HOL（包括一个经过验证的证明义务生成器）、Coq和PVS中对B的形式化，以及一个Coq证明系统，填补了这一空白。<sup>[10](https://inria.hal.science/hal-01305026/file/paper.pdf)</sup>

与Z相比，B同样基于ZF集合论，但使用的谓词变换器语义比Z的前后谓词更接近编程，并且B区分前置条件和卫式条件，而Z不区分；与VDM相比，B保留经典二值逻辑，而VDM为处理未定义性使用三值逻辑。<sup>[2](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)</sup> Event-B的精化演算接近巴克（Back）的动作系统，TLA+同样建立在集合论之上。<sup>[2](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)</sup> ABZ家族的综述指出，形式化方法在工业工程中总体上仍处于边缘地位，部分原因是缺乏选择指南。<sup>[16](https://link.springer.com/chapter/10.1007/978-3-319-33600-8_13)</sup>

B的高阶数据（函数、无界域上的嵌套关系）限制了对SMT的直接支持：集合概括式、关系复合、闭包、量化的并与交以及基数在SMT-LIB中没有原生的对应物。<sup>[17](https://link.springer.com/article/10.1007/s10009-022-00682-y)</sup> 在Rodin中，一个将Event-B证明义务翻译为SMT-LIB的插件，在所有推理器组合使用时，将未证明义务从196个（仅用Atelier B证明器）削减到31个，尽管每个SMT求解器单独使用时都不如Atelier B证明器有效。<sup>[18](https://people.montefiore.uliege.be/pfontain/Deharbe10.pdf)</sup>

## References

1. [The B-Book: Assigning Programs to Meanings (J. R. Abrial, Cambridge University Press)](https://www.cambridge.org/core/books/bbook/D0FA939B9F72E49DBA14BA0F68491118)
2. [Foundations of the B Method (Cansell & Méry survey)](http://www.imm.dtu.dk/~dibj/cai/cai-b.pdf)
3. [Applying the B Method (Atelier B training documentation)](https://b-method.gitbook.io/training-resources-for-atelier-b/guides-and-tutorials/applying-the-b-method)
4. [The First Twenty-Five Years of Industrial Use of the B-Method (Butler, Körner, Krings, Leuschel et al., FMICS 2020)](https://krin.gs/publication/butler-history-of-b-fmics20/butler-history-of-b-fmics20.pdf)
5. [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)
6. [The History and Evolution of B and Event-B (Körner, Krings, Leuschel, Butler, Lecomte, Voison)](https://stups.hhu-hosting.de/downloads/pdf/history-of-b.pdf)
7. [Review of The B-Book (Southampton eprints copy)](https://eprints.soton.ac.uk/250549/1/bbook.html)
8. [The B-Method (B-Core B-Toolkit on-line help)](https://edwardcrichton.github.io/BToolkit/BHELP/BMethod.html)
9. [Introduction to the B Method and B Toolkit (UNSW course notes)](https://cgi.cse.unsw.edu.au/~cs2111/ClassicalB/PDF/intro-b-method-article.pdf)
10. [Software component design with the B method, a formalization in Isabelle/HOL](https://inria.hal.science/hal-01305026/file/paper.pdf)
11. [The CLEARSY Safety Platform: 5 Years of Research, Development and Deployment](https://arxiv.org/pdf/2005.10662v1.pdf)
12. [On B and Event-B: Principles, Success and Challenges (J.-R. Abrial)](https://www.atelierb.eu/wp-content/uploads/2023/07/B_EventB_JRA.pdf)
13. [Rodin: an open toolset for modelling and reasoning in Event-B (Abrial, Butler, Hallerstede, Hoang, Mehta, Voisin, STTT 2010)](https://eprints.soton.ac.uk/271058/)
14. [Formal Methods in Safety-Critical Railway Systems (ClearSy industrial experience report)](https://www.atelierb.eu/wp-content/uploads/2023/02/Formal_Methods_in_Safety-Critical_Railway_Systems.pdf)
15. [A Comprehensive Study of Declarative Modelling Languages (Bandali, MMath thesis, University of Waterloo)](https://csclub.uwaterloo.ca/~abandali/bandali-mmath-thesis-for-readers.pdf)
16. [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)
17. [SMT solving for the validation of B and Event-B models (STTT)](https://link.springer.com/article/10.1007/s10009-022-00682-y)
18. [Integrating SMT solvers in Rodin (Deharbe)](https://people.montefiore.uliege.be/pfontain/Deharbe10.pdf)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026; Oct 21, 2026 · Last review: Sep 30, 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/b-method

English original: https://www.edgepedia.org/b-method
