🤖 AI Summary
Formal verification of sequential algorithms faces two key bottlenecks: (1) specification languages struggle to naturally express stateful programs with complex, flexible control flow; and (2) Hoare-logic-based verification fails to reflect the intuitive, hierarchical structure of human-written proofs. To address these challenges, we propose a novel Coq-based formal framework. Our approach introduces the *state-relation monad*, a new semantic modeling mechanism that enables concise, compositional representation of both program state and control flow. We further design a two-phase proof methodology that cleanly separates high-level logical reasoning from low-level mechanical verification, thereby faithfully mirroring natural proof structure. We have fully verified the KMP string-matching algorithm and formally developed a generic depth-first search (DFS) algorithm within this framework. Experimental evaluation demonstrates significant improvements in specification readability, verification modularity, and proof maintainability.
📝 Abstract
Current approaches for formal verification of algorithms face important limitations. For specification, they cannot express algorithms naturally and concisely, especially for algorithms with states and flexible control flow. For verification, formal proof based on Hoare logic cannot reflect the logical structure of natural proof. To address these challenges, we introduce a formal framework for naturally specifying and verifying sequential algorithms in Coq. We use the state relation monad to integrate Coq's expressive type system with the flexible control flow of imperative languages. It supports nondeterministic operations and customizable program states, enabling specifying algorithms at an appropriate level of abstraction. For verification, we build a Hoare logic for the monad and propose a novel two-stage proof approach that separates natural logical reasoning from mechanical composition. It reflects the logical structure of natural proof, enhancing modularity and readability. We evaluate the framework by formalizing the Depth-First Search (DFS) algorithm and verifying the Knuth-Morris-Pratt (KMP) algorithm.