Lecture - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture

Description:

Type constructors are higher order since they take types as input and return ... We pronounce = as 'bind' and as 'sequence' Note m is a. type constructor ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 20
Provided by: timsh3
Learn more at: http://web.cecs.pdx.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture


1
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
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.
  • Haskells Kind system
  • A Kind is haskells way of typing types
  • Ordinary types have kind
  • Int
  • String
  • Type constructors have kind -gt
  • Tree -gt
  • -gt
  • (,) -gt -gt

3
The Functor Class
  • class Functor f where
  • fmap (a -gt b) -gt (f a -gt f b)
  • Note how the class Functor requires a type
    constructor of kind -gt as an argument.
  • The method fmap abstracts the operation of
    applying a function on every parametric Argument.

a a a
Type T a
x x x
(f x) (f x) (f x)
fmap f
4
Notes
  • Special syntax for built in type constructors
  • (-gt) -gt -gt
  • -gt
  • (,) -gt -gt
  • (,,) -gt -gt -gt
  • 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
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
More Instances
  • instance Functor where
  • fmap f
  • fmap f (xxs) f x fmap f xs
  • instance Functor Maybe where
  • fmap f Nothing Nothing
  • fmap f (Just x) Just (f x)

7
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
  • Maingt t t1
  • t1 Tree Int
  • data Bin x Two x x
  • t2 Node (Two(Tip 5) (Tip 21))
  • Maingt t t2
  • t2 Tree Bin Int

8
What is the kind of Tree?
  • Tree is a binary type constructor
  • Its kind will be something like ? -gt ? -gt
  • The first argument to Tree is itself a type
    constructor, the second is just an ordinary type.
  • Tree ( -gt )-gt -gt

9
The Monad Class
Note m is a type constructor
  • class Monad m where
  • (gtgt) m a -gt (a -gt m b) -gt m b
  • (gtgt) m a -gt m b -gt m b
  • return a -gt m a
  • fail String -gt m a
  • p gtgt q p gtgt \ _ -gt q
  • fail s error s
  • We pronounce gtgt as bind
  • and gtgt as sequence

10
Default methods
  • Note that Monad has two default definitions
  • p gtgt q p gtgt \ _ -gt q
  • fail s error s
  • These are the definitions that are usually
    correct, so when making an instance of class
    Monad, only two definitions (gtgt and return) are
    usually given.

11
Do notation shorthand
  • The do notation is shorthand for the infix
    operator gtgt
  • do e gt e
  • do e1e2en gt e1 gtgt do e2en
  • do x lt- e1e2en where x is a variable
  • gt e1 gtgt \x -gt do e2en
  • do pat lt- e1 e2 en
  • gt let ok pat do e2 en
  • in e1 gtgt ok

12
Monads and Actions
  • Weve 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 gtgt k k x
  • Nothing gtgt k Nothing
  • return Just

13
Example
  • find Eq a gt a -gt (a,b) -gt 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 lt- find a x return (cb)
  • What is the type of test?
  • What does it return if the find fails?

14
Monad Laws
  • If you define your own monad then it needs to
    meet the following laws
  • return a gtgt k k a
  • m gtgt return m
  • m gtgt (\x -gt k x gtgt h) (m gtgt k) gtgt h
  • These laws specify the precise order that
    actions are performed. In do notation they
    appear as
  • do x lt- return a k x gt k a
  • do x lt- m return x gt m
  • do x lt- m y lt- k x h y gt
  • do y lt- (do x lt- m k x) h y
  • do m1 m2 m3 gt do (do m1 m2) m3

15
Using the laws
  • do k lt- getKey w return k
  • gt getKey w
  • do k lt- getKey w
  • n lt- changeKey k
  • respond n
  • gt
  • let keyStuff do k lt- getKey w
  • changeKey k
  • in do n lt- keyStuff
  • respond n

16
More Monad Instances
  • instance Monad where
  • (xxs) gtgt f f x (xs gtgt f)
  • gtgt f
  • return x x
  • What kind of action does the type constructor
    represent?

17
Generic Monad functions
  • sequence Monad m gt m a -gt m a
  • sequence foldr mcons (return )
  • where mcons p q do x lt- p
  • xs lt- q
  • return
    (xxs)
  • sequence_ Monad m gt m a -gt m ()
  • sequence_ foldr (gtgt) (return ())
  • mapM Monad m gt (a -gt m b) -gt a
    -gt m b
  • mapM f as sequence (map f as)
  • mapM_ Monad m gt (a -gt m b) -gt a
    -gt m ()
  • mapM_ f as sequence_ (map f as)
  • (ltlt) Monad m gt (a -gt m b) -gt m a
    -gt m b
  • f ltlt x x gtgt f

18
The MonadPlus Class
  • Some monads support recoverable failure
  • class Monad m gt MonadPlus m where
  • mzero m a -- failure
  • mplus m a -gt m a -gt m a -- recovery
  • Laws
  • m gtgt (\x -gt mzero) mzero
  • mzero gtgt m mzero
  • mzero mplus m m
  • m mplus mzero m
  • Think of mzero as 0, mplus as (), and (gtgt) as
    ()

19
MonadPlus Instances
  • instance MonadPlus where
  • mzero
  • mplus ys ys
  • (xxs)mplus ys x (xs mplus ys)
  • instance MonadPlus Maybe where
  • mzero Nothing
  • Nothing mplus ys ys
  • xs mplus ys xs
  • Do the laws hold?
Write a Comment
User Comments (0)
About PowerShow.com