Definition
Correct 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 .
Idea
The correctness statement can be read as:
- the precondition describes the allowed inputs,
- the program transforms those inputs,
- the postcondition describes the required result.
For every state , if is true, then the resulting state should also make true.
Correctness
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:
Methods
Hoare Calculus
Definition
Link to originalHoare 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.
Weakest Precondition
Definition
Link to originalWeakest Precondition
The weakest precondition of a program and a postcondition is the most general (least restrictive) formula such that the Hoare triple is totally correct.
Equivalently, characterises exactly the states from which terminates in a state satisfying .
If a precondition implies , then is totally correct.
Annotation Calculus
Definition
Link to originalAnnotation 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 .
Strongest Postcondition
Definition
Link to originalStrongest Postcondition
The strongest postcondition of a program and a precondition is the most specific (most restrictive) formula such that the Hoare triple is partially correct.
Equivalently, it is the set of states reached by executing from states satisfying :
If a postcondition is implied by , then is partially correct.
Examples
This specification is totally correct and therefore also partially correct. The assignment always terminates, and after execution the value of is , which satisfies the postcondition.
This specification is neither totally correct nor partially correct. The program terminates, but the final value of is always , so the postcondition is never satisfied. A counterexample is any state, since the postcondition fails after execution.
This specification is neither totally correct nor partially correct under the given precondition. If the initial state has , the loop condition is false and the program terminates immediately with , so the postcondition fails. Hence even partial correctness does not hold.
This specification is partially correct but not totally correct. Whenever the loop terminates, it does so with , so the postcondition is satisfied. However, for some initial states, such as , the loop decreases forever and never reaches , so termination is not guaranteed.
This specification is totally correct and therefore also partially correct. The precondition ensures that the loop starts from a value greater than , so repeated decrements must eventually reach . The program then terminates with , which satisfies the postcondition.
Example
The postcondition is the expected product computation: the loop adds to exactly once for each decrement of .
This is partially correct because whenever the loop terminates, the value of is the product .
However, it is not automatically totally correct, because if initially, then moves away from and the loop never terminates.
To obtain total correctness, the precondition must be strengthened with .
Example
This specification is partially correct: whenever the program terminates, the final value is , which satisfies the postcondition.
However, it is not known whether the program always terminates for every input with . Therefore, total correctness is an open question here, as expressed by the Collatz conjecture.
Example
The program searches for two prime numbers whose sum is . It is partially correct because, if it terminates, the postcondition states exactly what the program has found.
It is also totally correct for the stated precondition, because the loop can only iterate finitely many times: starts at and increases by until it reaches .
The deeper mathematical question is whether the postcondition is always reachable, which is the content of the Goldbach conjecture.
This specification is partially correct for every program , because if the program terminates, the postcondition is always true.
It is totally correct exactly when terminates for every input state.
This specification is partially correct only if never terminates on any input state. If the program ever terminates, then the postcondition fails.
It is never totally correct, because total correctness would require termination and satisfaction of at the same time.
This specification is partially correct and totally correct for every program .
The precondition is false, so there are no allowed input states. The specification is therefore vacuously true.
This specification is also partially correct and totally correct for every program .
Again, the precondition is false, so the specification is vacuously true.