Question: Consider the program: / / { N > = 0 } # P i = 0 ; while ( i < N ) { /
Consider the program:
N # P
i ;
while i N
I loop invariant
i i ;
i N # Q
The while loop is the same as
while true
I loop invariant
If i N break;
i i ;
Using a sufficiently strong invariant to prove the program is correct,
Let I i N we want to find that eventually P which is N implies our WP
WPi :; while i N i N do i : i i N
WPi :; WPwhile i N i N do i: i i N
Prove inner WP a loop with three rules and then combining them
I : the loop invariant should hold when entering the loop
I & b WPS I : entering the loop because b is true I is preserved after each loop body execution
I & b Q exiting the loop because b is false when exiting the loop, the post condition holds
I
i N
I & b WPS I
i N & i N WPi : i i N
i N WPi : i i N
i N i N
i Ni N
i Ni N
i N i N
True
I & b Q
i N & i Ni N
i N & i Ni N
i Ni N
True
Combine to make i N & True & True
i N
WPi:; i N
N
P
Since the WP results in P and P P is a tautology then we know the program satisfies the preconditions.
Attempt to prove the program using an insufficiently strong invariant, describe what happens and why.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
