Skip to main content

CSE 331: Forward and backward reasoning

In each question, fill in each blank by applying Lecture 7's assignment rules and conditional rule. Treat the arithmetic in the claims as exact. You may simplify a claim, but do not weaken it. Use the rule of consequence when checking an implication.

You may assume no arithmetic here ever overflows.

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

Question 1: backward and forward through an if/else

Use forward reasoning for P1, P2, and P, and backward reasoning for Q1, Q2, and the unnamed blanks inside each branch. Then show that P1 implies Q1, that P2 implies Q2, and that P implies Q.

For the backward steps, use 4 <= c as the target at the end of each branch. For the final forward step, start from the given 4 <= c immediately before c = c + 2.

/**
 * @param a the input; requires 0 <= a <= 4
 * @return a value at least 5
 */
public static int q1(int a) {
    int b;
    int c;
    if (a < 2) {
        {{ P1: ______________________________ }}
        {{ Q1: ______________________________ }}
        b = a + 1;
        {{ ______________________________ }}
        c = 2 * b + 2;
        {{ ______________________________ }}
    } else {
        {{ P2: ______________________________ }}
        {{ Q2: ______________________________ }}
        b = a - 1;
        {{ ______________________________ }}
        c = 3 * b + 5;
        {{ ______________________________ }}
    }
    {{ 4 <= c }}
    c = c + 2;
    {{ P: __________________________________ }}
    {{ Q: 5 <= c }}
    return c;
}
Solution
public static int q1(int a) {
    int b;
    int c;
    if (a < 2) {
        {{ P1: 0 <= a <= 4 and a < 2 }}
        {{ Q1: 4 <= 2 * (a + 1) + 2 }}
        b = a + 1;
        {{ 4 <= 2 * b + 2 }}
        c = 2 * b + 2;
        {{ 4 <= c }}
    } else {
        {{ P2: 0 <= a <= 4 and 2 <= a }}
        {{ Q2: 4 <= 3 * (a - 1) + 5 }}
        b = a - 1;
        {{ 4 <= 3 * b + 5 }}
        c = 3 * b + 5;
        {{ 4 <= c }}
    }
    {{ 4 <= c }}
    c = c + 2;
    {{ P: 6 <= c }}
    {{ Q: 5 <= c }}
    return c;
}

P1 implies Q1. Expanding the expression gives 2 * (a + 1) + 2 = 2 * a + 4. Since 0 <= a, 4 <= 2 * a + 4.

P2 implies Q2. Expanding the expression gives 3 * (a - 1) + 5 = 3 * a + 2. Since 2 <= a, 8 <= 3 * a + 2. Therefore 4 <= 3 * a + 2.

P implies Q. 6 <= c gives 5 <= c.

The rules: assignment, conditional, and consequence.

Question 2: backward through straight-line code

P is given. Use backward reasoning to fill in Q and the unnamed blank. Then show that P implies Q.

/**
 * @param x the input; requires 1 <= x
 * @return a value at least 3
 */
public static int q2(int x) {
    {{ P: 1 <= x }}
    {{ Q: __________________________________ }}
    int t = 3 * x - 2;
    {{ ______________________________ }}
    int y = 2 * t + 1;
    {{ 3 <= y }}
    return y;
}
Solution
public static int q2(int x) {
    {{ P: 1 <= x }}
    {{ Q: 3 <= 2 * (3 * x - 2) + 1 }}
    int t = 3 * x - 2;
    {{ 3 <= 2 * t + 1 }}
    int y = 2 * t + 1;
    {{ 3 <= y }}
    return y;
}

P implies Q. Expanding the expression gives 2 * (3 * x - 2) + 1 = 6 * x - 3. Since 1 <= x, 3 <= 6 * x - 3.

The rules: assignment and consequence.

Question 3: backward through an if/else

P1 and P2 are given. Use backward reasoning to fill in Q1, Q2, and the unnamed blanks. Then show that P1 implies Q1 and that P2 implies Q2.

/**
 * @param a the input; requires 0 <= a <= 6
 * @return a value at least 4
 */
public static int q3(int a) {
    int b;
    int c;
    if (a < 3) {
        {{ P1: 0 <= a < 3 }}
        {{ Q1: ______________________________ }}
        b = a + 1;
        {{ ______________________________ }}
        c = 3 * b + 1;
        {{ ______________________________ }}
    } else {
        {{ P2: 3 <= a <= 6 }}
        {{ Q2: ______________________________ }}
        c = a + 1;
        {{ ______________________________ }}
    }
    {{ 4 <= c }}
    return c;
}
Solution
public static int q3(int a) {
    int b;
    int c;
    if (a < 3) {
        {{ P1: 0 <= a < 3 }}
        {{ Q1: 4 <= 3 * (a + 1) + 1 }}
        b = a + 1;
        {{ 4 <= 3 * b + 1 }}
        c = 3 * b + 1;
        {{ 4 <= c }}
    } else {
        {{ P2: 3 <= a <= 6 }}
        {{ Q2: 4 <= a + 1 }}
        c = a + 1;
        {{ 4 <= c }}
    }
    {{ 4 <= c }}
    return c;
}

P1 implies Q1. Expanding the expression gives 3 * (a + 1) + 1 = 3 * a + 4. Since 0 <= a, 4 <= 3 * a + 4.

P2 implies Q2. 3 <= a gives 4 <= a + 1.

The rules: assignment, conditional, and consequence.

Question 4: forward through an if with no else

Use forward reasoning to fill in each blank. Then show that P implies Q.

/**
 * @param x the input; requires 0 <= x
 * @return a value between 0 and 3, inclusive
 */
public static int q4(int x) {
    int y = x;
    {{ ______________________________ }}
    if (3 < y) {
        {{ ______________________________ }}
        y = 3;
        {{ ______________________________ }}
    }
    {{ P: __________________________________ }}
    {{ Q: 0 <= y <= 3 }}
    return y;
}
Solution
public static int q4(int x) {
    int y = x;
    {{ 0 <= x and y = x }}
    if (3 < y) {
        {{ 0 <= x and y = x and 3 < y }}
        y = 3;
        {{ 3 < x and y = 3 }}
    }
    {{ P: (3 < x and y = 3) or
          (0 <= x and y = x and y <= 3) }}
    {{ Q: 0 <= y <= 3 }}
    return y;
}

The if has no else, so its false branch is empty. The claim at that branch's end combines the claim before the if with not (3 < y): 0 <= x and y = x and y <= 3.

P implies Q. Consider the two cases:

  • If 3 < x and y = 3, substituting 3 for y gives 0 <= 3 <= 3.

  • If 0 <= x and y = x and y <= 3, then 0 <= x and y = x give 0 <= y. The claim already includes y <= 3.

The rules: assignment, conditional, and consequence.

Question 5: forward through an if/else

Use forward reasoning to fill in each blank. Then show that P implies Q.

/**
 * @param n the input; requires 0 <= n
 * @return a value at least 1
 */
public static int q5(int n) {
    int r;
    if (n < 10) {
        {{ ______________________________ }}
        r = n + 1;
        {{ ______________________________ }}
    } else {
        {{ ______________________________ }}
        r = n - 9;
        {{ ______________________________ }}
    }
    {{ P: __________________________________ }}
    {{ Q: 1 <= r }}
    return r;
}
Solution
public static int q5(int n) {
    int r;
    if (n < 10) {
        {{ 0 <= n < 10 }}
        r = n + 1;
        {{ 0 <= n < 10 and r = n + 1 }}
    } else {
        {{ 10 <= n }}
        r = n - 9;
        {{ 10 <= n and r = n - 9 }}
    }
    {{ P: (0 <= n < 10 and r = n + 1) or
          (10 <= n and r = n - 9) }}
    {{ Q: 1 <= r }}
    return r;
}

P implies Q. Consider the two cases:

  • If 0 <= n < 10 and r = n + 1, then 0 <= n gives 1 <= r.

  • If 10 <= n and r = n - 9, then 10 <= n gives 1 <= r.

The rules: assignment, conditional, and consequence.

Question 6: forward with old values

x0 is the value x has when the method is called. Use forward reasoning to fill in each blank. Where an assignment changes a variable, name its old value as lecture does, y0 for y. Then show that P implies Q.

/**
 * @param x the input; requires 0 <= x <= 5
 * @return 3 * x0 + 1, where x0 is x at method entry
 */
public static int q6(int x) {
    {{ x = x0 and 0 <= x0 <= 5 }}
    int y = x + 1;
    {{ ______________________________ }}
    x = 2 * x;
    {{ ______________________________ }}
    y = y + x;
    {{ P: __________________________________ }}
    {{ Q: y = 3 * x0 + 1 }}
    return y;
}
Solution
public static int q6(int x) {
    {{ x = x0 and 0 <= x0 <= 5 }}
    int y = x + 1;
    {{ x = x0 and 0 <= x0 <= 5 and y = x0 + 1 }}
    x = 2 * x;
    {{ x = 2 * x0 and 0 <= x0 <= 5 and y = x0 + 1 }}
    y = y + x;
    {{ P: x = 2 * x0 and 0 <= x0 <= 5 and y0 = x0 + 1 and
          y = y0 + x }}
    {{ Q: y = 3 * x0 + 1 }}
    return y;
}

P implies Q. y = y0 + x, y0 = x0 + 1, and x = 2 * x0 give y = (x0 + 1) + 2 * x0 = 3 * x0 + 1.

The rules: assignment and consequence.

Question 7: forward through invertible operations

Use forward reasoning to fill in each blank. Write each claim without a name for an old value: where an assignment can be undone, write the old value as an expression, as lecture does. Then show that P implies Q.

/**
 * @param x the input; requires 2 <= x <= 6
 * @return a value between 1 and 5, inclusive
 */
public static int q7(int x) {
    {{ 2 <= x <= 6 }}
    x = x - 2;
    {{ ______________________________ }}
    x = 5 - x;
    {{ P: __________________________________ }}
    {{ Q: 1 <= x <= 5 }}
    return x;
}
Solution
public static int q7(int x) {
    {{ 2 <= x <= 6 }}
    x = x - 2;
    {{ 0 <= x <= 4 }}
    x = 5 - x;
    {{ P: 1 <= x <= 5 }}
    {{ Q: 1 <= x <= 5 }}
    return x;
}
  • After x = x - 2, the old value of x is x + 2. Substitute into the prior claim to get 2 <= x + 2 <= 6. This simplifies to 0 <= x <= 4.

  • After x = 5 - x, the old value is 5 - x. Substitution gives 0 <= 5 - x <= 4, which simplifies to 1 <= x <= 5.

P implies Q. P is Q.

The rules: assignment and consequence.

Question 8: forward reasoning with two variables

Use forward reasoning to fill in each blank, writing each claim without a name for an old value, as in Question 7. Then show that P implies Q.

/**
 * @param x the first input; requires 0 <= x <= 3
 * @param y the second input; requires 1 <= y <= 2
 * @return a value between -3 and 3, inclusive
 */
public static int q8(int x, int y) {
    {{ 0 <= x <= 3 and 1 <= y <= 2 }}
    x = x + y;
    {{ ______________________________ }}
    y = 3 - y;
    {{ ______________________________ }}
    x = x - 2 * y;
    {{ P: __________________________________ }}
    {{ Q: -3 <= x <= 3 }}
    return x;
}
Solution
public static int q8(int x, int y) {
    {{ 0 <= x <= 3 and 1 <= y <= 2 }}
    x = x + y;
    {{ 0 <= x - y <= 3 and 1 <= y <= 2 }}
    y = 3 - y;
    {{ 3 <= x + y <= 6 and 1 <= y <= 2 }}
    x = x - 2 * y;
    {{ P: 3 <= x + 3 * y <= 6 and 1 <= y <= 2 }}
    {{ Q: -3 <= x <= 3 }}
    return x;
}
  • After x = x + y, the old x is x - y. Substitution gives 0 <= x - y <= 3. The bounds on y are unchanged.

  • After y = 3 - y, the old y is 3 - y. Substitute into both facts to get 0 <= x - (3 - y) <= 3 and 1 <= 3 - y <= 2. These simplify to 3 <= x + y <= 6 and 1 <= y <= 2.

  • After x = x - 2 * y, the old x is x + 2 * y. Substitution gives 3 <= (x + 2 * y) + y <= 6. The bounds on y remain true.

P implies Q. Check each bound:

  • 3 <= x + 3 * y gives 3 - 3 * y <= x. Since y <= 2, -3 = 3 - 6 <= 3 - 3 * y <= x.

  • x + 3 * y <= 6 gives x <= 6 - 3 * y. Since 1 <= y, x <= 6 - 3 * y <= 6 - 3 = 3.

The rules: assignment and consequence.

Question 9: does P imply Q?

Use forward reasoning to fill in P1 and P2. The condition 3 <= c is given at the end of each branch as the target for backward reasoning. Work backward from those conditions to fill in Q1 and Q2. Then determine whether P1 implies Q1 and whether P2 implies Q2. Explain each implication that is true. For an implication that is false, give a counterexample input and explain what goes wrong.

/**
 * @param a the input; requires 0 <= a <= 4
 * @return a value at least 3
 */
public static int q9(int a) {
    int c;
    if (a < 3) {
        {{ P1: ______________________________ }}
        {{ Q1: ______________________________ }}
        c = a + 3;
        {{ 3 <= c }}
    } else {
        {{ P2: ______________________________ }}
        {{ Q2: ______________________________ }}
        c = a - 2;
        {{ 3 <= c }}
    }
    {{ 3 <= c }}
    return c;
}
Solution
public static int q9(int a) {
    int c;
    if (a < 3) {
        {{ P1: 0 <= a < 3 }}
        {{ Q1: 3 <= a + 3 }}
        c = a + 3;
        {{ 3 <= c }}
    } else {
        {{ P2: 3 <= a <= 4 }}
        {{ Q2: 3 <= a - 2 }}
        c = a - 2;
        {{ 3 <= c }}
    }
    {{ 3 <= c }}
    return c;
}

P1 implies Q1. Yes. 0 <= a gives 3 <= a + 3.

P2 does not imply Q2. Q2 requires 5 <= a, but P2 allows a = 3. That input makes c = 1, so the target 3 <= c is false. The other allowed input in this branch, a = 4, also fails: it makes c = 2.

The rules: assignment, conditional, and consequence.