Prove while loop correctness using Hoare logic. Define the loop invariant and show precondition establishes it, body preserves it, and termination implies postcondition.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This is a comprehensive question about proving while loop correctness using Hoare logic. I need to explain:
The general framework for while loop correctness
Define loop invariants
Show the three key conditions (precondition establishes invariant, body preserves invariant, termination implies postcondition)
Provide concrete examples
Let me structure this thoroughly with formal definitions and practical examples.
Hoare Logic for While Loops
The general Hoare triple for a while loop is:
code
{P} while (b) S {Q}
To prove this correct, we need to find a loop invariant I and show:
P → I (precondition establishes invariant)
{I ∧ b} S {I} (body preserves invariant)
I ∧ ¬b → Q (loop termination with invariant implies postcondition)
(Optional but important) Prove termination using a variant function
Let me provide a complete exposition with examples.
Proving While Loop Correctness with Hoare Logic
General Framework
To prove correctness of a while loop using Hoare logic:
code
{P} while (b) S {Q}
We need to find a loop invariant I and prove four conditions:
Condition
Meaning
Establishment
P → I
Preservation
{I ∧ b} S {I}
Termination
I ∧ ¬b → Q
Variant
Progress toward termination
Formal Proof Rule
hoare
P → I
{I ∧ b} S {I}
I ∧ ¬b → Q
─────────────────────────
{P} while (b) S {Q}
Complete Example: Array Sum
Problem
python
# Compute sum of array elements
sum = 0
i = 0
while i < n:
sum = sum + a[i]
i = i + 1
# Postcondition: sum equals sum of a[0..n-1]
Step 1: Establish Precondition and Postcondition
Precondition P:
code
n ≥ 0 ∧ a is an array of length n
Postcondition Q:
code
sum = Σ(a[j]) for j ∈ [0, n-1]
Step 2: Define Loop Invariant
Invariant I:
code
0 ≤ i ≤ n ∧ sum = Σ(a[j]) for j ∈ [0, i-1]
This states:
i is within valid bounds
sum contains the sum of elements already processed
Step 3: Establish Precondition → Invariant
Proof:
code
Given: n ≥ 0, a has length n
Before loop: sum = 0, i = 0
Check invariant I:
1. 0 ≤ 0 ≤ n ✓ (n ≥ 0)
2. sum = Σ(a[j]) for j ∈ [0, -1] ✓ (empty sum = 0)
Therefore: P → I
Step 4: Prove Body Preserves Invariant
Precondition for body: I ∧ b
code
(0 ≤ i ≤ n ∧ sum = Σ(a[j] for j ∈ [0, i-1])) ∧ (i < n)
Loop body:
code
sum = sum + a[i]
i = i + 1
Proof:
code
Assume I ∧ b holds before body execution:
0 ≤ i ≤ n
sum = Σ(a[j]) for j ∈ [0, i-1]
i < n
After sum = sum + a[i]:
sum' = sum + a[i] = Σ(a[j] for j ∈ [0, i])
After i = i + 1:
i' = i + 1
Check invariant I':
1. 0 ≤ i' ≤ n
Since i < n (from b), we have i ≤ n-1
Therefore i' = i+1 ≤ n ✓
2. sum' = Σ(a[j]) for j ∈ [0, i'-1]
sum' = Σ(a[j]) for j ∈ [0, i] ✓
Therefore: {I ∧ b} S {I}
Step 5: Prove Termination Implies Postcondition
Postcondition from invariant: I ∧ ¬b
code
(0 ≤ i ≤ n ∧ sum = Σ(a[j] for j ∈ [0, i-1])) ∧ (i ≥ n)
Proof:
code
When loop exits:
I ∧ ¬b holds
¬b means: ¬(i < n), so i ≥ n
Combined with I:
0 ≤ i ≤ n ∧ sum = Σ(a[j] for j ∈ [0, i-1]) ∧ i ≥ n
This implies:
i = n (from 0 ≤ i ≤ n and i ≥ n)
sum = Σ(a[j]) for j ∈ [0, n-1] ✓ = Q
Therefore: I ∧ ¬b → Q
Step 6: Prove Termination (Variant Function)
Variant V:
code
V = n - i
Properties:
Initially bounded: V = n - 0 = n ≥ 0 ✓
Decreases: V' = n - (i+1) = V - 1 < V ✓
Termination: When V = 0, condition i < n fails ✓
Complete Formal Proof
hoare
┌─────────────────────────────────────────────────────┐
│ Theorem: {n ≥ 0} sum = 0; i = 0; │
│ while (i < n) │
│ sum = sum + a[i]; i = i + 1; │
│ {sum = Σ(a[j]) for j ∈ [0, n-1]} │
└─────────────────────────────────────────────────────┘
Proof:
1. After initialization:
{n ≥ 0} sum = 0; i = 0;
{0 ≤ i ≤ n ∧ sum = Σ(a[j] for j ∈ [0, -1])}
2. Loop invariant I: 0 ≤ i ≤ n ∧ sum = Σ(a[j] for j ∈ [0, i-1])
3. Establishment: P → I ✓ (verified in Step 3)
4. Preservation: {I ∧ i < n} body {I} ✓ (verified in Step 4)
5. Termination: I ∧ ¬(i < n) → Q ✓ (verified in Step 5)
6. Progress: Variant V = n - i decreases ✓
By while loop rule: ⊢ {P} while {Q}
Second Example: Integer Division
Problem
python
# Compute q = ⌊a/b⌋, r = a mod b
q = 0
r = a
while r >= b:
q = q + 1
r = r - b
# Postcondition: a = b*q + r ∧ 0 ≤ r < b
Solution Analysis
Invariant I:
code
a = b*q + r ∧ r ≥ 0 ∧ b > 0
Key Properties:
Initially: a = b*0 + a ✓
After body: a = b*(q+1) + (r-b) ✓
Loop exits when: r < b, giving a = b*q + r ∧ 0 ≤ r < b ✓
Variant:V = r (decreases each iteration while ≥ b)
Common Pitfalls
hoare
❌ WRONG: Using wrong invariant
I = "sum contains something" (too vague)
✓ CORRECT: Precise invariant
I = "sum = Σ(a[j] for j ∈ [0, i-1]) ∧ 0 ≤ i ≤ n"
❌ WRONG: Forgetting bounds check
I = "sum = Σ(a[j])" (missing that array access is valid)
✓ CORRECT: Include all necessary conditions
I = "sum = Σ(a[j] for j ∈ [0, i-1]) ∧ i ≤ n"
❌ WRONG: No termination proof
Loop might not terminate at all
✓ CORRECT: Provide variant function
V = n - i; V decreases and is bounded
Summary
Component
Role
Loop Invariant I
Relation that holds before, during, and after each iteration