BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help

Hoare logic

Print-Friendly
About 4 pages (1,126 words)

Bookmark and Share Know this topic well? Help others and get FREE products!

Hoare logic (also known as Floyd–Hoare logic) is a formal system developed by the British computer scientist C. A. R. Hoare, and subsequently refined by Hoare and other researchers. It was published in Hoare's 1969 paper "An axiomatic basis for computer programming". The purpose of the system is to provide a set of logical rules in order to reason about the correctness of computer programs with the rigour of mathematical logic. Hoare acknowledges earlier contributions from Robert Floyd, who had published a similar system for flowcharts. 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

<math>\{P\}\;C\;\{Q\}</math>

where P and Q are assertions and C is a command. P is called the precondition and Q the postcondition. Assertions are formulas in predicate logic. Hoare logic has 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, jumps, and pointers.

Contents

Partial and total correctness

Standard Hoare logic proves only partial correctness, while termination would have to be proved separately. 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. Note that if C does not terminate, then 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. Total correctness can also be proven with an extended version of the While rule.

Rules

Empty statement axiom schema

<math> \frac{}{\{P\}\ \textbf{skip}\ \{P\}} \!</math>

Assignment axiom schema

The assignment axiom states that after the assignment any predicate holds for the variable that was previously true for the right-hand side of the assignment:

<math> \frac{}{\{P[x/E]\}\ x:=E \ \{P\} } \!</math>

Here <math>P[x/E]</math> denotes the expression P in which all free occurrences of the variable x have been replaced with the expression E. The meaning of the assignment axiom is that the truth of <math>\{P[x/E]\}</math> is equivalent to the after-assignment truth of <math>\{P\}</math>. Thus if <math>\{P[x/E]\}</math> were true prior to the assignment, by the assignment axiom then <math>\{P\}</math> will be true subsequent to that assignment. Conversely, if <math>\{P[x/E]\}</math> were false prior to the assignment statement, <math>\{P\}</math> must then be false following the assignment. Examples of valid triples include:

  • <math>\{x+1 = 43\}\ y:=x + 1\ \{ y = 43 \}\!</math>
  • <math>\{x + 1 \leq N \}\ x := x + 1\ \{x \leq N\}\ \!</math>

The assignment axiom proposed by Hoare does not apply when more than one name can refer to the same stored value. For example,

<math>\{ y = 3\} \ x := 2\ \{y = 3 \}</math>

is not a true statement if x and y refer to the same variable, because no precondition can cause y to be 3 after x is set to 2.

Rule of composition

Hoare's rule of composition applies to sequentially-executed programs S and T, where S executes prior to T and is written S;T.

<math> \frac {\{P\}\ S\ \{Q\}\ , \ \{Q\}\ T\ \{R\} } {\{P\}\ S;T\ \{R\}} \!</math>

For example, consider the following two instances of the assignment axiom:

<math>\{ x + 1 = 43\} \ y:=x + 1\ \{y =43 \}</math>

and

<math>\{ y = 43\} \ z:=y\ \{z =43 \}</math>

By the sequencing rule, one concludes:

<math>\{ x + 1 = 43\} \ y:=x + 1; z:= y\ \{z =43 \}</math>

Conditional rule

<math>\frac { \{B \wedge P\}\ S\ \{Q\}\ ,\ \{\neg B \wedge P \}\ T\ \{Q\} }
             { \{P\}\ \textbf{if}\ B\ \textbf{then}\ S\ \textbf{else}\ T\ \textbf{endif}\ \{Q\} } \!</math>

While rule

<math>\frac { \{P \wedge B \}\ S\ \{P\} }
             { \{P \}\ \textbf{while}\ B\ \textbf{do}\ S\ \textbf{done}\ \{\neg B \wedge P\} }

\!</math> Here P is the loop invariant.

Consequence rule

<math>

\frac { P^\prime \rightarrow\ P\ ,\ \lbrace P \rbrace\ S\ \lbrace Q \rbrace\ ,\ Q \rightarrow\ Q^\prime }

   { \lbrace P^\prime\ \rbrace\ S\ \lbrace Q^\prime\rbrace }

\!</math>

While rule for total correctness

<math>

\frac { \{P \wedge B \wedge t = z \}\ S\ \{P \wedge t < z \} \ ,\ P \rightarrow t \geq 0}

             { \{P \}\ \textbf{while}\ B\ \textbf{do}\ S\ \textbf{done}\ \{\neg B \wedge P\} }

\!</math> In this rule, in addition to maintaining the loop invariant, one also proves termination by way of a term whose value decreases during each iteration, here t. Note that t must take values from a well-founded set, so that each step of the loop is counted by decreasing members of a finite chain.

Examples

Example 1
<math>\{x+1 = 43\}\!</math> <math>\ y:=x + 1\ \!</math> <math>\{ y = 43 \}\!</math> (Assignment Axiom)
<math>( x + 1 = 43 \implies x = 42 )</math>
<math>\{x=42\}\!</math> <math>\ y:=x + 1\ \!</math> <math>\{y=43 \land x=42\}\!</math> (Consequence Rule)
Example 2
<math>\{x + 1 \leq N \}\!</math> <math>\ x := x + 1\ \!</math> <math>\{x \leq N\}\ \!</math> (Assignment Axiom)
(<math> x < N \implies x + 1 \leq N</math> for x, N with integer types.)
<math>\{x < N \}\!</math> <math>\ x := x + 1\ \!</math> <math>\{x \leq N\}\ \!</math> (Consequence Rule)

See also

References

View More Summaries on Hoare logic
 
Ask any question on Hoare logic and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
Hoare logic from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy