hoare-calculus

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

Hoare 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.
Link to original

Inference Rules

Empty Program

Definition

Empty Program (Hoare Calculus)

The empty program skip does nothing. It leaves the program state unchanged.

In Hoare notation, this means:

Link to original

Sequential Composition

Definition

Sequential Composition (Hoare Calculus)

The composition rule allows for reasoning about the execution of two programs in sequence (). It is defined as:

Link to original

Implication

Definition

Implication (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 .

Link to original

Standard Backward Assignment

Definition

Standard 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 .

Link to original

Forward Assignment

Definition

Floyd'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.

Link to original

Simplified Forward Assignment

Definition

Simplified Forward Assignment

Under specific side conditions, the assignment rule can be simplified to:

Side Conditions:

  1. does not occur in .
  2. does not occur in .
Link to original

Conditional

Definition

Conditional Rule

The conditional rule allows for reasoning about branching statements (if-then-else). It is defined as:

where is a boolean expression.

Link to original

Correctness

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 .

Link to original

Partial Correctness

Definition

Partially 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:

Link to original

Total Correctness

Definition

Totally 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:

Link to original

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:

Thus, we still have to show that , i.e. show that

is a valid statement.