JMU
Program Correctness
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
Our Current Objective
Assertions
Loops
Loop Correctness
Loop Invariant Theorem

Given a while loop with guard \(G\), pre-conditions, post-conditions, and a predicate \(I(k)\) describing the loop invariant at iteration \(k\), the loop is correct if the following hold:

  1. \(I(0)\) is true. (Basis Property)
  2. If \(G\) is true and \(I(k)\) is true then \(I(k+1)\) is true. (Inductive Property)
  3. \(G\) becomes false after a finite number of iterations. (Eventual Falsity of the Guard)
  4. Letting \(n\) denote the smallest number of iterations after which \(G\) is false, if \(I(n)\) is true then the post-conditions are true. (Correctness of the Post-Conditions)
An Example: Integer Division
An Example: Integer Multiplication
Integer Multiplication (cont.)
Integer Multiplication (cont.)

Basis Property:

  1. \(k = 0\) is true
  2. \(p = k x\) is true because \(0 = 0 x\) is true for all \(x\)
Integer Multiplication (cont.)

Inductive Property:

Suppose that before iteration \(k\) of the loop the guard is true (i.e., \(k \lt m\) is true) and \(I(k)\) is true (i.e., \(k = n\) is true and \(p = n x\) is true).

We must show that after the iteration, \(I(k+1)\) is true.

  1. \(p_{k+1} = p_{k} + x = k x + x = (k + 1) x\) is true
  2. \(k + 1 = k + 1\) is true
  3. So \(I(k+1)\) is true
Integer Multiplication (cont.)

Eventual Falsity of the Guard:

  1. \(k = m\) is true
  2. So the guard is true after \(m\) iterations
Integer Multiplication (cont.)

Correctness of the Post Condition:

  1. \(G\) becomes false after \(m\) iterations
  2. So \(m = k = n\) is true and \(p = m x\) is true
An Example: The Floor of the Square Root
The Floor of the Square Root (cont.)
Another Correct Algorithm
unsigned int floorsqrt(unsigned long x)
{
    unsigned long r, nr, m;
 
    r = 0;
    m = 0x40000000;
    do 
    {
        nr = r + m;
        if (nr <= x) 
        {
            x -= nr;
            r = nr + m;
        }
        r >>= 1;
        m >>= 2;

    } while (m != 0);
 
    return(r);
}
The Floor of the Square Root (cont.)
An Example: The Euclidean Algorithm
Euclidean Algorithm (cont.)
Euclidean Algorithm (cont.)

Basis Property:

  1. \(I(0) \doteq (\gcd(a_0 ,b_0) = \gcd(a_0, b_0)) \wedge (b_0 \geq 0) \wedge (a_0 \gt b_0)\) is true
Euclidean Algorithm (cont.)

Inductive Property:

Suppose that before iteration \(k\) of the loop the guard is true (i.e., \(b_k \gt 0\) is true) and \(I(k)\) is true [i.e., \(\gcd(a_k , b_k) = \gcd(a_0 , b_0)\) is true and \(b_k \geq 0\) is true and \(a_k \gt b_k\) is true].

We must show that after the iteration, \(I(k+1)\) is true (i.e., \(\gcd(a_{k+1},b_{k+1}) = \gcd(a_0 , b_0)\) is true and \(b_{k+1} \geq 0\) is true and \(a_{k+1} \gt b_{k+1}\) is true).

  1. \(a_{k+1} = b_{k}\) is true
  2. \(b_{k+1} = r_{k+1} = a_{k} \mod b_{k}\) is true
  3. \(\gcd(a_{k+1}, b_{k+1}) = \gcd(b_{k}, b_{k+1})\) is true
  4. \(= \gcd(b_{k}, a_{k})\) is true (from the lemma)
  5. \(= \gcd(a_{0},b_{0})\) is true (because \(I(k)\) is true)
  6. \(b_{k+1} \geq 0\) is true
  7. \(a_{k+1} \gt b_{k+1}\) is true
  8. So \(I(k+1)\) is true
Euclidean Algorithm (cont.)

Eventual Falsity of the Guard:

  1. \(b\) is an integer
  2. Each iteration of the loop reduces \(b\)
  3. \(b\) is non-negative (from the loop invariant)
  4. So, \(b\) will eventually be 0
Euclidean Algorithm (cont.)

Correctness of the Post Condition:

  1. Suppose that for some integer \(n\), the guard, \(G\) is false (in which case \(b = 0\) is true) and \(I(n)\) is true (i.e., \(\gcd(a_{n},b_{n}) = \gcd(a_{0},b_{0})\) is true and \(b_{n} = 0\) is true and \(a_{n} \gt b_{n}\) is true
  2. It follows that \(\gcd(a_{0},b_{0}) = \gcd(a_{n},b_{n}) = \gcd(a_{n},0) = a_{n}\) is true so the post-condition is true