A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Hoare Logic

Research Depth 93 in the knowledge graph ☐ I know this ☆ Set as goal
13topics build on this
439prerequisites beneath it
See this on the map →
Introduction to Predicate Logic (First-Order Logic)Introduction to Propositional Logic+1 more→→Concurrency VerificationContract Verification (Blockchain)+6 more
hoare-triple partial-correctness total-correctness program-verification

Core Idea

Hoare logic is a formal system for reasoning about the correctness of imperative programs using triples of the form {P} C {Q}, where P is a precondition, C is a command, and Q is a postcondition. The triple asserts that if P holds before executing C, and C terminates, then Q holds afterward (partial correctness). Hoare logic provides axioms and inference rules for each language construct (assignment, sequencing, conditionals, loops), enabling compositional proofs that a program meets its specification.

Explainer

Hoare logic, introduced by Tony Hoare in 1969, provides a rigorous framework for proving that programs meet their specifications. The central object is the Hoare triple {P} C {Q}. P is a logical assertion (the precondition) describing what must be true before command C executes; Q (the postcondition) describes what will be true afterward. The triple is a partial correctness assertion: it only guarantees Q when C actually terminates. If C loops forever, the triple is vacuously satisfied regardless of Q.

The power of Hoare logic lies in its compositional proof rules — one rule per language construct. The assignment axiom {Q[x/E]} x := E {Q} works backward: to establish postcondition Q after assigning E to x, you need Q with E substituted for every occurrence of x to hold beforehand. The sequencing rule lets you chain triples: if {P} C1 {R} and {R} C2 {Q}, then {P} C1; C2 {Q}. The conditional rule splits on the branch condition: prove the then-branch under the condition and the else-branch under its negation. The while rule introduces a loop invariant I: if {I and B} body {I}, then {I} while B do body {I and not B}. Finding the right loop invariant is typically the hardest step in a Hoare logic proof.

The rule of consequence is the structural glue. It says: if you can prove {P} C {Q}, and P' implies P and Q implies Q', then {P'} C {Q'} holds. This lets you strengthen preconditions (assume more) and weaken postconditions (promise less), which is always sound. Without this rule, composing subproofs would be impractical because each step's postcondition would need to exactly match the next step's precondition.

A key subtlety is that Hoare logic as described only handles partial correctness — a non-terminating program satisfies any specification. To prove total correctness, you must additionally show termination, typically by exhibiting a variant (also called a ranking function): a natural-number-valued expression that decreases with each loop iteration and is bounded below, guaranteeing the loop must eventually exit. This extension connects Hoare logic to well-founded orderings and termination analysis.

Hoare logic provides the conceptual foundation for all subsequent work in program verification. Floyd-Hoare verification automates the process, weakest precondition calculus mechanizes the backward reasoning the assignment axiom uses, and separation logic extends the framework to handle heap-manipulating programs. Understanding Hoare triples and their proof rules is the entry point to the entire field of formal program verification.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Finite State Machines (FSMs) → Deterministic Finite Automata (DFA) → Nondeterministic Finite Automata (NFA) → Two-Way Finite Automata → NFA to DFA Conversion (Subset Construction) → DFA Properties and Minimization Algorithms → Regular Languages: Definition and Characterization → Context-Free Grammars (CFGs) → Context-Free Grammar Properties and Ambiguity → Parse Trees, Derivations, and Ambiguity in CFGs → Context-Free Grammars in Compiler Design → Compiler Phases and Organization → Grammar Design for Compilation → Domain-Specific Language Design and Implementation → Programming Language Semantics → Hoare Logic

Longest path: 94 steps · 439 total prerequisite topics

Prerequisites (3)

Leads To (8)