Process
- Formula a LI. The LI should describe the purpose of every variable
- Use Induction to prove LI
- Use the LI in step 1 to prove Partial Correctness Step ()
- Use LI in step 1 to prove Termination. We start by finding an expression that uses variables in the loop such that:
- The value of is a Natural Number starting the loop
- The value of decreases with every iteration
- Prove the sequence of values of after each iteration is a sequence of Natural Number, this means proving:
- The value of is always a natural number
- The value of decreases after every iteration (i.e )
Example
https://cmsweb.utsc.utoronto.ca/nick/cscB36/additional-notes/sample-correct.pdf
- Precondition
- Postcondition: Return
SQ(n)
(1) s=0; d=1; i=0;
(2) while i < n:
(3) s=s+d
(4) d=d+2
(5) i=i+1
(6) return s
Proof
- We find the LI with three parts:
- Prove the LI, (prove it holds at the start and after every iteration)
- Basis: On entering the loop, , then we get that holds, so LI holds
- Induction step:
- Now we prove Partial Correctness
- Now we find an expression that forms a decreasing sequence of natural numbers
- Choose
- Then, we show that is decreasing sequence
- By LI,
- So, this means
- Thus,
- Consider arbitrary iteration
- Then,
- By line
- Thus, is always decreasing
- Therefore, the sequence of forms a decreasing sequence of natural numbers