The goal is to construct a Predicate that holds for all input sizes
Process
- Define : If precondition holds, and program runs and, input size is , then the program halts and the postcondition holds after it halts
- Use PCI to prove that holds for every with base case being the smallest number required of the program
Example
With the algorithm:
isPal(x):
(1) if len(x) < 2: return True
(2) if x[0] != x[len(x) - 1]: return False
(3) return isPal(x[1:len(x)-1])
Then, lets prove that it is correct:
- Define Logical Predicate on where
- We will use PCI that holds for every Base case: Consider cases
- Then, this is correct because every string of length is a palindrome Induction step:
- Let
- Suppose holds whenever Induction Hypothesis
- We want to prove: holds
- Consider two cases:
x[0] != x[len(x)-1]
x[0] = x[len(x)-1]
- If
x[0] != x[len(x)-1]
then,isPal(x)
returns false by- This is correct by specification
- If
x[0] == x[len(x)-1]
then,isPal(x)
returnsisPal(x[1:len(x)-1])
lines isPal(x[1:len(x)-1])
returns true ifx[1:len(x)-1]
is a palindrome, which is true by IH- Thus, this is correct by specification
- Thus, our program is correct because the first and last symbols of are the same, then is a palindrome with its first and last symbols removed is a palindrome