Skip to main content

CSE 331: Floyd-Hoare logic: loops

Use Lecture 8's while-loop rule and Lecture 7's assignment rules to fill in the claims. Check the implications with the rule of consequence. Treat arithmetic in the claims as exact.

You may assume no arithmetic here ever overflows.

For the array questions, a != null is assumed in every claim. No method reassigns a, so this condition remains true throughout.

Each answer is folded under its question. Write yours first.

Question 1: sumTo

The method below sums the numbers from 1 to n.

Fill in each blank. Q is the method's postcondition: read it from the specification. Use forward reasoning for P0, P1, and P2, and backward reasoning for Q2, Q1, and Q0. Then show that P0 implies Inv, that P1 implies Q2, and that P2 implies Q.

/**
 * Sums the numbers from 1 to n.
 * @param n the upper limit; requires 0 <= n
 * @return 0 + 1 + ... + n = n * (n + 1) / 2
 */
public static int sumTo(int n) {
    int i = 0;
    int s = 0;
    {{ P0: ________________________________ }}
    {{ Inv: 0 <= i and i <= n and s = i * (i + 1) / 2 }}
    while (i < n) {
        {{ P1: ________________________________ }}
        {{ Q2: ________________________________ }}
        s = s + (i + 1);
        {{ Q1: ________________________________ }}
        i = i + 1;
        {{ Q0: ________________________________ }}
    }
    {{ P2: ________________________________ }}
    {{ Q: ________________________________ }}
    return s;
}
Solution
public static int sumTo(int n) {
    int i = 0;
    int s = 0;
    {{ P0: 0 <= n and i = 0 and s = 0 }}
    {{ Inv: 0 <= i and i <= n and s = i * (i + 1) / 2 }}
    while (i < n) {
        {{ P1: 0 <= i and i <= n and s = i * (i + 1) / 2 and i < n }}
        {{ Q2: 0 <= i + 1 and i + 1 <= n and
               s + (i + 1) = (i + 1) * (i + 2) / 2 }}
        s = s + (i + 1);
        {{ Q1: 0 <= i + 1 and i + 1 <= n and
               s = (i + 1) * ((i + 1) + 1) / 2 }}
        i = i + 1;
        {{ Q0: Inv }}
    }
    {{ P2: 0 <= i and i <= n and s = i * (i + 1) / 2 and n <= i }}
    {{ Q: s = n * (n + 1) / 2 }}
    return s;
}

P0 implies Inv. Check each part:

  • i = 0 gives 0 <= i.

  • 0 <= n and i = 0 give i <= n.

  • Since i = 0 and s = 0, both sides of s = i * (i + 1) / 2 are zero.

P1 implies Q2. Since 0 <= i and i < n, we have 0 <= i + 1 and i + 1 <= n. The invariant gives s = i * (i + 1) / 2, so:

s + (i + 1)
    = i * (i + 1) / 2 + 2 * (i + 1) / 2
    = (i + 1) * (i + 2) / 2

P2 implies Q. i <= n and n <= i give i = n, so s = i * (i + 1) / 2 is s = n * (n + 1) / 2.

The rules: while loop, assignment, and consequence.

Question 2: max

The method below finds the maximum element of an array. We write a[0 .. i] for the elements of a from index 0 up to, but not including, index i. Thus a[0 .. 1] contains just a[0]. In a claim, max means the mathematical maximum of those elements. The prefix must be nonempty for its maximum to be defined. You may name a variable's old value as lecture does, m0 for m.

Fill in each blank. Q is the method's postcondition: read it from the specification. Use forward reasoning for P0, P1, P2, and the three blanks without a label, and backward reasoning for Q2 and Q1. Then show that P0 implies Inv, that P1 implies Q2, and that P2 implies Q.

/**
 * Finds the maximum element of an array.
 * @param a the array; must be non-null and nonempty
 * @return the maximum element of a
 */
public static int max(int[] a) {
    int m = a[0];
    int i = 1;
    {{ P0: ________________________________ }}
    {{ Inv: 1 <= i and i <= a.length and m = max(a[0 .. i]) }}
    while (i < a.length) {
        {{ ________________________________ }}
        if (m < a[i]) {
            {{ ________________________________ }}
            m = a[i];
            {{ ________________________________ }}
        }
        {{ P1: ________________________________ }}
        {{ Q2: ________________________________ }}
        i = i + 1;
        {{ Q1: ________________________________ }}
    }
    {{ P2: ________________________________ }}
    {{ Q: ________________________________ }}
    return m;
}
Solution
public static int max(int[] a) {
    int m = a[0];
    int i = 1;
    {{ P0: 0 < a.length and m = a[0] and i = 1 }}
    {{ Inv: 1 <= i and i <= a.length and m = max(a[0 .. i]) }}
    while (i < a.length) {
        {{ 1 <= i and i <= a.length and m = max(a[0 .. i]) and
           i < a.length }}
        if (m < a[i]) {
            {{ 1 <= i and i <= a.length and m = max(a[0 .. i]) and
               i < a.length and m < a[i] }}
            m = a[i];
            {{ 1 <= i and i < a.length and m0 = max(a[0 .. i]) and
               m0 < a[i] and m = a[i] }}
        }
        {{ P1: 1 <= i and i < a.length and
               ((m0 = max(a[0 .. i]) and m0 < a[i] and m = a[i]) or
                (m = max(a[0 .. i]) and a[i] <= m)) }}
        {{ Q2: 1 <= i + 1 and i + 1 <= a.length and
               m = max(a[0 .. i + 1]) }}
        i = i + 1;
        {{ Q1: Inv }}
    }
    {{ P2: 1 <= i and i <= a.length and m = max(a[0 .. i]) and
           a.length <= i }}
    {{ Q: m = max(a) }}
    return m;
}

P0 implies Inv. P0 gives 0 < a.length, m = a[0], and i = 1:

  • Since i = 1, the lower bound 1 <= i is true.

  • Since 1 <= a.length, the upper bound i <= a.length is true.

  • The prefix a[0 .. 1] contains just a[0]. Since m = a[0], m = max(a[0 .. i]).

P1 keeps the two branches separate. In the true branch, m0 is the value of m before m = a[i] in that iteration. The false branch leaves m unchanged, so its claim uses m directly. This is Lecture 8's conditional merge for max.

P1 implies Q2. The bounds 1 <= i and i < a.length give 1 <= i + 1 and i + 1 <= a.length. The prefix a[0 .. i + 1] consists of a[0 .. i] followed by a[i]. Consider each branch:

  • In the true branch, m0 is the maximum of the earlier prefix. Since m0 < a[i], the maximum of the extended prefix is a[i]. The assignment makes m = a[i].

  • In the false branch, m remains the maximum of the earlier prefix. Since a[i] <= m, it is also the maximum of the extended prefix.

Thus both cases give m = max(a[0 .. i + 1]).

P2 implies Q. The bounds i <= a.length and a.length <= i give i = a.length. Substituting into the invariant gives m = max(a[0 .. a.length]). That prefix is all of a, so m = max(a).

The rules: while loop, assignment, and consequence.

Question 3: scale

The method below multiplies each element of an array by k. We write a0 for a snapshot of the contents of a when the method is called. Equality with a0 compares contents, not references. The snapshot does not change when a changes.

Write a[p .. q] for the elements from index p up to, but not including, index q. The notation a0[0 .. i] * k means the sequence a0[0] * k, ..., a0[i - 1] * k. Thus a[0 .. i] = a0[0 .. i] * k says a[j] = a0[j] * k for each 0 <= j < i.

Fill in each blank. Use forward reasoning for P0, P1, and P2, and backward reasoning for Q2, Q1, and Q0. Then show that P0 implies Inv, that P1 implies Q2, and that P2 implies Q.

/**
 * Multiplies each element of an array by k.
 * @param a the array to change; must be non-null
 * @param k the multiplier
 * @modifies a
 * @effects a[j] = a0[j] * k for each j with 0 <= j < a.length
 */
public static void scale(int[] a, int k) {
    int i = 0;
    {{ P0: ________________________________ }}
    {{ Inv: 0 <= i and i <= a.length and
            a[0 .. i] = a0[0 .. i] * k and
            a[i .. a.length] = a0[i .. a.length] }}
    while (i < a.length) {
        {{ P1: ________________________________ }}
        {{ Q2: ________________________________ }}
        a[i] = a[i] * k;
        {{ Q1: ________________________________ }}
        i = i + 1;
        {{ Q0: ________________________________ }}
    }
    {{ P2: ________________________________ }}
    {{ Q: a[0 .. a.length] = a0[0 .. a.length] * k }}
}
Solution
public static void scale(int[] a, int k) {
    int i = 0;
    {{ P0: a = a0 and i = 0 }}
    {{ Inv: 0 <= i and i <= a.length and
            a[0 .. i] = a0[0 .. i] * k and
            a[i .. a.length] = a0[i .. a.length] }}
    while (i < a.length) {
        {{ P1: Inv and i < a.length }}
        {{ Q2: 0 <= i and i < a.length and
               a[0 .. i] = a0[0 .. i] * k and
               a[i] * k = a0[i] * k and
               a[i + 1 .. a.length] = a0[i + 1 .. a.length] }}
        a[i] = a[i] * k;
        {{ Q1: 0 <= i + 1 and i + 1 <= a.length and
               a[0 .. i + 1] = a0[0 .. i + 1] * k and
               a[i + 1 .. a.length] = a0[i + 1 .. a.length] }}
        i = i + 1;
        {{ Q0: Inv }}
    }
    {{ P2: Inv and a.length <= i }}
    {{ Q: a[0 .. a.length] = a0[0 .. a.length] * k }}
}

P0 implies Inv. P0 gives a = a0 and i = 0:

  • Since i = 0 and an array's length is nonnegative, 0 <= i and i <= a.length.

  • Both a[0 .. 0] and a0[0 .. 0] * k are empty, so they are equal.

  • Since a = a0, the unprocessed suffix is unchanged: a[0 .. a.length] = a0[0 .. a.length].

The backward array step includes access bounds. Substituting i + 1 for i in Inv gives Q1. To work backward through a[i] = a[i] * k, first require 0 <= i < a.length so the read and write are defined. These bounds also imply the bounds in Q1. Only entry i changes. Split the prefix promised by Q1 into the earlier entries and entry i:

  • The earlier entries must already satisfy a[0 .. i] = a0[0 .. i] * k.

  • At entry i, substitute the value about to be written to get a[i] * k = a0[i] * k.

  • The suffix after i is unchanged by the assignment, so require a[i + 1 .. a.length] = a0[i + 1 .. a.length].

These are the clauses of Q2. The equality at entry i does not require a[i] = a0[i]: when k = 0, both products are zero.

P1 implies Q2. The invariant and the loop guard give 0 <= i < a.length. The invariant already gives the required scaled prefix. Its unchanged suffix gives a[i] = a0[i], hence a[i] * k = a0[i] * k. That suffix also includes every entry strictly after i, as Q2 requires.

P2 implies Q. The bounds i <= a.length and a.length <= i give i = a.length. Therefore the invariant's a[0 .. i] = a0[0 .. i] * k is Q.

The rules: while loop, assignment, and consequence.

Question 4: lastIndexOf

The method below finds the last index of an element in an array. We write a[p .. q] for the elements of a from index p up to, but not including, index q, and x not in a[p .. q] for "no element of a[p .. q] equals x". The slice a[a.length .. a.length] is empty, so x is not in it.

Java's && evaluates its right operand only when its left operand is true. Here, the loop condition reads a[i] only when 0 <= i. The invariant supplies the upper bound i < a.length, so the read is safe. In the exit claim, write separate cases for i = -1 and for a nonnegative index where a[i] = x.

Fill in each blank. Use forward reasoning for P0, P1, and P2, and backward reasoning for Q2 and Q1. Then show that P0 implies Inv, that P1 implies Q2, and that P2 implies Q.

/**
 * Finds the last index of an element in an array.
 * @param a the array to search; must be non-null
 * @param x the value to find
 * @return the largest j with 0 <= j < a.length and a[j] = x,
 *         or -1 if there is no such j
 */
public static int lastIndexOf(int[] a, int x) {
    int i = a.length - 1;
    {{ P0: ________________________________ }}
    {{ Inv: -1 <= i and i < a.length and
            x not in a[i + 1 .. a.length] }}
    while (0 <= i && a[i] != x) {
        {{ P1: ________________________________ }}
        {{ Q2: ________________________________ }}
        i = i - 1;
        {{ Q1: ________________________________ }}
    }
    {{ P2: ________________________________ }}
    {{ Q: (i = -1 and x not in a[0 .. a.length]) or
          (0 <= i and i < a.length and a[i] = x and
           x not in a[i + 1 .. a.length]) }}
    return i;
}
Solution
public static int lastIndexOf(int[] a, int x) {
    int i = a.length - 1;
    {{ P0: 0 <= a.length and i = a.length - 1 }}
    {{ Inv: -1 <= i and i < a.length and
            x not in a[i + 1 .. a.length] }}
    while (0 <= i && a[i] != x) {
        {{ P1: -1 <= i and i < a.length and
               x not in a[i + 1 .. a.length] and
               0 <= i and a[i] != x }}
        {{ Q2: -1 <= i - 1 and i - 1 < a.length and
               x not in a[i .. a.length] }}
        i = i - 1;
        {{ Q1: Inv }}
    }
    {{ P2: -1 <= i and i < a.length and
           x not in a[i + 1 .. a.length] and
           (i = -1 or (0 <= i and a[i] = x)) }}
    {{ Q: (i = -1 and x not in a[0 .. a.length]) or
          (0 <= i and i < a.length and a[i] = x and
           x not in a[i + 1 .. a.length]) }}
    return i;
}

P0 implies Inv. Since 0 <= a.length and i = a.length - 1, we have -1 <= i and i < a.length. Also i + 1 = a.length, so a[i + 1 .. a.length] is empty. Thus x is not in that slice.

P1 implies Q2. Since 0 <= i and i < a.length, we have -1 <= i - 1 and i - 1 < a.length. The invariant gives x not in a[i + 1 .. a.length]. The loop condition also gives a[i] != x, so x is not in the larger slice a[i .. a.length].

P2 implies Q. The exit claim has two cases:

  • If i = -1, then a[i + 1 .. a.length] is the whole array. The invariant says that x is absent, which is the first case of Q. No claim in this case needs to read a[-1].

  • If 0 <= i and a[i] = x, the invariant gives i < a.length and excludes x from every later index. Thus i is the last matching index, which is the second case of Q.

The rules: while loop, assignment, and consequence.