Monad
Design
Pattern
in Functional
Programming
Arla Zeqaj, Estela Mele
monad tutorial fallacy
1. person X doesn’t understand monads
2. person X works hard and gets monads
3. person X experiences enlightenment
4. person X gives horrible explanation of
monads to others
Let’s get into monads now…
WHAT IS A MONAD?
Structure or building block in FP that
combines functions and wraps their
return values in a type with additional
computation
Two operators:
1. to wrap a value in the monad type
2. to compose together functions that
output values of the monad type
PURPOSE OF A MONAD
To handle side effects (e.g.,
state, I/O, exceptions) in a pure
functional way.
EXAMPLE
This is a program:
f(x) = 2 * x
g(x,y) = x / y
What is to be executed first?
We only want to use functions…
EXAMPLE (cont’d)
One solution: compose functions
f(g(x,y)) (first g and then f)
The problem: some functions
might fail g(2,0), divide by 0
We have no "exceptions" in FP
(an exception is not a function)
EXAMPLE (cont’d)
Solution:
Let's allow functions to return
two kind of things
g : Real, Real -> Real | Nothing
function from two reals into (real
or nothing)
EXAMPLE (cont’d)
But functions should (to be simpler)
return only one thing!
Let's create a new type of data to be
returned, a "boxing type" that
encloses maybe a real.
Hence, we can have g : Real,Real ->
Maybe Real
EXAMPLE (cont’d)
Solution: let's have a special
function to connect/compose/link
functions.
We can, behind the scenes, adapt
the output of one function to feed
the following one.
g >>= f
PSEUDOCODE
g >>= f
1. Get g's output and inspect it
2. If it is Nothing just don't call f and
return Nothing
3. On the contrary, extract the boxed
Real and feed f with it
THE PROBLEMS WE
SOLVE:
1. Having a global state that every
function in the program can share: a
StateMonad.
2. We don't like "impure functions":
functions that yield different output
for same input. Instead we make them
to return a tagged/boxed value: IO
monad.
MONAD TYPES
1. Maybe Monad: Used for computations
that might fail.
2. List Monad: Represents non-
deterministic computations.
3. IO Monad: Handles input/output
operations.
HOW DO THEY WORK?
Monads can be thought as wrappers.
The wrapper can be represented as a
box around something.
We can put a value in the box and can
apply a transformation on the value
inside the box.
HOW DO THEY WORK?
A monad is anything with a constructor
and a flatMap method and it’s a
mechanism for sequencing
computations.
You can call the flatmap() function on
the monad to put something inside the
box.
MONAD LAWS
Left Right Associativi
Identity Identity ty
`return a >>= f` `m >>= return` `(m >>= f) >>= g`
is the same as is the same as is the same as
`f a` `m` `m >>= (\x -> f x
>>= g)`
A MAYBE MONAD
data Maybe a = Nothing | Just
a
Nothing: Represents a failure or
absence of value
Just a: Represents a success with a
value a
HASKELL IMPLEMENTATION
HASKELL IMPLEMENTATION
CONCLUSION
We need monads because they are
proven to be a tool that enables
more composition in code,
resulting in encouraging one of the
holy grails of programming:
REUSABLE CODE