hoare-calculus program-proofing
Definition
Annotation Calculus
The annotation calculus is a system for verifying the correctness of programs by systematically inserting assertions (annotations) between statements. It serves as a more readable, linear alternative to the Hoare calculus tree representation, effectively “tracking” the state through the program.
An annotated program consists of a sequence of commands and assertions .
Rules
The rules of the annotation calculus correspond to the inference rules of the Hoare calculus.
Assignment
The assertion before an assignment is typically derived using the backward assignment rule:
Sequential Composition
Assertions are placed between statements to link them:
where and must be valid.
Conditional
Branching statements require annotations in both branches:
While Loop
Loops require the specification of a loop invariant :
Verification Condition Generation
To prove a Hoare triple using the annotation calculus:
- Annotate the program starting from the postcondition (working backwards) or the precondition (working forwards).
- Ensure that every adjacent pair is a valid Hoare triple.
- For every point where two assertions meet (e.g., at the start of the program where the user-defined meets the calculated ), prove the implication .
Examples
Example: Swapping Values
To verify the swapping of two variables and with initial values :
Relationship to WP
The process of annotating a program backwards from a postcondition is essentially a step-by-step calculation of the weakest liberal precondition.
Verification Conditions