Hoare logic (also known as Floyd–Hoare logic or Hoare rules) is a
formal system
A formal system is an abstract structure and formalization of an axiomatic system used for deducing, using rules of inference, theorems from axioms.
In 1921, David Hilbert proposed to use formal systems as the foundation of knowledge in ma ...
with a set of logical rules for reasoning rigorously about the
correctness of computer programs
In theoretical computer science, an algorithm is correct with respect to a specification if it behaves as specified. Best explored is ''functional'' correctness, which refers to the input–output behavior of the algorithm: for each input it prod ...
. It was proposed in 1969 by the British computer scientist and
logician Tony Hoare, and subsequently refined by Hoare and other researchers. The original ideas were seeded by the work of
Robert W. Floyd, who had published a similar system for
flowcharts.
Hoare triple
The central feature of Hoare logic is the Hoare triple. A triple describes how the execution of a piece of code changes the state of the computation. A Hoare triple is of the form
:
where
and
are ''
assertions'' and
is a ''command''.
[Hoare originally wrote "" rather than "".] is named the ''
precondition'' and
the ''
postcondition'': when the precondition is met, executing the command establishes the postcondition. Assertions are formulae in
predicate logic
First-order logic, also called predicate logic, predicate calculus, or quantificational logic, is a collection of formal systems used in mathematics, philosophy, linguistics, and computer science. First-order logic uses quantified variables ove ...
.
Hoare logic provides
axiom
An axiom, postulate, or assumption is a statement that is taken to be true, to serve as a premise or starting point for further reasoning and arguments. The word comes from the Ancient Greek word (), meaning 'that which is thought worthy or ...
s and
inference rules for all the constructs of a simple
imperative programming language. In addition to the rules for the simple language in Hoare's original paper, rules for other language constructs have been developed since then by Hoare and many other researchers. There are rules for
concurrency,
procedures,
jumps, and
pointers.
Partial and total correctness
Using standard Hoare logic, only
partial correctness can be proven.
Total correctness additionally requires
termination, which can be proven separately or with an extended version of the While rule. Thus the intuitive reading of a Hoare triple is: Whenever
holds of the state before the execution of
, then
will hold afterwards, or
does not terminate. In the latter case, there is no "after", so
can be any statement at all. Indeed, one can choose
to be false to express that
does not terminate.
"Termination" here and in the rest of this article is meant in the broader sense that computation will eventually be finished, that is it implies the absence of infinite loops; it does not imply the absence of implementation limit violations (e.g. division by zero) stopping the program prematurely. In his 1969 paper, Hoare used a narrower notion of termination which also entailed the absence of implementation limit violations, and expressed his preference for the broader notion of termination as it keeps assertions implementation-independent:
Rules
Empty statement axiom schema
The
empty statement rule asserts that the statement does not change the state of the program, thus whatever holds true before also holds true afterwards.
[This article uses a natural deduction style notation for rules. For example, informally means "If both and hold, then also holds"; and are called antecedents of the rule, is called its succedent. A rule without antecedents is called an axiom, and written as .]
:
Assignment axiom schema
The assignment axiom states that, after the assignment, any predicate that was previously true for the right-hand side of the assignment now holds for the variable. Formally, let be an assertion in which the variable is
free. Then:
:
where