Math 2.0

Computer science Definition

Hoare logic

Builds on Predicate logicOperational semanticsSubstitutionSoundness

Definition

Consider the while language, whose commands are C::=๐—Œ๐—„๐—‚๐—‰โˆฃx:=EโˆฃC;Cโˆฃ๐—‚๐–ฟย Bย ๐—๐—๐–พ๐—‡ย Cย ๐–พ๐—…๐—Œ๐–พย Cโˆฃ๐—๐—๐—‚๐—…๐–พย Bย ๐–ฝ๐—ˆย CC ::= \mathsf{skip} \mid x := E \mid C; C \mid \mathsf{if}\ B\ \mathsf{then}\ C\ \mathsf{else}\ C \mid \mathsf{while}\ B\ \mathsf{do}\ C, where EE ranges over integer expressions and BB over boolean expressions in the program variables. Assertions P,Q,RP, Q, R are formulas of first-order predicate logic over the integers whose free variables may include the program variables. A Hoare triple is an expression {P}โ€‰Cโ€‰{Q}\set{P}\, C\, \set{Q}. Runs of commands are given by the operational semantics of the language. The triple is valid if, for every state ss satisfying PP, every terminating run of CC from ss ends in a state satisfying QQ. Hoare logic is the proof system whose derivable triples are generated by these rules.

  1. Skip: {P}โ€‰๐—Œ๐—„๐—‚๐—‰โ€‰{P}\set{P}\, \mathsf{skip}\, \set{P}.
  2. Assignment: {Q[E/x]}โ€‰x:=Eโ€‰{Q}\set{Q[E/x]}\, x := E\, \set{Q}, where Q[E/x]Q[E/x] is the substitution of EE for xx in QQ.
  3. Composition: from {P}โ€‰C1โ€‰{R}\set{P}\, C_1\, \set{R} and {R}โ€‰C2โ€‰{Q}\set{R}\, C_2\, \set{Q} derive {P}โ€‰C1;C2โ€‰{Q}\set{P}\, C_1; C_2\, \set{Q}.
  4. Conditional: from {PโˆงB}โ€‰C1โ€‰{Q}\set{P \land B}\, C_1\, \set{Q} and {PโˆงยฌB}โ€‰C2โ€‰{Q}\set{P \land \lnot B}\, C_2\, \set{Q} derive {P}โ€‰๐—‚๐–ฟย Bย ๐—๐—๐–พ๐—‡ย C1ย ๐–พ๐—…๐—Œ๐–พย C2โ€‰{Q}\set{P}\, \mathsf{if}\ B\ \mathsf{then}\ C_1\ \mathsf{else}\ C_2\, \set{Q}.
  5. While: from {IโˆงB}โ€‰Cโ€‰{I}\set{I \land B}\, C\, \set{I} derive {I}โ€‰๐—๐—๐—‚๐—…๐–พย Bย ๐–ฝ๐—ˆย Cโ€‰{IโˆงยฌB}\set{I}\, \mathsf{while}\ B\ \mathsf{do}\ C\, \set{I \land \lnot B}.
  6. Consequence: from Pโ‡’Pโ€ฒP \Rightarrow P', {Pโ€ฒ}โ€‰Cโ€‰{Qโ€ฒ}\set{P'}\, C\, \set{Q'} and Qโ€ฒโ‡’QQ' \Rightarrow Q, where the implications are true in the integers, derive {P}โ€‰Cโ€‰{Q}\set{P}\, C\, \set{Q}.

Remarks

Example: {x=1}ย x:=x+1ย {x=2}\set{x = 1}\ x := x + 1\ \set{x = 2} is derivable. Rule 2 gives {x+1=2}ย x:=x+1ย {x=2}\set{x + 1 = 2}\ x := x + 1\ \set{x = 2}, and rule 6 applies since x=1x = 1 implies x+1=2x + 1 = 2.

Non-example: {๐—๐—‹๐—Ž๐–พ}ย x:=x+1ย {x=2}\set{\mathsf{true}}\ x := x + 1\ \set{x = 2} is not valid, since from the state x=5x = 5 the command ends with x=6x = 6. It is not derivable either, because Hoare logic is sound: every derivable triple is valid.

The triples above express partial correctness: {๐—๐—‹๐—Ž๐–พ}ย ๐—๐—๐—‚๐—…๐–พย ๐—๐—‹๐—Ž๐–พย ๐–ฝ๐—ˆย ๐—Œ๐—„๐—‚๐—‰ย {๐–ฟ๐–บ๐—…๐—Œ๐–พ}\set{\mathsf{true}}\ \mathsf{while}\ \mathsf{true}\ \mathsf{do}\ \mathsf{skip}\ \set{\mathsf{false}} is valid, because the loop never terminates. Total correctness, written [P]โ€‰Cโ€‰[Q][P]\, C\, [Q], also requires termination, and its while rule needs a variant that decreases in a well-founded order. Stephen Cook proved that the system is relatively complete: every valid triple is derivable if all true assertions of the integers may be used in rule 6.

Reading path

To understand this page from the ground up, read these 14 pages in order. The foundations come first.

Show the path as a graph
Each page uses the pages above it. The colour of the bar on a box shows how strong the evidence is, and the box also gives it as text. The list has the same pages.
  • no evidence level
  1. Binary relation concept
  2. Proposition concept
  3. Soundness concept
  4. Substitution concept
  5. Operational semantics concept
  6. Conjunction definition
  7. Disjunction definition
  8. Implication definition
  9. Negation definition
  10. Predicate definition
  11. Biconditional definition
  12. Propositional logic concept
  13. Predicate logic concept
  14. Hoare logic definition

Further reading

  1. Web https://en.wikipedia.org/wiki/Hoare_logic
About this page
Tag34XG5YRMPH
URIm2://34XG5YRMPH
Kinddefinition
WritingW0 stub
MSC68Q60, 03B70
DefinesHoare logic
UsesPredicate logic, Operational semantics, Substitution, Soundness
Changed3 November 2026

Markdown ยท JSON ยท Edit on GitHub

What this page needs for W1

  • an inventory row: the row of the inventory that the node accounts for (0 of 1)
  • a source with a locator (0 of 1)

The writing levels say how complete the text of a page is. They do not measure its truth.

What links here

No page links to this page yet.