SE 504 Spring 2019
HW #2: Problem 7 Sample Solution

7. Prove that the following Hoare Triple is valid:

{P: x = X}
if x ≥ 0 ⟶ skip
[] x ≤ 0 ⟶ x = -x
fi
{Q: x = |X|}

Solution: By the Hoare Triple Selection Command Law, it suffices to prove these three things:

The first one is trivial because, by "number theory", the consequent reduces to true, which (by (3.72)) makes the whole implication reduce to true. In gory detail:

    x = X ⟹ x≥0 ∨ x≤0

=     < number theory >

    x = X ⟹ true     <-------- (3.72) with p := x=X

As for the second and third, by the Hoare Triple skip Law and the Hoare Triple Assignment Law, they are equivalent to, respectively,

[P ∧ x≥0 ⟹ Q]   and   [P ∧ x≤0 ⟹ Q(x:=-x)]

Here are side-by-side proofs:

Assume P (i.e., x=X) and x≥0

    Q

=     < defn of Q >

    x = |X|

=     < assumption x=X >

    x = |x|

=     < given absolute value theorem >

    x ≥ 0

=     < assumption >

    true
Assume P (i.e., x=X) and x≤0.

    Q(x:=-x)

=     < defn of Q >

    (x = |X|)(x:=-x)

=     < textual substitution >

    -x = |X|

=     < assumption x=X >

    -x = |x|

=     < given absolute value theorem >

    x ≤ 0

=     < assumption >

    true