一句话:结合关系 Hoare 四元组与抽象程序断言标注,将算法层不变式转移到实现证明,简化验证。
验证复杂算法的真实代码功能正确性通常分为两层:具体代码精化抽象算法描述,以及证明抽象描述正确。但两层在实践中并不清晰分离:例如 KMP 验证中,实现正确性证明常需重新建立算法层已证性质,因为传统两层方法无法转移具体实现所依赖的不变式。本文提出两个简单想法实现干净分离:用关系 Hoare 四元组表达实现正确性,并在抽象程序中加入断言标注以捕获关键不变式。这样算法证明中建立的性质可直接转移到实现证明,无需重证。作者通过 KMP 模式匹配和深度优先搜索等非平凡案例展示,该方法使证明更简单、验证过程更模块化。
Verifying the functional correctness of real-world code with complex algorithms can be decomposed into two layers: verifying that the concrete code refines an abstract algorithmic description, and proving the correctness of the formal description. However, in practice the two layers do not stay cleanly separated. For example, in the verification of the Knuth-Morris-Pratt (KMP) algorithm, the implementation correctness proof often re-establishes algorithm properties that have already been proved, as the concrete implementation relies on invariants that the traditional two-layer method provides no mechanism to transfer. This makes it difficult to clearly separate the concerns of algorithm correctness and implementation correctness. In this pearl, we show how a clean separation can be achieved within the two-layer method by combining two simple ideas: expressing implementation correctness as a relational Hoare quadruple, and introducing assertion annotations into the abstract program to capture key invariants. Properties established in the algorithm proof are thereby transferred directly to the implementation proof, eliminating the need to re-prove them. We demonstrate the effectiveness of this approach through non-trivial case studies, including the Knuth-Morris-Pratt pattern-matching algorithm and the depth-first search algorithm, showing that it leads to simpler proofs and a more modular verification process.
@article{WuYWC26,
title = {Assertions for Free: Transferring Invariants from Algorithm to Implementation Proofs (Functional Pearl)},
author = {Shushu Wu and Chengxi Yang and Xiwei Wu and Qinxiang Cao},
journal = {Proceedings of the ACM on Programming Languages},
volume = {10},
number = {ICFP},
year = {2026},
doi = {10.1145/3828694}
}