hoare-calculus

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

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)

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

Partial Correctness

Definition

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

Link to original

Total Correctness

Definition

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

Link to original

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

is a valid statement.