DOC PREVIEW
CALTECH CS 11 - Haskell track

This preview shows page 1-2-3-4-25-26-27-52-53-54-55 out of 55 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 11 Haskell track lecture 4 n This week Monads Monads n Have already seen an example of a monad n n n IO monad But similar concepts can be used for a lot of completely unrelated tasks Monads are useful general interfaces to a wide variety of computational tasks Monads n Monads can act as generalized containers n n or as generalized transformers or actions n n n e g List monad e g IO monad State monad and many other things as well Don t get hung up on one viewpoint n all are valid Category theory n The word Monad comes from a branch of mathematics known as category theory n n n However we won t deal with category theory here If you re interested in this I can talk more about this off line CT is relevant but not strictly necessary to understand Haskell monads Monads n Haskell defines a Monad type class like this class Monad return fail m where m a a m b m b m a m b m b a m a String m a Monads n What does this mean class Monad return fail m where m a a m b m b m a m b m b a m a String m a Monads n Let s ignore and fail for now class Monad return fail m where m a a m b m b m a m b m b a m a String m a Effects n n To explain further we need to talk about the notion of functions with effects Effects may include input output IO monad manipulating local or global state State monad raising exceptions Error monad possible failure Maybe monad or returning multiple values List monad n or other possibilities Functions and effects 1 n There are many kinds of functions or function like actions that we might want to do that have effects beyond mapping specific inputs to specific outputs Functions and effects 2 n n n n A normal function has the signature a b for some types a and b If such a function also had some kind of effect call it E then we might write this as a E b I ll refer to functions with effects as monadic functions Functions and effects 3 n n n A normal function of type a b can be composed with a function of type b c to give a function of type a c How would be compose a function with effects monadic function with another such function How do we compose a E1 b with b E2 c to give a function a E1 E2 c Functions and effects 4 n Haskell represents functions with effects i e a E b as having the type a E b where E is some kind of a monad like IO n n We ll write m instead of E from now on So we need to figure out how to compose functions of type a m b with functions of type b m c to get functions of type a m c Functions and effects 5 n n Being able to compose functions with effects is critical because we want to be able to build larger effectful functions by composing smaller effectful functions Example chaining together functions that read input from the terminal in the IO monad to functions that write output to the terminal in the IO monad Functions and effects 6 n We want to compose functions with types n n n n n f1 a m b f2 b m c to get a function with type a m c We can pass a value of type a to f1 to get a value of type m b Then we need to somehow take the m b value unpack a value of type b and pass it to f2 to get the final m c value Functions and effects 7 n n How do we take the m b value unpack a value of type b and pass it to f2 to get the final m c value The answer is specific to every monad n n For IO it s kind of magical the system takes care of it This is why there is the function in the Monad type class with the type signature m a a m b m b Functions and effects 8 n Note the type signature n n is the same as n n n m a a m b m b m b b m c m c just change the type variable names so this is indeed what we want Functions and effects 9 n The bind operator n n m a a m b m b is thus a kind of monadic apply operator which takes a monadic value of type m a unpacks a value of type a somehow and feeds it to the monadic function of type a m b to get the final monadic value of type m b Functions and effects 10 n The bind operator n n m a a m b m b is part of the Monad type class so it has a separate overloaded definition for every instance of the Monad type class n such as IO State Error Maybe List etc Monad definition again class Monad m where m a a m b m b return a m a n Note that instances of Monad i e m must be polymorphic type constructors n m is a type constructor m a is a type n Whereas instances of Eq Ord etc are just regular types not type constructors Monad definition again n N B IO is a type constructor so IO can substitute for m here instance Monad IO where IO a a IO b IO b definition omitted return a IO a definition omitted Monad laws n Haskell s monads must obey these laws 1 return x f 2 mx return 3 mx f g mx x f x n n n f x mx g 1 and 2 are sorta kinda identity laws 3 is sorta kinda an associative law here mx is a value of type m x Note 3 mx f g mx x f x g Can write this as 3 mx x f x g mx x f x g n n Slightly more intuitive Monad laws 2 n n n n Monad laws just ensure that composing of monadic functions behaves properly Can re write them in terms of the monadic composition operator which we haven t seen before a m b b m c a m c This can be found in the module Control Monad if you re curious Monad laws 3 n In terms of and monadic functions n mf a m b n mg b m c n mh c m d the monad laws become 1 return mf mf left identity 2 mf return mf right identity n n n n 3 mf mg mh mf mg mh associativity Monad laws 4 n n n Haskell doesn t and can t enforce the monad laws n …


View Full Document

CALTECH CS 11 - Haskell track

Download Haskell track
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Haskell track and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Haskell track 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?