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.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

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.
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.
Functional Verification III Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture Notes 23.
1 In this lecture  Number Theory ● Rational numbers ● Divisibility  Proofs ● Direct proofs (cont.) ● Common mistakes in proofs ● Disproof by counterexample.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
CSCI 2670 Introduction to Theory of Computing September 13, 2005.
What is a Parser? A parser is a program that analyses a piece of text to determine its syntactic structure  3 means 23+4.
Cse536 Functional Programming 1 6/10/2015 Lecture 10, Oct. 29, 2004 Special Rescheduled Friday Lecture Today’s Topics – Pictures – Low level graphics bit-maps:
Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
Announcements: Homework 1 returned. Comments from Kevin? Homework 1 returned. Comments from Kevin? Matlab: tutorial available at
Chapter 10 Drawing Regions. Pictures  Drawing Pictures Pictures are composed of Regions o Regions are composed of shapes Pictures add Color data Picture.
Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.
Cse536 Functional Programming 1 6/24/2015 Lecture #4, Oct 6, 2004 Reading Assignments –Begin Reading chapter 3 (Simple Graphics) of the Text Homework Assignment.
Chapter 5 Polymorphic and Higher-Order Functions.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Creating Functions Functional Programming. The function calculator Functional programming is all about using functions Functions are first class – Take.
1 Strong Mathematical Induction. Principle of Strong Mathematical Induction Let P(n) be a predicate defined for integers n; a and b be fixed integers.
1 Mathematical Induction. 2 Mathematical Induction: Example  Show that any postage of ≥ 8¢ can be obtained using 3¢ and 5¢ stamps.  First check for.
Copyright © 2007 Pearson Education, Inc. Slide 8-1.
Programovací jazyky F# a OCaml Chapter 5. Hiding recursion using function-as-values.
Sequences and Series (T) Students will know the form of an Arithmetic sequence.  Arithmetic Sequence: There exists a common difference (d) between each.
Reading and Writing Mathematical Proofs
CSE373: Data Structures and Algorithms Lecture 2b: Proof by Induction and Powers of Two Nicki Dell Spring 2014.
CSE 311 Foundations of Computing I Lecture 8 Proofs and Set Theory Spring
9.4 Mathematical Induction
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.
CSE 311 Foundations of Computing I Lecture 9 Proofs and Set Theory Autumn 2012 CSE
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
CSCI 3130: Formal languages and automata theory Tutorial 3 Chin.
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.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
CSE 341 Lecture 8 curried functions Ullman 5.5 slides created by Marty Stepp
1 Discrete Mathematical Mathematical Induction ( الاستقراء الرياضي )
CSE 311 Foundations of Computing I Lecture 11 Modular Exponentiation and Primes Autumn 2011 CSE 3111.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Functional Programming Language: Haskell (cont’d) Lecture 20 – Functional Programming, Spring 2008.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
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.
(Proof By) Induction Recursion
What is a Parser? A parser is a program that analyses a piece of text to determine its syntactic structure  3 means 23+4.
CSE322 PUMPING LEMMA FOR REGULAR SETS AND ITS APPLICATIONS
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
PROGRAMMING IN HASKELL
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 311: Foundations of Computing
Lecture 15 CS 1813 – Discrete Mathematics
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 311: Foundations of Computing
Higher Order Functions
CS 457/557: Functional Languages Folds
PROGRAMMING IN HASKELL
Chapter 11: Further Topics in Algebra
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

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 drawPic –Laws about Monads –Laws about sequence –Using Calculation to improve functions »An example from the first Homework assignment Read Chapter 11 - Proofs by induction Read Chapter 12 – Qualified types

Cse502 Functional Programming 2 6/26/2015 Proof from Class drawPic :: Window -> Picture -> IO () drawPic w (Region c r) = drawReg w (c,r) drawPic w (p1 `Over` p2) = do { drawPic w p2 ; drawPic w p1 } drawPic w EmptyPic = return () flattenPict :: Picture -> [(Color,Region)] flattenPict EmptyPic = [] flattenPict (Region c r) = [(c,r)] flattenPict (p1 `Over` p2) = flattenPict p2 ++ flattenPict p1 draw2 w p = sequence (map (drawReg w) (flattenPict p))

Cse502 Functional Programming 3 6/26/2015 We would like to Prove drawPict w p = draw2 w p We can draw a picture OR We can flatten a picture, then draw each of the regions in the order they appear in the flattened list. This is important, because for some applications we want to maintain the list of regions for other reasons. Recall the structure of a picture: data Picture = Region Color Region | Picture `Over` Picture | EmptyPic

Cse502 Functional Programming 4 6/26/2015 Structure of the proof To Prove : drawPic w p = draw2 w p Prove 3 cases –case 1) p = EmptyPic prove: drawPic w EmptyPic = draw2 w EmptyPic –case 2) p = (Region c r) prove: drawPic w (Region c r) = draw2 w (Region c r) –case 3) p = (p1 `Over` p2) assume: a) drawPic w p1 = draw2 w p1 b) drawPic w p2 = draw2 w p2 prove: drawPic w (p1 `Over` p2) = draw2 w (p1 `Over` p2)

Cse502 Functional Programming 5 6/26/2015 First a few laws Laws about do and return (we saw these when we first studied monads) Monad1: do { a <- x; return a } = x Monad2: do { x <- return y; e } = e[y/x] Monad3: do { a <- do { b <- e; c}; d } = do { b <- e; a <- c; d } provided b not in FreeVars(d) Monad4: do { x<- a; b} = do {a; b} provided x not in FreeVars(b)

Cse502 Functional Programming 6 6/26/2015 Draw2 uses sequence Definition sequence :: [ IO () ] -> IO () S1: sequence [] = return () S2: sequence (x:xs) = do { x; sequence xs } Lemmas Lemma 1: sequence [ x ] = x Lemma 2: sequence(a ++ b) = do {sequence a; sequence b} Lemma 3: map f x ++ map f y = map f (x ++ y)

Cse502 Functional Programming 7 6/26/2015 Proof Lemma1 Lemma 1: sequence [ x ] = x sequence [ x ] = By def of [ x ] syntax sequence (x : []) = by def of sequence rule s2 do { x; sequence [] } = by def of sequence rule S1 do { x; return () } = by Monad4 backwards do { a <- x; return () } = Since a has type (), and () is unique do { a <- x; return a } = by Monad1 x

Cse502 Functional Programming 8 6/26/2015 Lemma 2: sequence(a ++ b) = do {sequence a; sequence b} P{a} = sequence(a ++ b) = do {sequence a; sequence b} Proof by induction over the structure of a Base Case - Prove: sequence([] ++ b) = do {sequence []; sequence b} sequence([] ++ b) = by def of ++ sequence(b) = by Monad2 (backwards) do { x <- return (); sequence b } = by def sequence do { x <- sequence []; sequence b } = Monad4 do {sequence []; sequence b }

Cse502 Functional Programming 9 6/26/2015 Induction Step Assume: sequence(xs ++ b) = do {sequence xs; sequence b} Prove: sequence((x:xs) ++ b) = do {sequence (x:xs); sequence b} sequence((x:xs) ++ b) = by def of ++ sequence( x : (xs ++ b)) = by def of sequence do { x; sequence (xs ++ b) } = by IH do { x; do {sequence xs; sequence b} } = by Monad3 do { do {x; sequence xs}; sequence b } = def of sequence do { sequence (x:xs); sequence b }

Cse502 Functional Programming 10 6/26/2015 Back to the main proof To Prove : drawPic w p = draw2 w p Prove 3 cases –case 1) p = EmptyPic prove: drawPic w EmptyPic = draw2 w EmptyPic –case 2) p = (Region c r) prove: drawPic w (Region c r) = draw2 w (Region c r) –case 3) p = (p1 `Over` p2) assume: a) drawPic w p1 = draw2 w p1 b) drawPic w p2 = draw2 w p2 prove: drawPic w (p1 `Over` p2) = draw2 w (p1 `Over` p2)

Cse502 Functional Programming 11 6/26/2015 case 1. p = EmptyPic drawPic w EmptyPic = draw2 w EmptyPic drawPic w EmptyPic = by def drawPict return () = by def sequence sequence [] = by def map sequence (map (drawReg w) []) = by def flattenPict sequence (map (drawReg w) (flattenPict EmptyPic)) = by def draw2 draw2 w EmptyPic

Cse502 Functional Programming 12 6/26/2015 case 2. p = (Region c r) drawPic w (Region c r) = draw2 w (Region c r) drawPic w (Region c r) = by def drawPic drawReg w (c,r) = by Lemma1 sequence [ drawReg w (c,r) ] by def of map sequence (map (drawRegion w) [(c,r)]) by def flattenPict sequence (map (drawRegion w) (flattenPict (Region c r))) by def draw2 draw2 w (Region c r)

Cse502 Functional Programming 13 6/26/2015 case 3. p = (a `Over` b) assume: a) drawPic w a = draw2 w a b) drawPic w b = draw2 w b prove: drawPic w (a `Over` b) = draw2 w (a `Over` b) drawPic w (a `Over` b) = by def drawPic do { drawPic w b; drawPic w a } = by IH do { draw2 w b; draw2 w a } = by def draw2 do { sequence (map (drawReg w) (flattenPict b)); sequence (map (drawReg w) (flattenPict a)) } =

Cse502 Functional Programming 14 6/26/2015 Case 3. continued = by Lemma2 sequence ( (map (drawReg w) (flattenPict b)) ++ (map (drawReg w) (flattenPict a)) ) = by Lemma3 sequence (map (drawReg w) (flattenPict b ++ flattenPict a)) = def of flattenPict sequence (map (drawReg w) (flattenPict (a `Over` b))) = by def of draw2 draw2 (a `Over` b)

Cse502 Functional Programming 15 6/26/2015 Ex2. Recall String to Integer A function which converts a string of digits into an Int. Uses the function which ord which takes a Char as input and returns its ascii code ord ‘1’ --> 49 Follows the "pipeline" analysis "167" --> ['1','6','7'] --> [49,54,55] --> [1,6,7] --> [(1,100),(6,10),(7,1)] --> [100, 60, 7] --> 167

Cse502 Functional Programming 16 6/26/2015 String to Int (cont) Then str2int is an easy composition str2int :: String -> Int str2int = sum. map (uncurry (*)). explist. map (\ z -> z -(ord '0')). map ord The Key is the function explist explist [5,3,4] --> [(5,100),(3,10),(4,1)]

Cse502 Functional Programming 17 6/26/2015 Key Function Explist Useful intermediates reverse [1,10,100] [1,10,100] --> [100,10,1] zip [3,4] [100, 10, 1] --> [(3,100), (4,10)] Definition explist zs = zip zs [ power 10 x | x <- reverse [0.. n-1] ] where n = length zs

Cse502 Functional Programming 18 6/26/2015 Another explist (we use this one in the improvement) explist = fst. foldr g ([],1) where z `g` (zs,n) = ((z,n) : zs, n * 10) Suppose we start with [5,3,2] Folding g leaves –5 `g` (3 `g` (2 `g` ([],1))) –5 `g` (3 `g` ((2,1):[],10)) –5 `g` (3 `g` ([(2,1)],10)) –5 `g` ((3,10):[(2,1)],100) –5 `g` ([(3,10),(2,1)],100) –([(5,100),(3,10),(2,1)],1000)

Cse502 Functional Programming 19 6/26/2015 Using theory to improve functions Given the definitions: sum: sum = foldr (+) 0 uncurry: uncurry f (x,y) = f x y and the laws: map1: map f. map g = map (f. g) lambda1: (\ x -> e). f = (\ x -> e[(f x)/x]) lambda2: f. (\ x -> e) = (\ x -> f e) mapfoldr: foldr c e. map f = (let d x y = c (f x) y in foldr d e) Improve the definition of str2int

Cse502 Functional Programming 20 6/26/2015 Improvement 1 str2int = sum. map (uncurry (*)). explist. map (\ z -> z -(ord '0')). map ord »by map1 str2int = sum. map (uncurry (*)). explist. map ((\z -> z -(ord '0')). ord)

Cse502 Functional Programming 21 6/26/2015 Improvement 2 str2int = sum. map (uncurry (*)). explist. map ((\z -> z -(ord '0')). ord) »by lambda1 str2int = sum. map (uncurry (*)). explist. map (\ z -> ord z -(ord '0'))

Cse502 Functional Programming 22 6/26/2015 Improvement 3 » by definition of sum str2int = foldr (+) 0. map (uncurry (*)). explist. map (\ z -> ord z -(ord '0')) »by mapfoldr str2int = (let d x y = (+)(uncurry (*) x) y in foldr d 0). explist. map (\ z -> ord z -(ord '0'))

Cse502 Functional Programming 23 6/26/2015 Improvement 4 str2int = (let d x y = (+)(uncurry (*) x) y in foldr d 0). explist. map (\ z -> ord z -(ord '0')) »by operator use : (+) x y = x + y str2int = (let d x y = (uncurry (*) x) + y in foldr d 0). explist. map (\ z -> ord z -(ord '0'))

Cse502 Functional Programming 24 6/26/2015 Improvement 5 » patterns and definition of uncurry str2int = (let d (a,b) y = a*b + y in foldr d 0). explist. map (\ z -> ord z -(ord '0')) »rearranging str2int = (foldr d 0). explist. map f where d (a,b) y = a*b + y f z = ord z -(ord '0')

Cse502 Functional Programming 25 6/26/2015 Improvement 6 »Using def of explist explist = fst. foldr g ([],1) where z `g` (zs,n) = ((z,n) : zs, n * 10)

Cse502 Functional Programming 26 6/26/2015 Improvement 7 str2int = (foldr d 0). fst. (foldr g ([],1)). map f where d (a,b) y = a*b + y f z = ord z -(ord '0') z `g` (zs,n) = ((z,n) : zs, n * 10) »Using mapfoldr again str2int = (foldr d 0).fst.(foldr d2 ([],1)) where d (a,b) y = a*b + y d2 z (zs,n) =((ord z - ord ‘0’,n)::zs, n*10)