Download Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Cse536 Functional Programming
Lecture #9, Oct 25, 2004
•Guest lecture by Tom Harke
•Todays Topics
–Review of Proofs by calculation
– Structure of Proofs by induction over lists
–Proofs by induction with case analysis
–Proofs by structural Induction
–Proofs by induction over Trees
•Read Chapter 11 - Proofs by induction
•Home work assignment #5
– See Webpage (this assignment is given on Monday, but you have 10 days)
– Due Wednesday, Nov. 3 (Day of midterm exam)
• Mid-Term Exam
–We need to discuss a midterm exam
–One possibility
» Exam distributed one day
» Due in class on next meeting
» Honor System – Use only two hours of time.
» Notes and use of computer allowed.
5/24/2017
1
Cse536 Functional Programming
Remember, No Class Wednesday
Sun
Oct 24
Mon
Tue
25
Wed
26
Thu
Fri
27
Sat
28
Guest Lect.
Proofs
about
Haskell
programs
31
Nov 1
Guest Lect.
30
Makeup
Class
No Class
Tom Harke
29
Tim Sheard
Regions
2
3
4
5
6
Midterm
exam
Tom Harke
The Haskell
Class System
5/24/2017
2
Cse536 Functional Programming
Recall the calculation proof method
• Substitution of equals for equals.
if
name: f x = e
is a definition or a theorem, then we can replace (f n)
with e[n/x] where ever (f n) occurs.
– name: is the name of the definition or theorem for reference in the
proof.
– e[n/x] means e with all free occurrences of x replaced by n
• For example consider:
comp: (f . g) x = f (g x)
• Now prove that
((f . g) . h) x = (f . (g . h)) x
5/24/2017
3
Cse536 Functional Programming
Proof by calculation
• Pick one side of the equation and transform using
rule comp: above
((f . g) . h) x =
by comp: (left to right)
(f . g) (h x) =
by comp: (left to right)
f (g (h x)) =
by comp: (right to left)
f ((g . h) x)
by comp: (right to left)
(f . (g . h)) x
5/24/2017
4
Cse536 Functional Programming
Proofs by induction over lists
• Format over lists
Let P{x} be some proposition (I.e. P{x} :: Bool)
i.e. P is an expression with some free variable x :: [a]
–
–
x has type :: [a]
x may occur more than once in P{x}
e.g.
length x = length (reverse x)
forall p x => p (head x)
sum (x ++ y) = sum x + sum y
map f (x ++ y) = map f x ++ map f y
(map f . map g) x = map (f . g) x
• Then
1) Prove P { [] }
2) Assume P{ xs } and then
Prove P{ x:xs }
5/24/2017
5
Cse536 Functional Programming
Example
• Definitions and Laws: (These are things we get to assume are
true)
1 sum [] = 0
2 sum (x:xs) = x + (sum xs)
3 [] ++ y = y
4 (x:xs) ++ y = x:(xs ++ y)
• Proposition: (This is what we are trying to prove)
P{x} = sum (x ++ y) = sum x + sum y
• Proof Structure:
– 1) Prove P{[]}:
sum ([] ++ y) = sum [] + sum y
– 2) Assume P{xs}: (as well as the definitions and laws)
sum (xs ++ y) = sum xs + sum y
Then Prove P{x:xs}:
sum ((x:xs) ++ y) = sum (x:xs) + sum y
5/24/2017
6
Cse536 Functional Programming
Proof
1) Prove: sum
([] ++ y) = sum [] + sum y
sum ([] ++ y) =
sum y =
0 + sum y =
sum [] + sum y
(by 3: [] ++ y = y )
(arithmetic: 0 + y = y )
(by 1: sum [] = 0 )
2) Assume: sum (xs
Prove: sum ((x:xs)
++ y) = sum xs + sum y
++ y) = sum (x:xs) + sum y
sum ((x:xs) ++ y) =
sum (x:(xs++y)) =
x + sum(xs++y) =
x + (sum xs + sum y) =
(x + sum xs) + sum y =
sum (x:xs) + sum y
5/24/2017
(by 4: (x:xs) ++ y = x:(xs ++ y) )
(by 2: sum (x:xs) = x + (sum xs) )
(by IH)
(associativity of +: (x + y) + z = x + (y + z) )
(by 2: sum (x:xs) = x + (sum xs) )
7
Cse536 Functional Programming
Proof by induction
using Case Analysis
• Prove by induction:
P(x) ==
(takeWhile p x) ++
(dropWhile p x) = x
• Where:
1 [] ++ ys = ys
2 (x:xs) ++ ys = x : (xs ++ ys)
3 dropWhile p [] = []
4 dropWhile p (x:xs) =
if p x then (dropWhile p xs)
else x::xs
5 takeWhile p [] = []
6 takeWhile p (x:xs) =
if p x then x:(takeWhile p xs)
else []
5/24/2017
8
Cse536 Functional Programming
Base and Ind cases
• Base case: P([])
(takeWhile p []) ++
(dropWhile p []) = []
= [] ++ [] (by 3,5)
= []
(by 1)
• Induction Step:
P(ys) => P(y:ys)
Assume:
(takeWhile p ys) ++
(dropWhile p ys) = ys
Prove:
(takeWhile p (y:ys)) ++
(dropWhile p (y:ys)) = (y : ys)
5/24/2017
9
Cse536 Functional Programming
Split Proof
(takeWhile p (y:ys)) ++
(dropWhile p (y:ys))
(if p y then y : (takeWhile p ys)
else []) ++
++ (if p y then (dropWhile p ys)
else y:ys) (by 4,6)
• Now, either (p y) = True or (p y) = False
• So split problem by doing a case analysis
5/24/2017
10
Cse536 Functional Programming
Case 1: Assume: p y = True
(y : (takeWhile p ys)) ++
(dropWhile p ys)
y : ((takeWhile p ys) ++
(dropWhile p ys)) (by 2)
y : ys (by I.H.)
5/24/2017
11
Cse536 Functional Programming
Case 2: Assume: p y = False
(if p y then y : (takeWhile p ys)else []) ++
(if p y then (dropWhile p ys) else y:ys)
» (by 4,6)
[] ++ y:ys
» (by 1)
y:ys
5/24/2017
12
Cse536 Functional Programming
Structural Induction
• Let
data T = C1 t1,1 t1,2 ... t1,n
| ...
| Cm tm,1 ... tm,k
• Then to prove P(x::T)
• for each Ci:: ti,1 -> ti,2 -> ... -> ti,n -> T
» assume
» and Prove
5/24/2017
P ( xi,j )
if xi,j :: T that is if
P(Ci xi,1 ... xi,n)
ti,j = T
13
Cse536 Functional Programming
Structural induction for Twolist
• Example
data Twolist a b =
Twonil
| Acons a (Twolist a b)
| Bcons b (Twolist a b)
• Prove: P(Twonil)
• Assume: P(xs) Prove: P(Acons x xs)
• Assume: P(ys) Prove: P(Bcons y ys)
5/24/2017
14
Cse536 Functional Programming
Example Proof
• Prove that append2 over Two lists are associative:
P(x) = append2 x (append2 y z) = append2 (append2 x y) z
1 append2 Twonil ys = ys
2 append2 (Acons a xs) ys = Acons a (append2 xs ys)
3 append2 (Bcons b xs) ys = Bcons b (append2 xs ys);
Proof by induction on x
• case 1: x = Twonil
append2 Twonil (append2 y z)
= append2 (append2 Twonil y) z
append2 Twonil (append2 y z)
= (append2 y z) by 1
= (append2 (append2 Twonil y) z) by 1 (backwards)
5/24/2017
15
Cse536 Functional Programming
Case 2
• case 2: x = Acons a xs
» Assume :
append2 xs (append2 y z)=append2 (append2 xs y) z
» Prove:
append2 (Acons a xs) (append2 y z) =
append2 (append2 (Acons a xs) y) z
append2 (Acons a xs) (append2 y z)
by 2
= Acons a (append2 xs (append2 y z))
by hypothesis
= Acons a (append2 (append2 xs y) z)
by 2 backwards
= append2 (Acons a (append2 xs y)) z
by 2 backwards
= append2(append2(Acons a xs) y) z
5/24/2017
16
Cse536 Functional Programming
Case 3
• case 3: x = Bcons b xs
» Assume :
append2 xs (append2 y z)=append2 (append2 xs y) z
» Prove:
append2 (Bcons b xs) (append2 y z) =
append2 (append2 (Bcons b xs) y) z
append2(Bcons b xs)(append2 y z)
by 3
= Bcons b (append2 xs(append2 y z))
by hypothesis
= Bcons b (append2 (append2 xs y) z)
by 3 backwards
= append2 (Bcons b (append2 xs y)) z
by 3 backwards
= append2(append2(Bcons b xs) y) z
5/24/2017
17
Cse536 Functional Programming
Binary Trees
data Bintree a = Lf a
| (Bintree a) :/\: (Bintree a)
• Note all infix constructors start with a colon (:)
1
2
3
4
5
6
7
8
left (a :/\: b) = a
right (a :/\: b) = b
isleaf (Lf _) = True
isleaf (_ :/\: _) = False
leftmostleaf (a :/\: b) = leftmostleaf a
leftmostleaf (Lf x) = Lf x
sumtree (Lf x) = x
sumtree (a :/\: b) = (sumtree a) + (sumtree b)
5/24/2017
18
Cse536 Functional Programming
Sample Tree Functions
flatten :: Bintree a -> [a]
9 flatten (Lf x) = [x]
10 flatten (a :/\: b) = (flatten a) ++ (flatten b)
? flatten (Lf 3 :/\: Lf 5)
[3, 5]
• Some useful Facts
11 sum [] =0
12 sum (x:xs) = x + (sum xs)
13 Lemma: sum(x ++ y) = (sum x) + (sum y)
5/24/2017
19
Cse536 Functional Programming
Proof about Trees
Prove: P(t) == sum(flatten t)=sumtree t
case 1: t = Lf x
sum(flatten (Lf x)) = sumtree (Lf x)
sum(flatten (Lf x))
= sum [x]
by def of flatten 9
= sum (x:[])
by [a] = a:[]
= x + (sum [])
by def of sum 12
= x + 0
by def of sum 11
= x
by property of +
= sumtree (Lf x) by def sumtree 7
5/24/2017
20
Cse536 Functional Programming
Case 2
case 2: t = a :/\: b
Assume: 1) sum(flatten a) = sumtree a
2) sum(flatten b) = sumtree b
Prove: sum(flatten (a :/\: b))=sumtree (a :/\: b)
sum(flatten (a :/\: b))
by def flatten
= sum ((flatten a) ++ (flatten b))
by lemma: 13
= sum(flatten a) + sum(flatten b)
by hypothesis
= (sumtree a) + (sumtree b)
by def of sumtree
= sumtree (a :/\: b)
5/24/2017
21
Related documents