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

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
String is a synonym for the type [Char].
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
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.
Grab Bag of Interesting Stuff. Topics Higher kinded types Files and handles IOError Arrays.
Cse536 Functional Programming 1 6/12/2015 Lecture #11, Nov. 01, 2004 Special Guest lecture by Tom Harke Today’s Topics –The Haskell Class system –Instance.
Tim Sheard Oregon Graduate Institute Lecture 6: Monads and Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
Introducing Monads Lecture 3, Designing and Using Combinators John Hughes.
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 5 Polymorphic and Higher-Order Functions.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
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,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 19: Functions, Types and Data Structures in Haskell COMP 144 Programming Language.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
0 PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Workshop: Towards Highly Portable Software Jakarta, 21 – 23 January 2003 Diselenggarakan oleh Universitas IndonesiaUniversitas Indonesia Part 1 : Programming.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Haskell Chapter 8. Input and Output  What are I/O actions?  How do I/O actions enable us to do I/O?  When are I/O actions actually performed?
Chapter 9: Functional Programming in a Typed Language.
Advanced Functional Programming Tim Sheard 1 Lecture 14 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
Advanced Programming Andrew Black and Tim Sheard Lecture 11 Parsing Combinators.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Advanced Functional Programming Tim Sheard 1 Lecture 6 Functional Programming Tim Sheard & Mark Jones Monads & Interpreters.
0 Functors in Haskell Adapted from material by Miran Lipovaca.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Grab Bag of Interesting Stuff. Higher Order types Type constructors are higher order since they take types as input and return types as output. Some type.
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,
Advanced Functional Programming Tim Sheard 1 Lecture 6 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
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.
String is a synonym for the type [Char].
Types CSCE 314 Spring 2016.
Functional Programming Lecture 12 - more higher order functions
PROGRAMMING IN HASKELL
Tim Sheard Oregon Graduate Institute
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Advanced Functional Programming
PROGRAMMING IN HASKELL
CS 457/557: Functional Languages Folds
PROGRAMMING IN HASKELL
Announcements Quiz 5 HW6 due October 23
Grab Bag of Interesting Stuff
PROGRAMMING IN HASKELL
Programming Languages
CSEP505: Programming Languages Lecture 9: Haskell Typeclasses and Monads; Dan Grossman Autumn 2016.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Advanced Functional Programming
Presentation transcript:

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, Final Exam scheduled for Wednesday Dec. 8, 2004

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 :: * -> * »[] :: * -> * »(,) :: * -> * -> *

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

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

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)

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)

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

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 :: (* -> *)-> * -> *

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

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.

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

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

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?

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

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

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?

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

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 (*)

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?