Process

  1. Formula a LI. The LI should describe the purpose of every variable
  2. Use Induction to prove LI
  3. Use the LI in step 1 to prove Partial Correctness Step ()
  4. Use LI in step 1 to prove Termination. We start by finding an expression that uses variables in the loop such that:
    1. The value of is a Natural Number starting the loop
    2. The value of decreases with every iteration
  5. Prove the sequence of values of after each iteration is a sequence of Natural Number, this means proving:
    1. The value of is always a natural number
    2. 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

  1. We find the LI with three parts:
  2. Prove the LI, (prove it holds at the start and after every iteration)
  3. Basis: On entering the loop, , then we get that holds, so LI holds
  4. Induction step:
    1. Suppose LI holds before iteration () IH
    2. Then, denote to be the values after the iteration
    3. by line
    4. by line
      1. by IH
    5. Also, by line
      1. by IH
    6. Also, by line
      1. Then, it follows that
      2. Since
      3. Thus,
    7. Thus, we have shown all conditions of LI
  5. Now we prove Partial Correctness
    1. Suppose the loop terminates, then consider the values on exit
    2. By LI,
    3. By exit condition,
    4. Thus,
    5. By LI,
    6. Therefore, by line as wanted
  6. Now we find an expression that forms a decreasing sequence of natural numbers
    1. Choose
  7. Then, we show that is decreasing sequence
    1. By LI,
    2. So, this means
    3. Thus,
    4. Consider arbitrary iteration
      1. Then,
      2. By line
      3. Thus, is always decreasing
    5. Therefore, the sequence of forms a decreasing sequence of natural numbers