Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.

Slides:



Advertisements
Similar presentations
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Advertisements

Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
Lecture 2 Based on Chapter 1, Weiss. Mathematical Foundation Series and summation: ……. N = N(N+1)/2 (arithmetic series) 1 + r+ r 2 + r 3 +………r.
Chapter Three: Closure Properties for Regular Languages
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Cs776(Prasad)L7fold1 Fold Operations Abstracting Repetitions.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Recursion Breaking down problems into solvable subproblems Chapter 7.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Chapter 9 More About Higher-Order Functions. Currying Recall the function: simple n a b = n * (a+b) Note that: simple n a b is really (((simple n) a)
CS 454 Theory of Computation Sonoma State University, Fall 2011 Instructor: B. (Ravi) Ravikumar Office: 116 I Darwin Hall Original slides by Vahid and.
Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Cse502 Functional Programming 1 6/26/2015 Lecture #11, Nov. 4, 2002 Todays Topics –Using Calculation to prove 2 functions are equal »An example from class.
Chapter 14 Programming With Streams. Streams  A stream is an infinite sequence of values.  We could define a special data type for them: data Stream.
1 Recitation 10. Induction Introduction. Induction is useful in proving properties of recursive algorithms, like their execution times. It is also the.
Advanced Programming Handout 7 More About Higher-Order Functions (SOE Chapter 9)
Chapter 5 Polymorphic and Higher-Order Functions.
Discrete Structures Chapter 5: Sequences, Mathematical Induction, and Recursion 5.2 Mathematical Induction I [Mathematical induction is] the standard proof.
Copyright © Cengage Learning. All rights reserved.
Copyright © 2007 Pearson Education, Inc. Slide 8-1.
Recursive Definitions and Structural Induction
Mathematical Maxims and Minims, 1988
Sequences and Series (T) Students will know the form of an Arithmetic sequence.  Arithmetic Sequence: There exists a common difference (d) between each.
Mathematical Induction. F(1) = 1; F(n+1) = F(n) + (2n+1) for n≥ F(n) n F(n) =n 2 for all n ≥ 1 Prove it!
1 Introduction to Abstract Mathematics Chapter 4: Sequences and Mathematical Induction Instructor: Hayk Melikya 4.1- Sequences. 4.2,
Cs7120 (Prasad)L9-RECUR-IND1 Recursion and Induction.
Type Safety Kangwon National University 임현승 Programming Languages.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
Copyright © Curt Hill Proofs An Introduction.
1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield.
CS5205Haskell1 CS5205: Foundation in Programming Languages Basics of Functional Programming.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Recursion Higher Order Functions CSCE 314 Spring 2016.
Aids to Formulating and Answering Key Questions NameWhen to use u Construction Methodproving “there exists” u Choose Method proving “for every” u Math.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
1 Discrete Mathematical Mathematical Induction ( الاستقراء الرياضي )
Properties of Groups Proposition 1: Let (G,  ) be a group. i.The inverse element of any element of G is unique. Remark: In view of i., we may use the.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Mathematical Induction. The Principle of Mathematical Induction Let S n be a statement involving the positive integer n. If 1.S 1 is true, and 2.the truth.
Chapter 1 Logic and Proof.
Sparkle a functional theorem prover
Functional Programming Lecture 12 - more higher order functions
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Chapter 3 The Real Numbers.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Programming Languages 2nd edition Tucker and Noonan
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
CS 457/557: Functional Languages Folds
CSE 3302 Programming Languages
Announcements Quiz 5 HW6 due October 23
CSCE 314: Programming Languages Dr. Dylan Shell
Induction Chapter
Chapter 11: Further Topics in Algebra
PROGRAMMING IN HASKELL
foldr and foldl applied to addition (using infix notation)
?? Programming with Streams Reading assignment
Presentation transcript:

Chapter 11 Proof by Induction

Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes to the large. E.g. inductive definition of a list: [] is a list; and if xs is a list, then so is x : xs.  Recursion usually starts with the large thing, working its way back to the small. E.g. recursion over lists: Start with non-empty list x : xs; do something with x, then recurse on xs.  It is not surprising then, that proof by induction is closely tied to recursion: it is usually needed when proving properties about recursive functions.

Proof by Induction on Lists To prove property P by induction on the length of a list: 1.Prove P([])-- the base case. 2.Assume P(xs) is true –- the induction hypothesis and prove that P(x:xs) is true -– the induction step

Example 1: A Property About foldr Recall this property about foldr: foldr (:) [] xs = xs A proof by induction: Base case: xs = [] foldr (:) [] []  [] (immediate from the definition of foldr) Induction step: assume foldr (:) [] xs = xs foldr (:) [] (x:xs)  x : foldr (:) [] xs-- unfold foldr  x : xs-- by the induction hypothesis

Example 2: A Property About length  Prove that: length (xs++ys) = length xs + length ys  Question: Over which list (xs or ys) should we perform the induction?  Hint: look at the definitions of length and (++), which recurse over the front of their arguments – thus we should use xs above.

Example 2, cont’d Base case: xs = [] length ([] ++ ys)  length ys  0 + length ys  length [] + length ys Induction step: assume length (xs++ys) = length xs + length ys length ((x:xs) ++ ys)  length (x : (xs ++ ys))  1 + length (xs ++ ys)  1 + (length xs + length ys)  (1 + length xs) + length ys  length (x:xs) + length ys

Proving Function Equivalence  Remember the mantra: “get it right first”.  Later, we can improve our programs for efficiency reasons.  But how do we know that the new program is the same as the old?  Answer: by proving them so.  And in the case of recursive functions, we will need induction.

Example 3: Equivalence of reverse Def’ns Recall these three definitions of reverse: reverse1 [] = [] reverse1 (x:xs) = reverse1 xs ++ [x] reverse2 xs = rev [] xs where rev acc [] = acc rev acc (x:xs) = rev (x:acc) xs reverse3 = foldl (flip (:)) [] reverse1 is the obvious, but inefficient version. reverse2 is the recursive, efficient version. reverse3 is the non-recursive, efficient version.

Example 3, cont’d Let’s prove that reverse1 = reverse3.  Base case: xs = [] reverse1 []  []-- unfold reverse1  foldl (flip (:)) [] []-- fold foldl  reverse3 []-- fold reverse3  Induction step: assume reverse1 xs = reverse3 xs reverse1 (x:xs)  reverse1 xs ++ [x]-- unfold reverse1  reverse3 xs ++ [x]-- induction hypothesis  foldl (flip (:)) [] xs ++ [x]-- unfold reverse3  foldl (flip (:)) [] (x:xs)-- LEMMA!!!  reverse3 (x:xs)-- fold reverse3  The lemma states: foldl (flip (:)) [] xs ++ [x] = foldl (flip (:)) [] (x:xs) How do we prove this?

Generalizing Lemmas  The lemma: foldl (flip (:)) [] xs ++ [x] = foldl (flip (:)) [] (x:xs) is actually hard to prove directly.  It ‘s easier to prove this generalization: foldl (flip (:)) ys xs ++ [y] = foldl (flip (:)) (ys++[y]) xs from which the first lemma is easy to prove.  Knowing when to introduce a lemma, and when to generalize it, it is a skill in proving theorems in any context. (See text for proofs of lemmas.)

Laws of Lists  There are many useful properties of functions such as (++), map, foldl, and foldr on lists.  These properties can be used as “lemmas” in proving other properties of larger programs that use these functions.  Some of them depend on function “strictness”: A function f is strict if f _|_ = _|_.

Laws of Map map (\x->x) = \x->x map (f. g) = map f. map g map f. tail = tail. map f map f. reverse = reverse. map f map f. concat = concat. map (map f) map f (xs ++ ys) = map f xs ++ map f ys For all strict f: f. head = head. map f

Laws of fold If op is associative, e `op` x = x, and x `op` e = x, then for all finite xs: foldr op e xs = foldl op e xs If the following are true: x `op1` (y `op2` z) = (x `op1` y) `op2` z x `op1` e = e `op2` x then for all finite xs: foldr op1 e xs = foldl op2 e xs For all finite xs: foldr op e xs = foldl (flip op) e (reverse xs)

Induction on Other Data Types  Proof by induction is not limited to lists. Indeed, it is usually taught on natural numbers first.  Example: Exponentiation, defined by: (^) :: Integer -> Integer -> Integer x^0 = 1 x^n = x * x^(n-1)  Suppose we wish to prove that, for all natural numbers m and n: x^(n+m) = x^n * x^m

Example 4: A Property About Exponentiation As before, we proceed by induction.  Base case: n = 0 x^(0+m)  x^m  1 * (x^m)  x^0 * x^m  Induction step: assume x^(n+m) = x^n * x^m x^((n+1)+m)  x * x^(n+m)  x * (x^n * x^m)  (x * x^n) * x^m  x^(n+1) * x^m

Induction Over User-Defined Data Types  Recall the tree data type: data Tree a = Leaf a | Branch (Tree a) (Tree a)  And define these functions on it: flatten :: Tree a -> [a] flatten (Leaf x) = [x] flatten (Branch t1 t2) = flatten t1 ++ flatten t2 sumTree :: Tree Int -> Int sumTree (Leaf x) = x sumTree (Branch t1 t2) = sumTree t1 + sumTree t2  Further, assume that: sum = foldl (+) 0  Lemma: sum (xs++ys) = sum xs + sum ys

Example 5: A Property About Trees Theorem: sum. flatten = sumTree Proof by induction:  Base case: t = Leaf x sum (flatten (Leaf x))  sum [x]-- unfold flatten  foldl (+) 0 (x:[])-- syntax, and unfold sum  x + (foldl (+) 0 [])-- unfold foldl  x unfold foldl  x-- arithmetic  sumTree (Leaf x)-- fold sumTree

Example 5, cont’d  Induction step: assume that sum (flatten t1) = sumTree t1 sum (flatten t2) = sumTree t2 sum (flatten (Branch t1 t2))  sum (flatten t1 ++ flatten t2)-- unfold flatten  sum (flatten t1) + sum (flatten t2)-- lemma  sumTree t1 + sumTree t2-- induction hyp.  sumTree (Branch t1 t2)-- fold sumTree