ArticleslgStudy

computer science

Hoare logic

Hoare logic is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Hoare logic rather than just read about it. In short: Hoare logic (also known as Floyd–Hoare logic or Hoare rules) is a formal system 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.

Key takeaways

  • Hoare logic belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Hoare logic to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Hoare logic from memory before moving on to harder problems.

Reference excerpt

Hoare logic (also known as Floyd–Hoare logic or Hoare rules) is a formal system 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, 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

{ P } C { Q } {\displaystyle \{P\}C\{Q\}}

where P {\displaystyle P} and Q {\displaystyle Q} are assertions and C {\displaystyle C} is a command. P {\displaystyle P} is named the precondition and Q {\displaystyle 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, 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 P {\displaystyle P} holds of the state before the execution of C {\displaystyle C} , then Q {\displaystyle Q} will hold afterwards, or C {\displaystyle C} does not terminate. In the latter case, there is no "after", so Q {\displaystyle Q} can be any statement at all. Indeed, one can choose Q {\displaystyle Q} to be false to express that C {\displaystyle 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:

Another deficiency in the axioms and rules quoted above is that they give no basis for a proof that a program successfully terminates. Failure to terminate may be due to an infinite loop; or it may be due to violation of an implementation-defined limit, for example, the range of numeric operands, the size of storage, or an operating system time limit. Thus the notation “ P { Q } R {\displaystyle P\{Q\}R} ” should be interpreted “provided that the program successfully terminates, the properties of its results are described by R {\displaystyle R} .” It is fairly easy to adapt the axioms so that they cannot be used to predict the “results” of nonterminating programs; but the actual use of the axioms would now depend on knowledge of many implementation-dependent features, for example, the size and speed of the computer, the range of numbers, and the choice of overflow technique. Apart from proofs of the avoidance of infinite loops, it is probably better to prove the “conditional” correctness of a program and rely on an implementation to give a warning if it has had to abandon execution of the program as a result of violation of an implementation limit.

Rules

Empty statement axiom schema The empty statement rule asserts that the skip statement does not change the state of the program, thus whatever holds true before skip also holds true afterwards.

{ P } skip { P } {\displaystyle {\dfrac {}{\{P\}{\texttt {skip}}\{P\}}}}

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 P be an assertion in which the variable x is free. Then:

{ P [ E / x ] } x := E { P } {\displaystyle {\dfrac {}{\{P[E/x]\}x:=E\{P\}}}}

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Hoare logic

Start with the simplest possible case. Write down what Hoare logic claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Hoare logic before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Hoare logic ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Hoare logic

In research
Hoare logic appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Hoare logic in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Hoare logic is common in secondary-school and first-year university syllabi. It links to neighbouring topics 1969 in computing, Program logic, Static program analysis, so understanding it makes those chapters shorter.
In everyday life
Look for Hoare logic outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Hoare logic in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Hoare logic means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Hoare logic out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Hoare logic in simple terms?

Hoare logic (also known as Floyd–Hoare logic or Hoare rules) is a formal system 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 o…

Why does Hoare logic matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Hoare logic?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Hoare logic.

Tags

  • 1969 in computing
  • Program logic
  • Static program analysis
  • Tony Hoare

Keep exploring