HOME

TheInfoList



OR:

Hoare logic (also known as Floyd–Hoare logic or Hoare rules) is a
formal system A formal system is an abstract structure used for inferring theorems from axioms according to a set of rules. These rules, which are used for carrying out the inference of theorems from axioms, are the logical calculus of the formal system. A fo ...
with a set of logical rules for reasoning rigorously about the correctness of computer programs. 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 Robert W Floyd (June 8, 1936 – September 25, 2001) was a computer scientist. His contributions include the design of the Floyd–Warshall algorithm (independently of Stephen Warshall), which efficiently finds all shortest paths in a graph and ...
, who had published a similar system for
flowchart A flowchart is a type of diagram that represents a workflow or process. A flowchart can also be defined as a diagrammatic representation of an algorithm, a step-by-step approach to solving a task. The flowchart shows the steps as boxes of ...
s.


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 : \ C \ where P and Q are '' assertions'' and C is a ''command''.Hoare originally wrote "P\Q" rather than "\C\". P is named the '' precondition'' and Q the '' postcondition'': when the precondition is met, executing the command establishes the postcondition. Assertions are formulae in predicate logic. Hoare logic provides axioms 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,
jump Jumping is a form of locomotion or movement in which an organism or non-living (e.g., robotic) mechanical system propels itself through the air along a ballistic trajectory. Jump or Jumping also may refer to: Places * Jump, Kentucky or Jump S ...
s, and pointers.


Partial and total correctness

Using standard Hoare logic, only partial correctness can be proven. Total correctness additionally requires
termination Termination may refer to: Science *Termination (geomorphology), the period of time of relatively rapid change from cold, glacial conditions to warm interglacial condition *Termination factor, in genetics, part of the process of transcribing RNA ...
, which can be proven separately or with an extended version of the While rule.) Here: Sect. 3.4, p. 64. Thus the intuitive reading of a Hoare triple is: Whenever P holds of the state before the execution of C, then Q will hold afterwards, or C does not terminate. In the latter case, there is no "after", so Q can be any statement at all. Indeed, one can choose Q to be false to express that C 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, \dfrac 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 \dfrac. : \dfrac


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: : \dfrac where P /x/math> denotes the assertion in which each free occurrence of has been replaced by the expression . The assignment axiom scheme means that the truth of P /x/math> is equivalent to the after-assignment truth of . Thus were P /x/math> true prior to the assignment, by the assignment axiom, then would be true subsequent to which. Conversely, were P /x/math> false (i.e. \neg P /x/math> true) prior to the assignment statement, must then be false afterwards. Examples of valid triples include: :*\ y := x + 1 \ :*\ x := x + 1 \ All preconditions that are not modified by the expression can be carried over to the postcondition. In the first example, assigning y:=x+1 does not change the fact that x+1=43, so both statements may appear in the postcondition. Formally, this result is obtained by applying the axiom schema with being (y=43 and x+1=43), which yields P x+1)/y/math> being (x+1=43 and x+1=43), which can in turn be simplified to the given precondition x+1=43. The assignment axiom scheme is equivalent to saying that to find the precondition, first take the post-condition and replace all occurrences of the left-hand side of the assignment with the right-hand side of the assignment. Be careful not to try to do this backwards by following this ''incorrect'' way of thinking: \ x:=E \; this rule leads to nonsensical examples like: : \ x := 3 \ Another ''incorrect'' rule looking tempting at first glance is \ x:=E \; it leads to nonsensical examples like: : \ x := x + 1 \ While a given postcondition uniquely determines the precondition P /x/math>, the converse is not true. For example: :*\ x := y \cdot y \, :*\ x := y \cdot y \, :*\ x := y \cdot y \ , and :*\ x := y \cdot y \ are valid instances of the assignment axiom scheme. The assignment axiom proposed by Hoare ''does not apply'' when more than one name may refer to the same stored value. For example, : \ x := 2 \ is wrong if and refer to the same variable ( aliasing), although it is a proper instance of the assignment axiom scheme (with both \ and \ being \).


Rule of composition

Hoare's rule of composition applies to sequentially executed programs and , where executes prior to and is written S;T ( is called the ''midcondition''): :\dfrac For example, consider the following two instances of the assignment axiom: :\ y := x + 1 \ and :\ z := y \ By the sequencing rule, one concludes: :\ y := x + 1; z := y \ Another example is shown in the right box.


Conditional rule

:\dfrac The conditional rule states that a postcondition common to and part is also a postcondition of the whole statement. In the and the part, the unnegated and negated condition can be added to the precondition , respectively. The condition, , must not have side effects. An example is given in the next section. This rule was not contained in Hoare's original publication. However, since a statement :\texttt\ B\ \texttt\ S\ \texttt\ T\ \texttt has the same effect as a one-time loop construct :\texttt\ b:=\texttt; \texttt\ B\wedge b\ \texttt\ S; b:=\texttt\ \texttt; b:=\texttt; \texttt\ \neg B\wedge b\ \texttt\ T; b:=\texttt\ \texttt the conditional rule can be derived from the other Hoare rules. In a similar way, rules for other derived program constructs, like loop, loop, , , can be reduced by program transformation to the rules from Hoare's original paper.


Consequence rule

:\dfrac This rule allows to strengthen the precondition P_2 and/or to weaken the postcondition Q_2. It is used e.g. to achieve literally identical postconditions for the and the part. For example, a proof of :\\texttt\ x<15\ \texttt\ x:=x+1\ \texttt\ x:=0\ \texttt \ needs to apply the conditional rule, which in turn requires to prove :\ x:=x+1 \,   or simplified :\ x:=x+1 \ for the part, and :\ x:=0 \,   or simplified :\ x:=0 \ for the part. However, the assignment rule for the part requires to choose as 0\leq x \leq 15; rule application hence yields :\ x:=x+1 \,   which is logically equivalent to :\ x:=x+1 \. The consequence rule is needed to strengthen the precondition \ obtained from the assignment rule to \ required for the conditional rule. Similarly, for the part, the assignment rule yields :\ x:=0 \,   or equivalently :\ x:=0 \, hence the consequence rule has to be applied with P_1 and P_2 being \ and \, respectively, to strengthen again the precondition. Informally, the effect of the consequence rule is to "forget" that \ is known at the entry of the part, since the assignment rule used for the part doesn't need that information.


While rule

:\dfrac Here is the loop invariant, which is to be preserved by the loop body . After the loop is finished, this invariant still holds, and moreover \neg B must have caused the loop to end. As in the conditional rule, must not have side effects. For example, a proof of :\ \texttt\ x<10\ \texttt\ x:=x+1\ \texttt \ by the while rule requires to prove :\ x := x + 1 \,   or simplified :\ x := x + 1 \, which is easily obtained by the assignment rule. Finally, the postcondition \ can be simplified to \. For another example, the while rule can be used to formally verify the following strange program to compute the exact square root of an arbitrary number —even if is an integer variable and is not a square number: :\ \texttt\ x\cdot x \neq a\ \texttt\ \texttt\ \texttt \ After applying the while rule with being , it remains to prove :\ \texttt \, which follows from the skip rule and the consequence rule. In fact, the strange program is ''partially'' correct: if it happened to terminate, it is certain that must have contained (by chance) the value of 's square root. In all other cases, it will not terminate; therefore it is not ''totally'' correct.


While rule for total correctness

If the above ordinary while rule is replaced by the following one, the Hoare calculus can also be used to prove total correctness, i.e. termination as well as partial correctness. Commonly, square brackets are used here instead of curly braces to indicate the different notion of program correctness. :\dfrac In this rule, in addition to maintaining the loop invariant, one also proves
termination Termination may refer to: Science *Termination (geomorphology), the period of time of relatively rapid change from cold, glacial conditions to warm interglacial condition *Termination factor, in genetics, part of the process of transcribing RNA ...
by way of an expression , called the
loop variant In computer science, a loop variant is a mathematical function defined on the state space of a computer program whose value is monotonically decreased with respect to a (strict) well-founded relation by the iteration of a while loop under some inv ...
, whose value strictly decreases with respect to a
well-founded relation In mathematics, a binary relation ''R'' is called well-founded (or wellfounded) on a class ''X'' if every non-empty subset ''S'' ⊆ ''X'' has a minimal element with respect to ''R'', that is, an element ''m'' not related by ''s& ...
on some domain set during each iteration. Since is well-founded, a strictly decreasing chain of members of can have only finite length, so cannot keep decreasing forever. (For example, the usual order is well-founded on positive
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the languag ...
s \mathbb, but neither on the integers \mathbb nor on
positive real numbers In mathematics, the set of positive real numbers, \R_ = \left\, is the subset of those real numbers that are greater than zero. The non-negative real numbers, \R_ = \left\, also include zero. Although the symbols \R_ and \R^ are ambiguously used f ...
\mathbb^+; all these sets are meant in the mathematical, not in the computing sense, they are all infinite in particular.) Given the loop invariant , the condition must imply that is not a minimal element of , for otherwise the body could not decrease any further, i.e. the premise of the rule would be false. (This is one of various notations for total correctness.) Resuming the first example of the previous section, for a total-correctness proof of : \leq 10texttt\ x < 10\ \texttt\ x:=x+1\ \texttt neg x < 10 \wedge x \leq 10/math> the while rule for total correctness can be applied with e.g. being the non-negative integers with the usual order, and the expression being 10 - x, which then in turn requires to prove : \leq 10 \wedge x < 10 \wedge 10-x \geq 0 \wedge 10-x = zx:= x+1 \leq 10 \wedge 10-x \geq 0 \wedge 10-x < z/math> Informally speaking, we have to prove that the distance 10-x decreases in every loop cycle, while it always remains non-negative; this process can go on only for a finite number of cycles. The previous proof goal can be simplified to : < 10 \wedge 10-x = z x:=x+1 \leq 10 \wedge 10-x < z/math>, which can be proven as follows: : +1 \leq 10 \wedge 10-x-1 < z x:=x+1 \leq 10 \wedge 10-x < z/math> is obtained by the assignment rule, and : +1 \leq 10 \wedge 10-x-1 < z/math> can be strengthened to < 10 \wedge 10-x = z/math> by the consequence rule. For the second example of the previous section, of course no expression can be found that is decreased by the empty loop body, hence termination cannot be proved.


See also


Notes


References


Further reading

* Robert D. Tennent.
Specifying Software
' (a textbook that includes an introduction to Hoare logic, written in 2002)


External links


KeY-Hoare
is a semi-automatic verification system built on top of the KeY theorem prover. It features a Hoare calculus for a simple while language.
j-Algo-modul Hoare calculus
— A visualisation of the Hoare calculus in the algorithm visualisation program j-Algo {{Authority control 1969 in computing Program logic Static program analysis