Lecture 9
M.. M.. MONADS!
F12
, then click ConsoleF12
, then click ConsoleCtrl+Shift+k
↓ , PgDn , n , j 
next slide 
↑ , PgUp , p , k 
prev slide 
Esc 
enables ctrl+f globally 
 Designed by Eric J. Bomgardner
module Main where
main = do
theInput < readFile "input.txt"
writeFile "output.txt" (reverse theInput)
 Designed by Saul Brodsky
module Main where
main = do
answer < readFile "input.txt"
let rev = reverse answer in writeFile "output.txt" rev
 Designed by Travis Dean
main = interact reverse
 This must be run by directing input and output like so:
 $ runhaskell reverseFile.hs < input.txt > output.txt
 Designed by Anish S. Tondwalkar
main = readFile input >>= return . reverse >>= writeFile output
where input = "input"
output = "output"
Maybe
data type
data Maybe a = Nothing  Just a
Use Maybe
when the output isn't guaranteed
Prelude> import Data.List
Prelude> :t elemIndex
elemIndex :: Eq a => a > [a] > Maybe Int
Prelude Data.List> elemIndex 'b' "abc"
Just 1
Prelude Data.List> elemIndex 'z' "abc"
Nothing
Maybe
wraps things into a context.
Just 1
vs 1
Just "hello"
vs "hello"
Just 'a'
vs 'a'
Nothing
vs ERROR!
A Functor generalizes the map
function
It implements fmap
fmap :: (a > b) > f a > f b
Naturally, a list derives the Functor Typeclass
Prelude> fmap (+1) [1,2,3]
[2,3,4]
Maybe
is a Functor
*Main> fmap (+1) (Just 1)
Just 2
IO
is a Functor
*Main> fmap (++"!") getLine
hi
"hi!"
First law:
fmap id F = F
Second law:
fmap (f . g) F = fmap f (fmap g F)
Prelude> import Control.Applicative
Prelude> (*) <$> Just 2 <*> Just 8
Just 16
We won't go into much detail about the Applicative Typeclass
A Monoid is a function (•) that satisfies the following:
∀ a,b ∈ S: a•b ∈ S
The Set (S) is closed under the binary function (•).∀ a,b,c ∈ S: (a•b)•c = a•(b•c)
The binary function is associative∃ e∈S: ∀ a∈S: e•a = a•e = a
e is the identity element+
, 0
is a Monoid.
Which of the following are Monoids?
*
, 1
, S={0,1,2,...}
, False
, S={True, False}++
, []
, S={"", "a", "ab",...}∪
, ∅
, S={A,B,...}÷
, 1
, S={1,2,...}In Haskell, the binary function is called mappend
and its identity element is mempty
Prelude> import Data.Monoid
Prelude Data.Monoid> [1,2,3] `mappend` [4,5,6]
[1,2,3,4,5,6]
Prelude Data.Monoid> [1,2,3] `mappend` mempty
[1,2,3]
Prelude Data.Monoid> mempty `mappend` [1,2,3]
[1,2,3]
As you can see above, lists are Monoids.
If you have a type a
and a function a > b
then you can get b
.
Prelude> odd 3
True
Maybe
ContinuedIf we instead have m a
and a function a > m b
How would we get m b
?
For example, given Maybe a
and a > Maybe b
How would we get Maybe b
?
The answer we desire looks like this
(>>=) :: (Monad m) => m a > (a > m b) > m b
>>=
is called a bind
Let's say we had the function f
below
f :: Int > Maybe Int
f n = Just (n+1)
Prelude> f 1
Just 2
What if we wanted to pass in Just 1
?
Prelude> f (Just 1)
ERROR!
(>>=)
Prelude> :t (>>=)
(>>=) :: Monad m => m a > (a > m b) > m b
Prelude> Just 1 >>= f
Just 2
Monad
TypeclassA Monad is Typeclass with some extra rules.
The two important functions: return
and >>=
return :: a > m a
(>>=) :: m a > (a > m b) > m b
>>
functionThis function simply glues together Monads.
Prelude> print "foo" >>= \_ > print "bar"
"foo"
"bar"
Prelude> print "foo" >> print "bar"
"foo"
"bar"
do
notation is basically an interweaving of >>
main = do
print "foo"
print "bar"
do
usageWrite this piece of code without using do
main = do
putStrLn "Enter name:"
name < getLine
putStrLn ("Hi " ++ name)
Try it out by yourself
main = putStrLn "Enter name:" >> getLine >>= putStrLn.("Hi " ++)
For something to truly be a Monad,
it must also obey the following laws: *
Left Identity
return a >>= f
≡ f a
Right Identity
m >>= return
≡ m
Associativity
(m >>= f) >>= g
≡ m >>= (\x > f x >>= g)
These laws are somewhat similar to those of Monoids.
Here's another way to write the three laws

≡ 


≡ 


≡ 

(>=>) :: Monad m => (a > m b) > (b > m c) > a > m c
Left Identity
return >=> g
≡ g
Right Identity
f >=> return
≡ f
Associativity
(f >=> g) >=> h
≡ f >=> (g >=> h)
These Monad laws form a mathematical category.
A monad in X is just a monoid in the category of endofunctors of X, with product × replaced by composition of endofunctors and unit set by the identity endofunctor.
Monads are like burritos