Definition
Hoare Calculus
Hoare calculus is a formal system used to reason about the correctness of programs. It uses a set of axioms and inference rules to prove that if a program starts in a state satisfying a specific condition (precondition), it will end in a state satisfying another condition (postcondition).
Properties
Soundness
Every statement about the correctness of a program proven using the Hoare calculus is true.
Completeness
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)
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 for the strengthening of the precondition and the weakening of the postcondition. It is formally expressed as:
where:
- means that is stronger than (it restricts the state space more).
- means that is weaker than (it is a more general requirement).
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
Partial Correctness
Definition
Link to originalPartial Correctness
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 originalTotal Correctness
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:
Examples
Example: Swapping Values
We have to find a , such that:
are tautologies.
Two obvious choice for and are:
Thus, we still have to show that , i.e.: show that