SlideShare a Scribd company logo
1 of 92
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

More Related Content

What's hot

Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavVyacheslav Arbuzov
 
A Survey Of R Graphics
A Survey Of R GraphicsA Survey Of R Graphics
A Survey Of R GraphicsDataspora
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoidsLuka Jacobowitz
 
Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020Luka Jacobowitz
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverseLuka Jacobowitz
 
peRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysispeRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysisVyacheslav Arbuzov
 
Scientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
Scientific Computing with Python Webinar March 19: 3D Visualization with MayaviScientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
Scientific Computing with Python Webinar March 19: 3D Visualization with MayaviEnthought, Inc.
 
Gentlest Introduction to Tensorflow - Part 2
Gentlest Introduction to Tensorflow - Part 2Gentlest Introduction to Tensorflow - Part 2
Gentlest Introduction to Tensorflow - Part 2Khor SoonHin
 
All You Need is Fold in the Key of C#
All You Need is Fold in the Key of C#All You Need is Fold in the Key of C#
All You Need is Fold in the Key of C#Mike Harris
 

What's hot (15)

Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
 
A Survey Of R Graphics
A Survey Of R GraphicsA Survey Of R Graphics
A Survey Of R Graphics
 
Perm winter school 2014.01.31
Perm winter school 2014.01.31Perm winter school 2014.01.31
Perm winter school 2014.01.31
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoids
 
Seminar psu 20.10.2013
Seminar psu 20.10.2013Seminar psu 20.10.2013
Seminar psu 20.10.2013
 
Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
 
Functor Laws
Functor LawsFunctor Laws
Functor Laws
 
Chapter7
Chapter7Chapter7
Chapter7
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverse
 
peRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysispeRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysis
 
R introduction v2
R introduction v2R introduction v2
R introduction v2
 
Scientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
Scientific Computing with Python Webinar March 19: 3D Visualization with MayaviScientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
Scientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
 
Gentlest Introduction to Tensorflow - Part 2
Gentlest Introduction to Tensorflow - Part 2Gentlest Introduction to Tensorflow - Part 2
Gentlest Introduction to Tensorflow - Part 2
 
All You Need is Fold in the Key of C#
All You Need is Fold in the Key of C#All You Need is Fold in the Key of C#
All You Need is Fold in the Key of C#
 

Viewers also liked

Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskellgoncharenko
 
전자책산업동향과 서비스 모델 (Slide Share)
전자책산업동향과 서비스 모델 (Slide Share)전자책산업동향과 서비스 모델 (Slide Share)
전자책산업동향과 서비스 모델 (Slide Share)Joong Ho Lee
 
Real-World Functional Programming @ Incubaid
Real-World Functional Programming @ IncubaidReal-World Functional Programming @ Incubaid
Real-World Functional Programming @ IncubaidNicolas Trangez
 
Haskell study 15
Haskell study 15Haskell study 15
Haskell study 15Nam Hyeonuk
 
The HaLVM: A Simple Platform for Simple Platforms
The HaLVM: A Simple Platform for Simple PlatformsThe HaLVM: A Simple Platform for Simple Platforms
The HaLVM: A Simple Platform for Simple PlatformsThe Linux Foundation
 
OSv at Cassandra Summit
OSv at Cassandra SummitOSv at Cassandra Summit
OSv at Cassandra SummitDon Marti
 
Introducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGIntroducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGDavid Galichet
 
Playing with State Monad
Playing with State MonadPlaying with State Monad
Playing with State MonadDavid Galichet
 

Viewers also liked (9)

Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
 
전자책산업동향과 서비스 모델 (Slide Share)
전자책산업동향과 서비스 모델 (Slide Share)전자책산업동향과 서비스 모델 (Slide Share)
전자책산업동향과 서비스 모델 (Slide Share)
 
Real-World Functional Programming @ Incubaid
Real-World Functional Programming @ IncubaidReal-World Functional Programming @ Incubaid
Real-World Functional Programming @ Incubaid
 
Haskell study 15
Haskell study 15Haskell study 15
Haskell study 15
 
The HaLVM: A Simple Platform for Simple Platforms
The HaLVM: A Simple Platform for Simple PlatformsThe HaLVM: A Simple Platform for Simple Platforms
The HaLVM: A Simple Platform for Simple Platforms
 
OSv at Cassandra Summit
OSv at Cassandra SummitOSv at Cassandra Summit
OSv at Cassandra Summit
 
The monad fear
The monad fearThe monad fear
The monad fear
 
Introducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGIntroducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUG
 
Playing with State Monad
Playing with State MonadPlaying with State Monad
Playing with State Monad
 

Similar to (2015 06-16) Three Approaches to Monads

Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystifiedAlessandro Lacava
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adtsHang Zhao
 
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellMichel Rijnders
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2Hang Zhao
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsPhilip Schwarz
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Mattersromanandreg
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1Philip Schwarz
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)stasimus
 
Monad presentation scala as a category
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a categorysamthemonad
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systemsleague
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsPhilip Schwarz
 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskellJongsoo Lee
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsVasil Remeniuk
 

Similar to (2015 06-16) Three Approaches to Monads (20)

Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystified
 
Practical cats
Practical catsPractical cats
Practical cats
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adts
 
An Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using HaskellAn Introduction to Functional Programming using Haskell
An Introduction to Functional Programming using Haskell
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
Monad Fact #4
Monad Fact #4Monad Fact #4
Monad Fact #4
 
Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2
 
Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generators
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
Monad presentation scala as a category
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a category
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
Thesis PPT
Thesis PPTThesis PPT
Thesis PPT
 
Thesis
ThesisThesis
Thesis
 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskell
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami Patterns
 

Recently uploaded

Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 

Recently uploaded (20)

Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 

(2015 06-16) Three Approaches to Monads

  • 2. 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)
  • 4. In a typed language consider f,g : Float -> Float and their composition g . f : Float -> Float Example (A)
  • 5. What if we want debugging information? f : Float -> (Float, String) f(x) = (2*x, “Doubled!”) g : Float -> (Float, String) g(x) = (x/4, “Quartered!”)
  • 6. Now we can’t compose them. Yet there is an obvious “composition”: x |-> ((2*x)/4, “Doubled! Quartered!”)
  • 7. Example (B) Consider multi-valued functions 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 functions with possible failure f : Logline -> TimeStamp (Not in Logline?) g : TimeStamp -> DayOfWeek (Parsing fails?)
  • 10. 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)
  • 11. These three examples are 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 us the desired composition! Instead of g.f do (bind g).f
  • 23. 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
  • 24. 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.
  • 25. The Big Reveal Each example is an example of a monad! But what is a monad?
  • 26. For Part I we’ll define a monad as “A type extension together with an implementation of unit and bind” Ex: [] (, String) (, Bool)
  • 27. II. Monads in Haskell
  • 28. 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)
  • 30. Type Synonyms: (type) type TimeStamp = 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: data Bool = True | False data Color = Red | Blue | Green data LineType = Impression |Click |Win |Bid value constructors
  • 33. 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
  • 34. 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
  • 35. Type Classes: <<WARNING>> NOTHING LIKE CLASSES IN OO LANGUAGES! More like interfaces in OO languages...
  • 36. 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”
  • 37. In fact the Haskell REPL uses show to display results!
  • 38. 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!
  • 39.
  • 40.
  • 42. 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!
  • 43. Revisit the Examples in Part I
  • 44. Example (B) -- The List Monad instance Monad [] where return x = [x] xs >>= f = concat (map f xs) (This is in the Haskell Prelude)
  • 45.
  • 46.
  • 47.
  • 48. The Control.Monad library provides >=> for direct composition.
  • 49. Haskell do syntax and 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)
  • 51.
  • 52.
  • 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)
  • 54.
  • 55.
  • 56. Bonus Monad -- IO Monad I/O in Haskell is handled via the IO Monad. e.g. Haskell’s “print” is putStrLn :: String -> IO ()
  • 57.
  • 58. 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.
  • 59. III. Monads in Category Theory
  • 60. 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
  • 61. Ex: Set (Category of 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 (Category of Haskell Types) Objects: Haskell types Arrows: Haskell functions Note: Hask contains all types and all functions in the universe!
  • 65. 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)
  • 66. 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)
  • 67. 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”)...
  • 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 Functor is a Monad where unit : X -> P(X) join : P(P(X)) -> P(X)
  • 70. Ex: Powerset Functor is a Monad where unit : X -> P(X) unit(x) = {x} join : P(P(X)) -> P(X)
  • 71. 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
  • 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 a functor from Hask to Hask. ● Maps objects: ● Maps functions:
  • 81. Maybe is a functor from Hask to Hask. ● Maps objects: e.g. maps String to Maybe String ● Maps functions:
  • 82. 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
  • 83. In fact, Maybe is a monad from Hask to Hask. unit : X -> Maybe X join : Maybe (Maybe X) -> Maybe X
  • 84. In fact, Maybe is a monad from Hask to Hask. unit : X -> Maybe X x |-> Just x join : Maybe (Maybe X) -> Maybe X
  • 85. 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
  • 86. 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)
  • 87. 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!
  • 88. Answers ● Haskell Monad is just a programming construct ○ Doesn’t require monad laws to compile ○Typeclass could have a different name
  • 89. 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
  • 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. Fin
  • 92. 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