Three Approaches
to Monads
Chris Evans
Three Approaches
I. How Monads Arise.
(Ref: You Could Have Invented Monads)
I. Monads in Haskell
(Ref: Real World Haskell)
I. Monads in Category Theory
(Ref: Wikibooks - Haskell/Category Theory)
I. How Monads Arise
In a typed language consider
f,g : Float -> Float
and their composition
g . f : Float -> Float
Example (A)
What if we want debugging information?
f : Float -> (Float, String)
f(x) = (2*x, “Doubled!”)
g : Float -> (Float, String)
g(x) = (x/4, “Quartered!”)
Now we can’t compose them. Yet there is an
obvious “composition”:
x |-> ((2*x)/4, “Doubled! Quartered!”)
Example (B)
Consider multi-valued functions
f : Advertisable -> [Campaign]
g : Campaign -> [Ad]
We can’t compose them but there is a natural
“composition”
(In Python)
adv |-> [g(camp) for camp in f(adv)]
But this gets ugly for multiple compositions...
Example (C)
Consider functions with possible failure
f : Logline -> TimeStamp (Not in Logline?)
g : TimeStamp -> DayOfWeek (Parsing fails?)
We can add a Bool to indicate success
f : Logline -> (TimeStamp, Bool)
g : TimeStamp -> (DayOfWeek, Bool)
We can no longer compose them but there is
again a natural “composition” (propagate
failure)
These three examples are similar.
Each involves a “decorated type”
(A) (Float, String) for Float
(B) [Campaign] for Campaign
(C) (TimeStamp, Bool) for TimeStamp
For each example, we first define two functions,
unit and bind.
unit -- maps basic type to decorated type
bind -- “upgrades” function which takes basic
type to instead take decorated type
Example (A)
unit : Float -> (Float, String)
bind : (Float -> (Float, String)) -> ((Float, String) -> (Float, String))
Example (A)
unit : Float -> (Float, String)
x |-> (x, “”)
bind : (Float -> (Float, String)) -> ((Float, String) -> (Float, String))
Example (A)
unit : Float -> (Float, String)
x |-> (x, “”)
bind : (Float -> (Float, String)) -> ((Float, String) -> (Float, String))
(bind f)(x, old_str) = let (y, new_str) = f x
in (y, old_str ++ new_str)
Example (B)
unit : Campaign -> [Campaign]
bind : (Campaign -> [Ad]) -> ([Campaign] -> [Ad])
Example (B)
unit : Campaign -> [Campaign]
camp |-> [camp]
bind : (Campaign -> [Ad]) -> ([Campaign] -> [Ad])
Example (B)
unit : Campaign -> [Campaign]
camp |-> [camp]
bind : (Campaign -> [Ad]) -> ([Campaign] -> [Ad])
(bind f) campaigns = flatten([f(camp) for camp in campaigns])
Example (C)
unit : TimeStamp -> (TimeStamp, Bool)
bind : (TimeStamp -> (DayOfWeek, Bool))
-> ((TimeStamp, Bool) -> (DayOfWeek, Bool))
Example (C)
unit : TimeStamp -> (TimeStamp, Bool)
ts |-> (ts, True)
bind : (TimeStamp -> (DayOfWeek, Bool))
-> ((TimeStamp, Bool) -> (DayOfWeek, Bool))
Example (C)
unit : TimeStamp -> (TimeStamp, Bool)
ts |-> (ts, True)
bind : (TimeStamp -> (DayOfWeek, Bool))
-> ((TimeStamp, Bool) -> (DayOfWeek, Bool))
(bind f)(x,b) = case b of
True -> f(x)
False -> (??, False)
Points
bind gives us the desired composition!
Instead of g.f do (bind g).f
Points
bind gives us the desired composition!
Instead of g.f do (bind g).f
unit can be used to start the chain.
Instead of g.f do (bind g).(bind f).unit
Points
bind gives us the desired composition!
Instead of g.f do (bind g).f
unit can be used to start the chain.
Instead of g.f do (bind g).(bind f).unit
unit/bind depend only on the “type extension”.
e.g. there is a unit/bind for List not List-of-Strings.
The Big Reveal
Each example is an example of a monad!
But what is a monad?
For Part I we’ll define a monad as
“A type extension together with an implementation of unit
and bind”
Ex:
[]
(, String)
(, Bool)
II. Monads in Haskell
Haskell
Strongly/Statically typed
(cannot even add Integer and Float!)
Pure functional language
(absolutely no side effects. I/O must be handled specially)
There’s a type for that
(where other languages use enums/structs/classes, Haskell uses types)
Defining Haskell Types
Type Synonyms: (type)
type TimeStamp = String
type Advertisable = String
type Campaign = String
Syntactic sugar for existing types. In fact
type String = [Char]
New Data Types: (data)
type Name = String
type Age = Int
data Customer = Customer Name Age
“type constructor” “value constructor”
Algebraic Data Types:
data Bool = True | False
data Color = Red | Blue | Green
data LineType = Impression
|Click
|Win
|Bid
value constructors
More Algebraic Data Types:
type CPM = Float
type VCPC = Float
type ClickProb = Float
data PricingStrategy = FixedBid CPM
|BidIQ VCPC
bid_price :: PricingStrategy -> ClickProb -> CPM
bid_price strat p = case strat of
FixedBid cpm -> cpm
BidIQ vcpc -> vcpc * p
Parameterized Types:
data Maybe a = Just a | Nothing
data List a = Cons a (List a) | Nil
data Tree a = Node a (Tree a) (Tree a)
|Empty
Then can use Maybe String or List Float
Type Classes:
<<WARNING>>
NOTHING LIKE CLASSES IN OO LANGUAGES!
More like interfaces in OO languages...
Example: The Show Typeclass
class Show a where
show :: a -> String
data LineType = Impression | Click | Win
instance Show LineType where
show Impression = “imp”
show Click = “cli”
show Win = “win”
In fact the Haskell REPL uses show to display results!
Example: The Eq Typeclass
class Eq a where
(==),(/=) :: a -> a -> Bool
x == y = not (x /= y)
x /= y = not (x == y)
Only need to define one, the other
comes free!
Enter Monads!
Monads in Haskell are just a typeclass
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b (bind)
return :: a -> m a (unit)
...which a parameterized type can implement!
Revisit the Examples in Part I
Example (B) -- The List Monad
instance Monad [] where
return x = [x]
xs >>= f = concat (map f xs)
(This is in the Haskell Prelude)
The Control.Monad library provides >=> for direct composition.
Haskell do syntax and list comprehensions
Example (C) -- The Maybe Monad
instance Monad Maybe where
return x = Just x
(Just x) >>= f = f x
Nothing >>= f = Nothing
(This is in the Haskell Prelude)
Example (A) -- The History Monad
data History a = History a String
instance Monad History where
return x = History x “”
(History x old) >>= f = let (History y new) = f x
in History y (old ++ new)
-- Implementation of Show omitted --
(Not in Prelude -- this is our creation)
Bonus Monad -- IO Monad
I/O in Haskell is handled via the IO Monad.
e.g. Haskell’s “print” is
putStrLn :: String -> IO ()
Part II Takeaways
In Haskell, a Monad is just a typeclass
... which a parametric type can implement by
implementing return and (>>=).
Basic I/O in Haskell uses the IO Monad.
do syntactic sugar emulates imperative code.
III. Monads in Category Theory
Category Theory
A category is a collection of
objects
arrows/morphisms between pairs of objects
notion of composition of arrows
satisfying
(Associativity) h.(g.f) = (h.g).f
(Identity) id . f = f , g . id = g
Ex: Set (Category of Sets)
Objects: Sets
Arrows: Functions between sets
Note: Set contains all sets and all
functions between sets in the universe!
Set
{1, 3}
{2, 4}
{7, 8, 1}
id
f
id
g
h
g . f
r
Ex: Hask (Category of Haskell Types)
Objects: Haskell types
Arrows: Haskell functions
Note: Hask contains all types and all
functions in the universe!
Hask
[String]
String
Integer
id
my_join
id
my_len
my_func
my_split
Functors
A functor is a mapping F between categories C and D.
Maps each object X in C to F(X) in D
Maps each arrow f : X -> Y to F(f) : F(X) -> F(Y)
satisfying
F(id) = id
F(g . f)=F(g) . F(f)
Ex: Powerset Functor P : Set -> Set
Maps each set (object) to its power set
e.g. P({1,2}) = { ϕ , {1}, {2}, {1,2} }
Maps arrow f : X -> Y to P(f) : P(X) -> P(Y) by
[F(f)](S) = { f(s) | s in S}
(exercise: check that functor conditions are satisfied)
Monads
A monad is a functor M : C -> C along with two mappings
for every object X in C.
unit : X -> M(X)
join : M(M(X)) -> M(X)
such that unit and join satisfy certain properties (“monad
laws”)...
Monad Laws
1.join . M(join) = join . join
2.join . M(unit) = join . unit = id
3.unit . f = M(f) . unit
4.join . M(M(f)) = M(f) . join
Ex: Powerset Functor is a Monad
where
unit : X -> P(X)
join : P(P(X)) -> P(X)
Ex: Powerset Functor is a Monad
where
unit : X -> P(X)
unit(x) = {x}
join : P(P(X)) -> P(X)
Ex: Powerset Functor is a Monad
where
unit : X -> P(X)
unit(x) = {x}
join : P(P(X)) -> P(X)
join(S) = U
s in S
s
Check Monad Laws (We’ll just check #3 here)
3) unit . f = P(f) . unit
Consider f : {1, 2} -> {3, 4, 5}
1 |-> 3, 2 |-> 5
(unit . f)(1) = unit(f(1)) = unit(3) = {3}
(P(f).unit)(1) = [P(f)](unit(1)) = [P(f)]({1}) = {3}
same!
Back to Hask
[ ] is a functor from Hask to Hask.
● Maps objects:
● Maps functions:
Back to Hask
[ ] is a functor from Hask to Hask.
● Maps objects:
Maps type to list of that type
● Maps functions:
Back to Hask
[ ] is a functor from Hask to Hask.
● Maps objects:
Maps type to list of that type
● Maps functions:
e.g. maps function f : String -> Float to [ ](f): [String] -> [Float]
by [ ](f) = map f
In fact, [ ] is a monad from Hask to Hask.
unit : X -> [X]
join : [[X]] -> [X]
In fact, [ ] is a monad from Hask to Hask.
unit : X -> [X]
x |-> [x]
join : [[X]] -> [X]
In fact, [ ] is a monad from Hask to Hask.
unit : X -> [X]
x |-> [x]
join : [[X]] -> [X]
list_of_lists |-> concat list_of_lists
In fact, [ ] is a monad from Hask to Hask.
unit : X -> [X]
x |-> [x]
join : [[X]] -> [X]
list_of_lists |-> concat list_of_lists
(Exercise: Check the monad laws)
Maybe is a functor from Hask to Hask.
● Maps objects:
● Maps functions:
Maybe is a functor from Hask to Hask.
● Maps objects:
e.g. maps String to Maybe String
● Maps functions:
Maybe is a functor from Hask to Hask.
● Maps objects:
e.g. maps String to Maybe String
● Maps functions:
given f : X -> Y
define Maybe f : Maybe X -> Maybe Y
Just x |-> Just (f x)
Nothing |-> Nothing
In fact, Maybe is a monad from Hask to Hask.
unit : X -> Maybe X
join : Maybe (Maybe X) -> Maybe X
In fact, Maybe is a monad from Hask to Hask.
unit : X -> Maybe X
x |-> Just x
join : Maybe (Maybe X) -> Maybe X
In fact, Maybe is a monad from Hask to Hask.
unit : X -> Maybe X
x |-> Just x
join : Maybe (Maybe X) -> Maybe X
Just (Just x) |-> Just x
Just Nothing |-> Nothing
Nothing |-> Nothing
In fact, Maybe is a monad from Hask to Hask.
unit : X -> Maybe X
x |-> Just x
join : Maybe (Maybe X) -> Maybe X
Just (Just x) |-> Just x
Just Nothing |-> Nothing
Nothing |-> Nothing
(Exercise: Check the monad laws)
Wait… definitions seem different?
Part II (Haskell):
Monad is a typeclass
and you define
unit/return
bind/(>>=)
Part III (Categories):
Monad is a functor
and you define
how it maps functions
unit
join
and check monad laws!
Answers
● Haskell Monad is just a programming construct
○ Doesn’t require monad laws to compile
○Typeclass could have a different name
Answers
● Haskell Monad is just a programming construct
○ Doesn’t require monad laws to compile
○Typeclass could have a different name
● Haskell also has a typeclass Functor
○ Implements fmap
○But Monad instance need not be Functor instance
Answers
● return, (>>=) are equivalent to unit, join, fmap by
unit = return
join x = x >>= id
fmap f x = x >>=(return.f)
return = unit
x >>= f = join(fmap f x)
Fin
Bibliography
Piponi, Dan “You Could Have Invented Monads!”
http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html.
August, 2006. Accessed June, 2015.
O’Sullivan, B., Stewart, D., Goerzen, J. Real World Haskell.
O’Reilly Media, 2008. Print.
https://en.wikibooks.org/wiki/Haskell/Category_theory

(2015 06-16) Three Approaches to Monads

  • 1.
  • 2.
    Three Approaches I. HowMonads Arise. (Ref: You Could Have Invented Monads) I. Monads in Haskell (Ref: Real World Haskell) I. Monads in Category Theory (Ref: Wikibooks - Haskell/Category Theory)
  • 3.
  • 4.
    In a typedlanguage consider f,g : Float -> Float and their composition g . f : Float -> Float Example (A)
  • 5.
    What if wewant debugging information? f : Float -> (Float, String) f(x) = (2*x, “Doubled!”) g : Float -> (Float, String) g(x) = (x/4, “Quartered!”)
  • 6.
    Now we can’tcompose them. Yet there is an obvious “composition”: x |-> ((2*x)/4, “Doubled! Quartered!”)
  • 7.
    Example (B) Consider multi-valuedfunctions f : Advertisable -> [Campaign] g : Campaign -> [Ad] We can’t compose them but there is a natural “composition”
  • 8.
    (In Python) adv |->[g(camp) for camp in f(adv)] But this gets ugly for multiple compositions...
  • 9.
    Example (C) Consider functionswith possible failure f : Logline -> TimeStamp (Not in Logline?) g : TimeStamp -> DayOfWeek (Parsing fails?)
  • 10.
    We can adda Bool to indicate success f : Logline -> (TimeStamp, Bool) g : TimeStamp -> (DayOfWeek, Bool) We can no longer compose them but there is again a natural “composition” (propagate failure)
  • 11.
    These three examplesare similar. Each involves a “decorated type” (A) (Float, String) for Float (B) [Campaign] for Campaign (C) (TimeStamp, Bool) for TimeStamp
  • 12.
    For each example,we first define two functions, unit and bind. unit -- maps basic type to decorated type bind -- “upgrades” function which takes basic type to instead take decorated type
  • 13.
    Example (A) unit :Float -> (Float, String) bind : (Float -> (Float, String)) -> ((Float, String) -> (Float, String))
  • 14.
    Example (A) unit :Float -> (Float, String) x |-> (x, “”) bind : (Float -> (Float, String)) -> ((Float, String) -> (Float, String))
  • 15.
    Example (A) unit :Float -> (Float, String) x |-> (x, “”) bind : (Float -> (Float, String)) -> ((Float, String) -> (Float, String)) (bind f)(x, old_str) = let (y, new_str) = f x in (y, old_str ++ new_str)
  • 16.
    Example (B) unit :Campaign -> [Campaign] bind : (Campaign -> [Ad]) -> ([Campaign] -> [Ad])
  • 17.
    Example (B) unit :Campaign -> [Campaign] camp |-> [camp] bind : (Campaign -> [Ad]) -> ([Campaign] -> [Ad])
  • 18.
    Example (B) unit :Campaign -> [Campaign] camp |-> [camp] bind : (Campaign -> [Ad]) -> ([Campaign] -> [Ad]) (bind f) campaigns = flatten([f(camp) for camp in campaigns])
  • 19.
    Example (C) unit :TimeStamp -> (TimeStamp, Bool) bind : (TimeStamp -> (DayOfWeek, Bool)) -> ((TimeStamp, Bool) -> (DayOfWeek, Bool))
  • 20.
    Example (C) unit :TimeStamp -> (TimeStamp, Bool) ts |-> (ts, True) bind : (TimeStamp -> (DayOfWeek, Bool)) -> ((TimeStamp, Bool) -> (DayOfWeek, Bool))
  • 21.
    Example (C) unit :TimeStamp -> (TimeStamp, Bool) ts |-> (ts, True) bind : (TimeStamp -> (DayOfWeek, Bool)) -> ((TimeStamp, Bool) -> (DayOfWeek, Bool)) (bind f)(x,b) = case b of True -> f(x) False -> (??, False)
  • 22.
    Points bind gives usthe desired composition! Instead of g.f do (bind g).f
  • 23.
    Points bind gives usthe desired composition! Instead of g.f do (bind g).f unit can be used to start the chain. Instead of g.f do (bind g).(bind f).unit
  • 24.
    Points bind gives usthe desired composition! Instead of g.f do (bind g).f unit can be used to start the chain. Instead of g.f do (bind g).(bind f).unit unit/bind depend only on the “type extension”. e.g. there is a unit/bind for List not List-of-Strings.
  • 25.
    The Big Reveal Eachexample is an example of a monad! But what is a monad?
  • 26.
    For Part Iwe’ll define a monad as “A type extension together with an implementation of unit and bind” Ex: [] (, String) (, Bool)
  • 27.
  • 28.
    Haskell Strongly/Statically typed (cannot evenadd Integer and Float!) Pure functional language (absolutely no side effects. I/O must be handled specially) There’s a type for that (where other languages use enums/structs/classes, Haskell uses types)
  • 29.
  • 30.
    Type Synonyms: (type) typeTimeStamp = String type Advertisable = String type Campaign = String Syntactic sugar for existing types. In fact type String = [Char]
  • 31.
    New Data Types:(data) type Name = String type Age = Int data Customer = Customer Name Age “type constructor” “value constructor”
  • 32.
    Algebraic Data Types: dataBool = True | False data Color = Red | Blue | Green data LineType = Impression |Click |Win |Bid value constructors
  • 33.
    More Algebraic DataTypes: type CPM = Float type VCPC = Float type ClickProb = Float data PricingStrategy = FixedBid CPM |BidIQ VCPC bid_price :: PricingStrategy -> ClickProb -> CPM bid_price strat p = case strat of FixedBid cpm -> cpm BidIQ vcpc -> vcpc * p
  • 34.
    Parameterized Types: data Maybea = Just a | Nothing data List a = Cons a (List a) | Nil data Tree a = Node a (Tree a) (Tree a) |Empty Then can use Maybe String or List Float
  • 35.
    Type Classes: <<WARNING>> NOTHING LIKECLASSES IN OO LANGUAGES! More like interfaces in OO languages...
  • 36.
    Example: The ShowTypeclass class Show a where show :: a -> String data LineType = Impression | Click | Win instance Show LineType where show Impression = “imp” show Click = “cli” show Win = “win”
  • 37.
    In fact theHaskell REPL uses show to display results!
  • 38.
    Example: The EqTypeclass class Eq a where (==),(/=) :: a -> a -> Bool x == y = not (x /= y) x /= y = not (x == y) Only need to define one, the other comes free!
  • 41.
  • 42.
    Monads in Haskellare just a typeclass class Monad m where (>>=) :: m a -> (a -> m b) -> m b (bind) return :: a -> m a (unit) ...which a parameterized type can implement!
  • 43.
  • 44.
    Example (B) --The List Monad instance Monad [] where return x = [x] xs >>= f = concat (map f xs) (This is in the Haskell Prelude)
  • 48.
    The Control.Monad libraryprovides >=> for direct composition.
  • 49.
    Haskell do syntaxand list comprehensions
  • 50.
    Example (C) --The Maybe Monad instance Monad Maybe where return x = Just x (Just x) >>= f = f x Nothing >>= f = Nothing (This is in the Haskell Prelude)
  • 53.
    Example (A) --The History Monad data History a = History a String instance Monad History where return x = History x “” (History x old) >>= f = let (History y new) = f x in History y (old ++ new) -- Implementation of Show omitted -- (Not in Prelude -- this is our creation)
  • 56.
    Bonus Monad --IO Monad I/O in Haskell is handled via the IO Monad. e.g. Haskell’s “print” is putStrLn :: String -> IO ()
  • 58.
    Part II Takeaways InHaskell, a Monad is just a typeclass ... which a parametric type can implement by implementing return and (>>=). Basic I/O in Haskell uses the IO Monad. do syntactic sugar emulates imperative code.
  • 59.
    III. Monads inCategory Theory
  • 60.
    Category Theory A categoryis a collection of objects arrows/morphisms between pairs of objects notion of composition of arrows satisfying (Associativity) h.(g.f) = (h.g).f (Identity) id . f = f , g . id = g
  • 61.
    Ex: Set (Categoryof Sets) Objects: Sets Arrows: Functions between sets Note: Set contains all sets and all functions between sets in the universe!
  • 62.
    Set {1, 3} {2, 4} {7,8, 1} id f id g h g . f r
  • 63.
    Ex: Hask (Categoryof Haskell Types) Objects: Haskell types Arrows: Haskell functions Note: Hask contains all types and all functions in the universe!
  • 64.
  • 65.
    Functors A functor isa mapping F between categories C and D. Maps each object X in C to F(X) in D Maps each arrow f : X -> Y to F(f) : F(X) -> F(Y) satisfying F(id) = id F(g . f)=F(g) . F(f)
  • 66.
    Ex: Powerset FunctorP : Set -> Set Maps each set (object) to its power set e.g. P({1,2}) = { ϕ , {1}, {2}, {1,2} } Maps arrow f : X -> Y to P(f) : P(X) -> P(Y) by [F(f)](S) = { f(s) | s in S} (exercise: check that functor conditions are satisfied)
  • 67.
    Monads A monad isa functor M : C -> C along with two mappings for every object X in C. unit : X -> M(X) join : M(M(X)) -> M(X) such that unit and join satisfy certain properties (“monad laws”)...
  • 68.
    Monad Laws 1.join .M(join) = join . join 2.join . M(unit) = join . unit = id 3.unit . f = M(f) . unit 4.join . M(M(f)) = M(f) . join
  • 69.
    Ex: Powerset Functoris a Monad where unit : X -> P(X) join : P(P(X)) -> P(X)
  • 70.
    Ex: Powerset Functoris a Monad where unit : X -> P(X) unit(x) = {x} join : P(P(X)) -> P(X)
  • 71.
    Ex: Powerset Functoris a Monad where unit : X -> P(X) unit(x) = {x} join : P(P(X)) -> P(X) join(S) = U s in S s
  • 72.
    Check Monad Laws(We’ll just check #3 here) 3) unit . f = P(f) . unit Consider f : {1, 2} -> {3, 4, 5} 1 |-> 3, 2 |-> 5 (unit . f)(1) = unit(f(1)) = unit(3) = {3} (P(f).unit)(1) = [P(f)](unit(1)) = [P(f)]({1}) = {3} same!
  • 73.
    Back to Hask [] is a functor from Hask to Hask. ● Maps objects: ● Maps functions:
  • 74.
    Back to Hask [] is a functor from Hask to Hask. ● Maps objects: Maps type to list of that type ● Maps functions:
  • 75.
    Back to Hask [] is a functor from Hask to Hask. ● Maps objects: Maps type to list of that type ● Maps functions: e.g. maps function f : String -> Float to [ ](f): [String] -> [Float] by [ ](f) = map f
  • 76.
    In fact, [] is a monad from Hask to Hask. unit : X -> [X] join : [[X]] -> [X]
  • 77.
    In fact, [] is a monad from Hask to Hask. unit : X -> [X] x |-> [x] join : [[X]] -> [X]
  • 78.
    In fact, [] is a monad from Hask to Hask. unit : X -> [X] x |-> [x] join : [[X]] -> [X] list_of_lists |-> concat list_of_lists
  • 79.
    In fact, [] is a monad from Hask to Hask. unit : X -> [X] x |-> [x] join : [[X]] -> [X] list_of_lists |-> concat list_of_lists (Exercise: Check the monad laws)
  • 80.
    Maybe is afunctor from Hask to Hask. ● Maps objects: ● Maps functions:
  • 81.
    Maybe is afunctor from Hask to Hask. ● Maps objects: e.g. maps String to Maybe String ● Maps functions:
  • 82.
    Maybe is afunctor from Hask to Hask. ● Maps objects: e.g. maps String to Maybe String ● Maps functions: given f : X -> Y define Maybe f : Maybe X -> Maybe Y Just x |-> Just (f x) Nothing |-> Nothing
  • 83.
    In fact, Maybeis a monad from Hask to Hask. unit : X -> Maybe X join : Maybe (Maybe X) -> Maybe X
  • 84.
    In fact, Maybeis a monad from Hask to Hask. unit : X -> Maybe X x |-> Just x join : Maybe (Maybe X) -> Maybe X
  • 85.
    In fact, Maybeis a monad from Hask to Hask. unit : X -> Maybe X x |-> Just x join : Maybe (Maybe X) -> Maybe X Just (Just x) |-> Just x Just Nothing |-> Nothing Nothing |-> Nothing
  • 86.
    In fact, Maybeis a monad from Hask to Hask. unit : X -> Maybe X x |-> Just x join : Maybe (Maybe X) -> Maybe X Just (Just x) |-> Just x Just Nothing |-> Nothing Nothing |-> Nothing (Exercise: Check the monad laws)
  • 87.
    Wait… definitions seemdifferent? Part II (Haskell): Monad is a typeclass and you define unit/return bind/(>>=) Part III (Categories): Monad is a functor and you define how it maps functions unit join and check monad laws!
  • 88.
    Answers ● Haskell Monadis just a programming construct ○ Doesn’t require monad laws to compile ○Typeclass could have a different name
  • 89.
    Answers ● Haskell Monadis just a programming construct ○ Doesn’t require monad laws to compile ○Typeclass could have a different name ● Haskell also has a typeclass Functor ○ Implements fmap ○But Monad instance need not be Functor instance
  • 90.
    Answers ● return, (>>=)are equivalent to unit, join, fmap by unit = return join x = x >>= id fmap f x = x >>=(return.f) return = unit x >>= f = join(fmap f x)
  • 91.
  • 92.
    Bibliography Piponi, Dan “YouCould Have Invented Monads!” http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html. August, 2006. Accessed June, 2015. O’Sullivan, B., Stewart, D., Goerzen, J. Real World Haskell. O’Reilly Media, 2008. Print. https://en.wikibooks.org/wiki/Haskell/Category_theory