Definition
Hoare Calculus
The Hoare calculus is a calculus for reasoning about the correctness of programs. It uses Hoare triples together with axioms and inference rules to show that a program transforms states satisfying a precondition into states satisfying a postcondition.
Properties
Soundness
The Hoare calculus is sound. Every statement about the correctness of a program proven using the Hoare calculus is true.
Completeness
The Hoare calculus is complete. Every statement about the correctness of a program is provable using the Hoare calculus, if the validity of occurring formulas can be proven.
Triple
Definition
Link to originalHoare Triple
A Hoare triple is a construct used in Hoare calculus to describe how the execution of a piece of code changes the state of the computation. It is written as:
where:
- is the precondition: an assertion that must hold before the execution of .
- is the command or program fragment.
- is the postcondition: an assertion that must hold after terminates.
Inference Rules
Empty Program
Definition
Link to originalEmpty Program (Hoare Calculus)
The empty program
skipdoes nothing. It leaves the program state unchanged.In Hoare notation, this means:
Sequential Composition
Definition
Link to originalSequential Composition (Hoare Calculus)
The composition rule allows for reasoning about the execution of two programs in sequence (). It is defined as:
Implication
Definition
Link to originalImplication (Rule of Consequence)
The rule of consequence allows the precondition to be strengthened and the postcondition to be weakened.
Formally,
Here, means that is stronger than , and means that is weaker than .
Standard Backward Assignment
Definition
Link to originalStandard Backward Assignment
The assignment rule describes how an assignment affects the truth of an assertion . It is defined as:
where denotes the substitution of all free occurrences of variable in with the expression .
Forward Assignment
Definition
Link to originalFloyd's Rule ( Hoare Calculus)
Floyd’s rule or forward assignment provides a way to reason about assignment in a forward direction, meaning it calculates the strongest postcondition from a given precondition . It is defined as:
where is a fresh variable representing the value of before the assignment.
Simplified Forward Assignment
Definition
Link to originalSimplified Forward Assignment
Under specific side conditions, the assignment rule can be simplified to:
Side Conditions:
- does not occur in .
- does not occur in .
Conditional
Definition
Link to originalConditional Rule
The conditional rule allows for reasoning about branching statements (if-then-else). It is defined as:
where is a boolean expression.
Correctness
Definition
Link to originalCorrect Program
A correct program is a program that satisfies its specification.
More precisely, for a precondition and a postcondition , the program is correct if every input state that satisfies leads, after execution, to a state that satisfies .
Partial Correctness
Definition
Link to originalPartially Correct Program
A program is partially correct with respect to a precondition and a postcondition if, for every execution that starts in a state satisfying and terminates, the resulting state satisfies .
In Hoare notation, this is written as:
Total Correctness
Definition
Link to originalTotally Correct Program
A program is totally correct with respect to a precondition and a postcondition if it is partially correct and guaranteed to terminate for all inputs satisfying .
In Hoare notation, this is sometimes denoted using square brackets:
Axioms
An axiom is a Hoare triple that is valid without premises.
Specific empty-program axiom
is a correct axiom, but very specific.
General empty-program axiom
, for arbitrary , is a correct axiom and much more general.
Incorrect overgeneralisation
, for arbitrary and , is not correct. For example, is false when .
Abort axiom
is a correct axiom for partial correctness. The program never reaches a final state, so the postcondition is never violated.
A special case is
which is also correct by the rule of consequence.
Examples
Swapping values
We have to find a , such that:
are tautologies.
Two obvious choices for and are:
program backwards.
Start with the final postcondition . For the assignment , the standard backward choice is the weakest precondition . So we choose:
Then (iii) becomes immediate.
Apply the same idea once more to . Its backward choice is:
Then (ii) becomes immediate.
So and are chosen to make the last two proof obligations hold automatically. Only (i) remains to be checked.
Thus, we still have to show that , i.e. show that