Mathematical Induction - COM1022 Functional Programming

Proving statements about functions
Proving statements about functions
Mathematical Induction
oddsum 0 = 0
oddsum n = oddsum (n-1) + (2*n - 1)
COM1022 Functional Programming Techniques
oddsum 0 = ?
Professor Steve Schneider
oddsum 1 = ?
University of Surrey
oddsum 2 = ?
...
Semester 2, 2010 – Week 09
Can we spot a pattern?
For all n: oddsum n = ?
Can we prove it?
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
1 / 13
Professor Steve Schneider
Proving statements about functions
Mathematical Induction
Semester 2, 2010 – Week 09
3 / 13
Semester 2, 2010 – Week 09
5 / 13
The principle of induction
The general form
The principle of induction
If P(0)
and P(n) ⇒ P(n + 1) for all n
Let P(n) be a statement (predicate) about n.
e.g. P(n) ≡ ”oddsum n = n2 ”
Then we can conclude ∀n.P(n)
∀n.P(n) asserts that P(n) is true for all n.
How can we prove this? We can’t check each n in turn, because
there are infinitely many of them.
An infinite sequence of dominoes...
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
4 / 13
Professor Steve Schneider
Mathematical Induction
The principle of induction
The principle of induction
Inductive step: P(n) ⇒ P(n + 1)
Base case: P(0)
P(0) is true:
We have to show, for any n, that if P(n) is true, then P(n + 1) is
also true.
oddsum 0 = 02
So assume, for some arbitrary n, that P(n) is true. Let’s see if we
can prove that P(n + 1) is true.
The initial case, that is proved directly, is called the base case.
It often corresponds to the base case of the functional definition.
oddsum (n + 1) = oddsum n + (2n + 1)
It is typically established by examining the statement P(0) directly.
= n2
= (n + 1)
[The base case can be some number other than 0. The result will
hold for all numbers from the base case.]
+ (2n + 1)
2
So if P(n) is true, then P(n + 1) is also true
Hence by induction, ∀n.P(n)
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
6 / 13
Professor Steve Schneider
Mathematical Induction
Examples
Semester 2, 2010 – Week 09
7 / 13
Semester 2, 2010 – Week 09
9 / 13
Examples
Example: 4 divides (5n − 1)
Example: sigma n
sigma 0 = 0
sigma n = n + sigma (n-1)
We can prove that 5n − 1 is always divisible by 4.
Q(n) : sigma n =
n(n+1)
2
Base case: n = 0
Inductive step: assume true for n, prove for n + 1
Base case: show Q(0)
Inductive step: show Q(n) ⇒ Q(n + 1)
Then conclude ∀n.Q(n)
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
8 / 13
Professor Steve Schneider
Mathematical Induction
Take and drop
Take and drop
take and drop
a property of take and drop
take 0 xs = []
take n [] = []
take n (x:xs) = x:(take (n-1) xs)
Want to prove that take n xs ++ drop n xs = xs for all n.
drop 0 xs = xs
drop n [] = []
drop n (x:xs) = drop (n-1) xs
Base case: (take 0 xs) ++ (drop 0 xs) = [] ++ xs = xs
Inductive step: need to consider [] and x:xs:
We can prove this by induction:
(take (n+1) []) ++ (drop (n+1) []) = [] ++ [] = []
take (n+1) (x:xs) ++ drop (n+1) (x:xs)
= x:(take n xs) ++ (drop n xs)
= x:((take n xs) ++ (drop n xs))
= x:(xs)
= x:xs
Is it true that (take n xs) ++ (drop n xs) = xs?
What if n is greater than the length of xs?
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
10 / 13
Professor Steve Schneider
Take and drop
Mathematical Induction
Semester 2, 2010 – Week 09
11 / 13
Exercises (in place of lab)
Summary
Exercises
These exercises are instead of a lab for this week. Please try them in
advance of next week.
The principle of (mathematical) induction
Prove each of the following by induction:
To prove some property P(n) is true for all n:
20 + 21 + ... + 2n = 2(n+1) − 1
Show the base case: P(0)
Show the inductive step: P(n) ⇒ P(n + 1)
12 + 22 + ... + n2 =
(n(n+1)(2n+1))
6
4n + 6n − 1 is divisible by 9
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
12 / 13
Professor Steve Schneider
for all n
for all n
for all n
Mathematical Induction
Semester 2, 2010 – Week 09
13 / 13