Weakest-Precondition Reasoning What Do We Mean by "Weakest"?
Weakest-Precondition Reasoning What Do We Mean by "Weakest"?
s2 = s2 + s3; l w p(s3 = s3 + 6; …) =
{s1=(i+1) 3 s2=(i+2) 3-(i+1) 3 (s3+6) = 6*(i+2)}
s3 = s3 + 6;
i = i+1;
{s1=i 3 s2=(i+1)3-i 3 s3 = 6*(i+1)} l w p(s2 = s2 + s3; …) =
{s1=(i+1) 3 (s2+s3)=(i+2) 3-(i+1) 3 (s3+6) =6*(i+2)}
l Working backward
w p(i=i+1; …) = l w p(s1 = s1+s2; …) =
{s1=(i+1) 3 s2=(i+2) 3-(i+1) 3 s3 = 6*(i+2)} {(s1+s2)=(i+1) 3 (s2+s3)=(i+2) 3-(i+1) 3 (s3+6) =6*(i+2)}
l {??} l {??}
if( x > y ) x = x-y; else y = y-x; if( x > y ) z = x; else z = y;
{gcd(x, y) = z} {z = max(x, y)}
l {??} while(x > 0) x = x-1; {x==0} l {??} while(x > 0) x = x-1; {x==0}
can be expressed as the predicate H(P) l Check that H = x > 0 satisfies the
= H0(x==0) H1(x==0) H2 (x==0) … recurrence:
l where lx > 0 → (¬ x > 0 → x==0)
l H0(x==0) = ¬ x > 0 → x==0 which is valid, and
lx > 0 → (x > 0 → w p(x=x-1;, x > 0))
l Hk+1(x==0) = x > 0 → w p(x=x-1;, Hk(x==0))
l But wp(x=x-1;, x > 0) is x > 1, so we
check
l x > 0 → (x > 0 → x>1), which is true (for integers)
Example: Alternate way to approach WP
Another way to approach WP for a loop for a loop
l Note that WP for a loop captures total l Consider the java code:
correctness. for( j = 0; j < a.length; j++ )
{
l Since it is generally difficult to derive if( a[j] == v )
WP in a closed form, we may be {
break;
content with finding a pre-condition that }
satisfies the recurrence but is not the }
weakest. assert: a[j] == v
l append(reverse([A | L]), M)
Try to Prove this by
Structural Induction Structural Induction
ACL2 !>
l Normal Lisp execution
(defun app (x y)
l Symbolic execution (cond ((endp x) y)
(t (cons (car x)
l Automated theorem proving (app (cdr x) y)))))
l Formalism for admitting axioms to the
system
(continued)
ACL2 Theorem Prover Output Simplification of the Induction Step
Subgoal *1/1
Subgoal *1/2' (IMPLIES (ENDP A)
(IMPLIES (AND (CONSP A) (EQUAL (APP (APP A B) C)
(EQUAL (APP (APP (CDR A) B) C) (APP A (APP B C)))).
(APP (CDR A) (APP B C))))
(EQUAL (APP (APP A B) C) By the simple :definition ENDP we reduce the conjecture to
(APP A (APP B C)))).
Subgoal *1/1'
But simplification reduces this to T, using the :definition APP, the (IMPLIES (NOT (CONSP A))
:rewrite rules CDR-CONS and CAR-CONS and primitive type (EQUAL (APP (APP A B) C)
reasoning. (APP A (APP B C)))).
l Once the theorem is proved, it is saved l The problem with universal application
in the system to be used as a rewrite of a rewrite rule is that it can divert from
rule. the main problem.
l The system will henceforth rewrite l For example, resubmitting the previous
(app (app x y) z) theorem would cause an infinite loop in
as the form of repeated application of the
(app x (app y z)) rule.
l This is not necessarily a good thing. l This can be avoided, as shown next.
Goal'
(EQUAL (APP X1
(APP X2
(APP X3 (APP X4 (APP X5 (APP X6 X7))))))
(APP X1
(APP X2
(APP X3 (APP X4 (APP X5 (APP X6 X7))))))).
Q.E.D.