Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.

Similar presentations


Presentation on theme: "Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments."— Presentation transcript:

1 Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments –Functor –Monad –MonadPlus and MonadZero Reading Assignment –Read Chapter 18 - Higher Order types Last homework assigned today. See webpage. Due Wednesday Dec. 8, 2004. Final Exam scheduled for Wednesday Dec. 8, 2004

2 Cse536 Functional Programming 2 6/23/2015 Higher Order types Type constructors are higher order since they take types as input and return types as output. Some type constructors (and also some class definitions) are even higher order, since they take type constructors as arguments. Haskell’s Kind system –A Kind is haskell’s way of “typing” types –Ordinary types have kind * »Int :: * »[ String ] :: * –Type constructors have kind * -> * »Tree :: * -> * »[] :: * -> * »(,) :: * -> * -> *

3 Cse536 Functional Programming 3 6/23/2015 The Functor Class class Functor f where fmap :: (a -> b) -> (f a -> f b) Note how the class Functor requires a type constructor of kind * -> * as an argument. The method fmap abstracts the operation of applying a function on every parametric Argument. a Type T a = x (f x) fmap f

4 Cse536 Functional Programming 4 6/23/2015 Notes Special syntax for built in type constructors (->) :: * -> * -> * [] :: * -> * (,) :: * -> * -> * (,,) :: * -> * -> * -> * Most class definitions have some implicit laws that all instances should obey. The laws for Functor are: fmap id = id fmap (f. g) = fmap f. fmap g

5 Cse536 Functional Programming 5 6/23/2015 Instances of class functor data Tree a = Leaf a | Branch (Tree a) (Tree a) instance Functor Tree where fmap f (Leaf x) = Leaf (f x) fmap f (Branch x y) = Branch (fmap f x) (fmap f y) instance Functor ((,) c) where fmap f (x,y) = (x, f y)

6 Cse536 Functional Programming 6 6/23/2015 More Instances instance Functor [] where fmap f [] = [] fmap f (x:xs) = f x : fmap f xs instance Functor Maybe where fmap f Nothing = Nothing fmap f (Just x) = Just (f x)

7 Cse536 Functional Programming 7 6/23/2015 Other uses of Higher order T.C.’s data Tree t a = Tip a | Node (t (Tree t a)) t1 = Node [Tip 3, Tip 0] Main> :t t1 t1 :: Tree [] Int data Bin x = Two x x t2 = Node (Two(Tip 5) (Tip 21)) Main> :t t2 t2 :: Tree Bin Int

8 Cse536 Functional Programming 8 6/23/2015 What is the kind of Tree? Tree is a binary type constructor –It’s kind will be something like: ? -> ? -> * The first argument to Tree is itself a type constructor, the second is just an ordinary type. –Tree :: (* -> *)-> * -> *

9 Cse536 Functional Programming 9 6/23/2015 The Monad Class class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a p >> q = p >>= \ _ -> q fail s = error s We pronounce >>= as “bind” and >> as “sequence” Note m is a type constructor

10 Cse536 Functional Programming 10 6/23/2015 Default methods Note that Monad has two default definitions p >> q = p >>= \ _ -> q fail s = error s These are the definitions that are usually correct, so when making an instance of class Monad, only two definitions (>>= and return) are usually given.

11 Cse536 Functional Programming 11 6/23/2015 Do notation shorthand The do notation is shorthand for the infix operator >>= do e => e do {e 1 ;e 2 ;…;e n } => e 1 >> do {e 2 ;…;e n } do {x <- e 1 ;e 2 ;…;e n } where x is a variable => e 1 >>= \x -> do {e 2 ;…;e n } do { pat <- e1 ; e2 ; … ; en } => let ok pat = do { e2; … ; en } in e1 >>= ok

12 Cse536 Functional Programming 12 6/23/2015 Monad’s and Actions We’ve always used the do notation to indicate an impure computation that performs an actions and then returns a value. We can use monads to “invent” our own kinds of actions. To define a new monad we need to supply a monad instance declaration. Example: The action is potential failure instance Monad Maybe where Just x >>= k = k x Nothing >>= k = Nothing return = Just

13 Cse536 Functional Programming 13 6/23/2015 Example find :: Eq a => a -> [(a,b)] -> Maybe b find x [] = Nothing find x ((y,a):ys) = if x == y then Just a else find x ys test a c x = do { b <- find a x; return (c+b) } What is the type of test ? What does it return if the find fails?

14 Cse536 Functional Programming 14 6/23/2015 Monad Laws If you define your own monad then it needs to meet the following laws: return a >>= k = k a m >>= return = m m >>= (\x -> k x >>= h) = (m >>= k) >>= h These laws specify the precise “order” that actions are performed. In do notation they appear as: do x k a do x m do x do y <- (do x <- m ; k x) ; h y do m1 ; m2 ; m3 ==> do (do m1 ; m2) ; m3

15 Cse536 Functional Programming 15 6/23/2015 Using the laws do { k <- getKey w; return k } => getKey w do k <- getKey w n <- changeKey k respond n => let keyStuff = do k <- getKey w changeKey k in do n <- keyStuff respond n

16 Cse536 Functional Programming 16 6/23/2015 More Monad Instances instance Monad [ ] where (x:xs) >>= f = f x ++ (xs >>= f) [] >>= f = [] return x = [x] What kind of action does the type constructor [] represent?

17 Cse536 Functional Programming 17 6/23/2015 Generic Monad functions sequence :: Monad m => [m a] -> m [a] sequence = foldr mcons (return []) where mcons p q = do x <- p xs <- q return (x:xs) sequence_ :: Monad m => [m a] -> m () sequence_ = foldr (>>) (return ()) mapM :: Monad m => (a -> m b) -> [a] -> m [b] mapM f as = sequence (map f as) mapM_ :: Monad m => (a -> m b) -> [a] -> m () mapM_ f as = sequence_ (map f as) (= (a -> m b) -> m a -> m b f = >= f

18 Cse536 Functional Programming 18 6/23/2015 The MonadPlus Class Some monads support recoverable failure class Monad m => MonadPlus m where mzero :: m a -- failure mplus :: m a -> m a -> m a -- recovery Laws: m >>= (\x -> mzero) = mzero mzero >>= m = mzero mzero `mplus` m = m m `mplus` mzero = m Think of mzero as 0, mplus as (+), and (>>=) as (*)

19 Cse536 Functional Programming 19 6/23/2015 MonadPlus Instances instance MonadPlus [] where mzero = [] [] `mplus` ys = ys (x:xs)`mplus` ys = x : (xs `mplus` ys) instance MonadPlus Maybe where mzero = Nothing Nothing `mplus` ys = ys xs `mplus` ys = xs Do the laws hold?


Download ppt "Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments."

Similar presentations


Ads by Google