Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15 CS 1813 – Discrete Mathematics

Similar presentations


Presentation on theme: "Lecture 15 CS 1813 – Discrete Mathematics"— Presentation transcript:

1 Lecture 15 CS 1813 – Discrete Mathematics
Lecture 16 - CS 1813 Discrete Math, University of Oklahoma 9/20/2018 Lecture 15 CS 1813 – Discrete Mathematics Patterns of Computation CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

2 Type Specifications Every Haskell Entity Has a Type
x, y, z :: Integer this is a type spec, not a definition x, y, and z have type Integer Examples of equations defining x, y, and z x = this is a definition: x means 1 y = another definition: y means 10 z = sum[49, 12, 22, -19] -- the variable z means 64 xs, ys :: [Integer] xs and ys are sequences with elements of type Integer Examples of equations defining xs and ys xs = [29, -2, x, 3*y, 0, x+z] Same effect as xs = [29, -2, 1, 30, 0, 65] ys = [1 .. 4] ++ xs Same effect as ys = [1, 2, 3,4] ++ [ 29, -2, 1, 30, 0, 65] Which has same effect as ys = [1, 2, 3,4, 29, -2, 1, 30, 0, 65] CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

3 More Type Specifications Functions Have Types, Too
or :: [Bool] -> Bool this is a type spec, not a definition or is a function (any type with an arrow in it is a function type) One argument (number of arguments equals number of arrow) Argument is a sequence with elements of type Bool Delivers a value of type Bool (\/), (/\) :: Bool -> Bool -> Bool -- this is a type spec (\/) and (/\) are functions with two arguments Both arguments have type Bool Value delivered has type Bool Formulas can use these functions with prefix or infix syntax Formula using prefix syntax: (\/) False True Formula using infix syntax : False \/ True Parentheses around an infix operator converts to prefix syntax Back-quotes around a prefix function converts to infix syntax (Assume A) `OrI` (A \/ B) CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

4 Generic Type Specifications Functions Can Span Many Types
(++) :: [e] -> [e] -> [e] -- this is a type spec, not a definition (++) is a function with two arguments, both of which are sequences [e] specifies a sequence with elements of type e e is a type variable This type specification places no constraints on e — it can be any type Both argument sequences must have the same type of elements Formulas using ++ [True, False, True] ++ [False] e is the type Bool in this case [“Gödel”] ++ [“Backus”, “Dijkstra”] e is the type String in this case [4, 2, -12, 9] ++ [6, 8] e is a numeric type in this case CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

5 Generic Types Can Be Constrained
sum :: Num x => [x] -> x -- this is a type spec Double-shafted arrow delimits type constraint Classes are sets of types Num is the class of numeric types Num x — specification constraining the type x to be in the class Num sum is a function with one argument Argument is a sequence whose elements are numbers of type x, where x is a type in the class Num Value delivered is a number of type x Examples sum[49, 12, 22, -19] -- delivers 64 sum[3.1416, , sqrt 2] -- delivers Both context and syntax determine type Syntax eliminates type Integer in second example CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

6 Multiple Constraints in Type Specs
powerSet :: (Eq e, Show e) => Set e -> Set(Set e) Type e must be both in class Eq and in class Show OK to use == to compare values from type of class Eq Interpreter can display values from type of class Show powerSet is a function with one argument Argument has type Set e Set e — sets with elements of type e The type e can be any type within the constraints Value delivered has type Set(Set e) Set(Set e) — sets with elements of type Set e The type-variable e denotes the same type in the type-spec of the argument that it denotes in the type-spec of the value delivered CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

7 Type Specs with More Than One Variable
crossProduct :: (Eq a, Show a, Eq b, Show b) => Set a -> Set b -> Set(a, b) Two types in involved First type denoted by type variable a Second type denoted by type variable b These types may be different Both types are constrained Type a must be both in class Eq and in class Show Type b has the same constraints crossProduct is a function with two arguments First argument has type Set a Set a — sets with elements of type a Second argument has type Set b (set, elements of type b) Value delivered has type Set(a, b) Set (a, b) — sets with elements that are tuples of type (a, b) CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

8 Combinators Arguments Can Be Functions, Too
foldr :: (a -> b -> b) -> b -> [a] -> b foldr combinator builds a formula: foldr () z [x1, x2, x3, x4, x5 ] = x1  ( x2  ( x3  ( x4  (x5  z)))) foldr has three arguments First argument is a function with two arguments Second argument has same type that first argument delivers Third argument is a sequence with elements of the same type that the first argument (a function, remember) requires in its first argument foldr (+) 0 [49, 12, 22, -19] = 49+(12+(22+((-19)+0))) = (+) :: Num a => a -> a -> a foldr (++) “” [“Gödel”, “Backus”, “Dijkstra”] = “Gödel” ++ (“Backus” ++ (“Dijkstra” ++ “”)) = “GödelBackusDijkstra” (++) :: [a] -> [a] -> [a] Note: “Backus” :: [Char] CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

9 What’s foldr Got to Do With It?
foldr () z (x: xs) = x  foldr () z xs (foldr).: foldr () z [ ] = z (foldr).[] sum(x: xs) = x + sum xs (sum).: sum[ ] = (sum).[] Theorem: sum = foldr (+) 0 n. length xs = n  sum xs = foldr (+) 0 xs Proof P(n)  length xs = n  sum xs = foldr (+) 0 xs Base case: P(0) sum xs = sum [ ] len 0  [] = (sum).[] = foldr (+) 0 [ ] = foldr (+) 0 xs (foldr):[] Inductive case: P(n)  P(n+1) sum(xs) = sum(x: ys) (:len corollary) = x + sum ys (sum).: = x + foldr (+) 0 ys P(n) = foldr (+) 0 (x: ys) = foldr (+) 0 xs (foldr).: P(0) P(n)P(n+1) Ind Hyp CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page qed

10 There’s a Nugget in There Somewhere
foldr :: (a -> b -> b) -> b -> [a] -> b foldr () z [x1, x2, x3, x4, x5 ] = x1  ( x2  ( x3  ( x4  (x5  z)))) () :: a -> b -> b z :: b [x1, x2, x3, x4, x5 ] :: [a] (foldr () z [x1, x2, …, xn ] ) :: b sum = foldr (+) 0 or = foldr (\/) False and = foldr (/\) True length = foldr oneMore where oneMore x n = 1 + n oneMore :: a -> Int -> Int xs ++ ys = foldr (:) ys xs (:) :: a -> [a] -> [a] all of these have the same pattern of computation: the foldr pattern foldr (:) [y1, y2 ] [x1, x2, x3 ] = x1: x2: x3: [y1, y2 ] x1: (x2: (x3: [y1, y2 ])) x1 x2 x3 [y1, y2 ] = [x1, x2, x3, y1, y2 ] concat = foldr (++) [ ] (++) :: [a] -> [a] -> [a] = [x1, x2, x3 ] ++ ([y1, y2 ] ++ [z1, z2, z3, z4]) = [x1, x2, x3, y1, y2 , z1, z2, z3, z4] CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

11 CS 1813 Discrete Mathematics, Univ Oklahoma
length = foldr oneMore length(x: xs) = 1 + length xs (length).: length[ ] = (length).[] Theorem: n. length xs = n  length xs = foldr oneMore 0 xs Proof P(n)  length xs = n  length xs = foldr oneMore 0 xs Note: The statement of the theorem is the formula n. P(n) P(0): length[ ] = (length).[] = foldr oneMore 0 [ ] (foldr).[] P(n+1): length xs = = length(x: ys) = 1 + length ys :len corollary = 1 + foldr oneMore 0 ys P(n) = x `oneMore` (foldr oneMore 0 ys) (def oneMore) = foldr oneMore 0 (x:ys) (foldr).: = foldr oneMore 0 xs xs = (x: ys) CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page qed

12 Doing the Same Thing to All of Them the map combinator
Computational pattern the map pattern map f [x1, x2, … xn] = [f x1, f x2, … f xn] Examples map sqrt [4, 16, 9, 25, 196] = [2, 4, 3, 5, 14] map sum [[4, 7, 9], [-4, 12], [22, 11, -33, 6]] = [20, 8, 6] map and [[False, True], [True,True,True]] = [False, True] map :: (a -> b) -> [a] -> [b] map f [ ] = [ ] (map).[] map f (x : xs) = (f x) : (map f xs ) (map).: CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

13 Pairing Things Up the zipWith combinator
Computational pattern the zipWith pattern zipWith b [x1, x2, … xn] [y1, y2, … yn] = [b x1 y1, b x2 y2, … b xn yn] Note: extra elements in either sequence are dropped Examples zipWith (+) [4, 16, 9] [3, 7, 5] = [4+3, 16+7, 9+5] = [7, 23, 14] zipWith (++) [“Terry”, “Pat”, “Carol”] [“Ray”, “Li”, “Lopez”] = [“TerryRay”, “PatLi”, “CarolLopez”] mysteryFunction xs ys = sum(zipWith () xs ys) What function is this? (comes from vector/matrix algebra) zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] zipWith b [ ] ys = [ ] (zipW).[]-1 zipWith b xs [ ] = [ ] (zipW).[]-2 zipWith b (x:xs) (y:ys) = (b x y): (zipWith b xs ys) (zipW).: CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

14 CS 1813 Discrete Mathematics, Univ Oklahoma
shuffle shuffle :: [a] -> [a] -> [a] Examples shuffle [1, 2, 3, 4, 5] [6, 7, 8, 9, 10] = [1, 6, 2, 7, 3, 8, 4, 9, 5, 10] shuffle [1, 2, 3, 4, 5] [6, 7, 8] = [1, 6, 2, 7, 3, 8, 4, 5] shuffle [1, 2, 3, 4] [6, 7, 8, 9, 10] = [1, 6, 2, 7, 3, 8, 4, 9, 10] Pattern of computation shuffle [x1, x2, … xn] [y1, y2, … yn] = [x1, y1, x2 , y2, … xn , yn] Note: extra elements in either sequence appended to the end Definition shuffle (x: xs) (y: ys) = [x, y] ++ shuffle xs ys shuffle [ ] ys = ys shuffle xs [ ] = xs Define shuffle using the zipWith pattern of computation pairUp x y = [x, y] shuffle xs ys = concat(zipWith pairUp xs ys) if (length xs) = (length ys) CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page

15 Lecture 16 - CS 1813 Discrete Math, University of Oklahoma
9/20/2018 End of Lecture CS Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page


Download ppt "Lecture 15 CS 1813 – Discrete Mathematics"

Similar presentations


Ads by Google