I previously asked this question on StackOverflow about using loop invariants to catch a bug in my program. Now, I want to take the next step and understand how to compute the weakest precondition when a loop invariant is provided.
Background: For a simple if-statement, I understand how to compute the weakest precondition. Consider the following code:
if x > 0:
x = x - 1
else:
x = x + 1
# Postcondition: { x >= 0 }
To ensure that the postcondition x >= 0 holds after execution, I compute the weakest precondition as:
(x>0/\x>=1)\/(x<0/\(x+1)>=0)
which simplifies to:
(x>1)∨(−1<x<0)
However, I don’t know how to compute the weakest precondition when there is a loop and a given loop invariant.
My Question: Consider the following loop:
i = 0
x = user_input
while i < 10:
if x > 0:
x = x - 1
else:
x = x + 1
# Postcondition: { x >= 0 }
For some loop invariant I provided and proved (x>=0), how do I formally infer the weakest precondition for the entire program using my postcondition and invariant with backward reasoning?
ifis wrong; it says x can't be 0, but x ends up as 1 if you start theifwith x=0, which satisfies your postcondition. In fact, it says x can't be 1 either, but x ends up 0 if you start with x=1, which also satisfies your postcondition.