What are monads?
@jlgarhdez
slides
But rst...
What is functional programming?
Functional programming!
A programming paradigm that:
treats functions as rst class citicens
encourages immutability
avoids global state
makes your code honest
Avoids side effects (encourages purity)
What are side effects?
Everything that your code does, apart of operating
with values.
How can we implement this?
public int sum(Int a, Int b) {
// ???
}
public int sum(int a, int b) {
launchMissiles(); // Side effect!
return a + b;
}
public int sum(int a, int b) {
this.pass = 4; // Side effect!
return a + b;
}
public int sum(int a, int b) {
throw new Exception("message"); // Side effect!
return a + b;
}
More bits of FP
Imagine this
class IO {
def read(): String = readLine()// reads from standard input
def write(text: String): Unit = println(text)// writes to stan
}
you could use it in the following way:
def hello(): String = {
write("what is your name?")
val name = read()
val hello = "hello" + name
write(hello)
return hello
}
The good part:
That that code is idiomatic and concise!
The bad part:
That code is not pure! (has side effects)
How can we x this?
Transform this
class IO {
def read(): String = readLine()
def write(text: String): Unit = println(text)
}
Into this!
This is our small IO language
trait IO[A]
case class Read() extends IO[String]
case class Write(str: String) extends IO[Unit]
This kind of construction is called Algebraic Data
Type.
Interpreter
Since our previous ADT is not effectful, meaning that
can not execute side effects, we need its companion,
the interpreter!
def interpret(op: IO[A]): A = op match {
case Read() => readLine()
case Write(text) => printLn(text)
}
Now, let's write our hello
function!
def pureHello(): IO[String] = {
Write("what is your name?")
val name: IO[String] = Read()
Write("hello" + name) // ERROR!, you can not concat
// String and IO[String]
}
How can we x it?
Lets add a way to sequence IO operations to our
ADT!
trait IO[A]
case class Read() extends IO[String]
case class Write(str: String) extends IO[Unit]
case class Sequence[A, B](
fa: IO[A],
fn: A => IO[B])
extends IO[B]
Can we write hello now?
def pureHello: IO[String] = {
Sequence(
Write("What is your name?"),
(_) => {
Sequence(
Read(),
(name) => {
Write("hello, " + name)
}
)
})
}
We can't! we are returning an IO[Unit] , because of
the last Write() ... Lets x that!
Add another case to our ADT
Let's add a way to put values inside IO
trait IO[A]
case class Read() extends IO[String]
case class Write(str: String) extends IO[Unit]
case class Sequence[A, B](
fa: IO[A],
fn: A => IO[B]) extends IO[B]
case class Point[A](a: A) extends IO[A]
Finally, we can write it now!
def pureHello: IO[String] = {
Sequence(
Write("What is your name?"),
(_) => {
Sequence(
Read(),
(name) => {
Sequence(
Write("hello, " + name),
(_) => {
Point("hello, " + name)
}
)
}
)
})
}
The good parts
That code is pure!
The bad parts
THAT CODE IS UGLY
Let's x that!
Introducing TypeClasses
Typeclasses are a way to give more behaviour to an
class. You can think of them like interfaces in OOP.
But better
Much better
Some typeclasses
trait Eq[A] {
def equals(a: A, b: A): Bool
}
val intEquality = new Eq[Int] {
def equals(a: Int, b: Int) = a == b
}
trait Show[A] {
def show(a: A): String
}
val showInt = new ToString[Int] {
def show(a: Int) = a.toString
}
Guess what?
Monad is a typeclass!
What are monads?
What are monads?
Monad
trait Monad[M[_]] {
def point[A](a: A): M[A]
def flatMap[A, B](ma: M[A])(fn: A => M[B]): M[B]
}
What are monads?
Monad is a typeclass for de ning imperative
languages.
Back to our IO example
Remember all the weirdness we needed to do in
order to do our pureHello function?
We are really close to simplifying it a lot!
Let's create a Monad instance for our IO language
rst!
val ioMonad = new Monad[IO] {
def point[A](a: A): IO[A] = Point(a)
def flatMap[A, B](ma: IO[A])(fn: A => IO[B]): IO[B] =
Sequence(ma, fn)
}
Now, let's rewrite our
pureHello
def pureHello: IO[String] = for {
_ <- Write("What's your name?")
name <- Read()
_ <- Write("Hello, " + name)
} yield "Hello, " + name
We only need to review our interpreter...
New interpreter
def ioInterp[A](op: IO[A]): A = op match {
case Write(text) => println(text)
case Read() => readLine()
case Point(x) => x
case Sequence(x, fn) => ioInterp(fn(ioInterp(x)))
}
And, combine!
ioInterp(pureHello()) // this actually does IO
// thanks to the interpreter
Small recap
Functional programming is cool
Typeclasses are here to help
Monad is for imperative programming
Monads + Interpreters = WIN!
We are done,
thanks!
@jlgarhdez
meetup.com/FP-Madrid/
meetup.com/Haskell-MAD/

More Related Content

PPTX
Introduction to C++
PPTX
Introduction to c++
PPT
JAVA Variables and Operators
PPTX
Polymorphism in c++(ppt)
PDF
Introduction to cpp
PDF
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Introduction to C++
Introduction to c++
JAVA Variables and Operators
Polymorphism in c++(ppt)
Introduction to cpp
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]

What's hot (20)

PPTX
Python programming workshop session 1
PPTX
C Theory
PDF
C++ book
DOCX
Diff between c and c++
PPTX
Learning C++ - Introduction to c++ programming 1
PPTX
Intro to c++
PPT
C++: inheritance, composition, polymorphism
PPT
Python Part 1
PPTX
Virtual function in C++ Pure Virtual Function
PPT
Lecture01
PPT
Java 8 - CJ
PPTX
Python workshop session 6
PPT
Introduction to C++
PPTX
C introduction by thooyavan
PDF
01. introduction to C++
PPTX
Python programming workshop session 3
PPTX
Python programming workshop session 2
PPTX
Polymorphism in C++
PPT
PDF
Lecture 3 getting_started_with__c_
Python programming workshop session 1
C Theory
C++ book
Diff between c and c++
Learning C++ - Introduction to c++ programming 1
Intro to c++
C++: inheritance, composition, polymorphism
Python Part 1
Virtual function in C++ Pure Virtual Function
Lecture01
Java 8 - CJ
Python workshop session 6
Introduction to C++
C introduction by thooyavan
01. introduction to C++
Python programming workshop session 3
Python programming workshop session 2
Polymorphism in C++
Lecture 3 getting_started_with__c_
Ad

Similar to What are monads? (20)

PDF
Simple IO Monad in 'Functional Programming in Scala'
PPTX
Functional programming with FSharp
PDF
PPTX
Functional IO and Effects
PDF
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
PDF
Scalapeno18 - Thinking Less with Scala
PDF
pure-functional-programming.pdf
PPTX
Introduction to Python Programming
PDF
Kotlin Austin Droids April 14 2016
PPTX
Kotlin as a Better Java
PPTX
Introduction to Kotlin.pptx
PPTX
01 Introduction to Kotlin - Programming in Kotlin.pptx
PDF
Monoids - Part 1 - with examples using Scalaz and Cats
PDF
Drinking the free kool-aid
PDF
Talk - Query monad
PDF
Swift, swiftly
PPT
Python Programming Introduction demo.ppt
PDF
Erlang session1
PDF
Introduction to Python Programming | InsideAIML
PPTX
Introduction to F#
Simple IO Monad in 'Functional Programming in Scala'
Functional programming with FSharp
Functional IO and Effects
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Scalapeno18 - Thinking Less with Scala
pure-functional-programming.pdf
Introduction to Python Programming
Kotlin Austin Droids April 14 2016
Kotlin as a Better Java
Introduction to Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx
Monoids - Part 1 - with examples using Scalaz and Cats
Drinking the free kool-aid
Talk - Query monad
Swift, swiftly
Python Programming Introduction demo.ppt
Erlang session1
Introduction to Python Programming | InsideAIML
Introduction to F#
Ad

Recently uploaded (20)

PPTX
MLforCyber_MLDataSetsandFeatures_Presentation.pptx
PPTX
Human-Computer Interaction for Lecture 2
PDF
Cloud Native Aachen Meetup - Aug 21, 2025
PDF
SOFTWARE ENGINEERING Software Engineering (3rd Edition) by K.K. Aggarwal & Yo...
PDF
Internet Download Manager IDM Crack powerful download accelerator New Version...
PDF
CapCut PRO for PC Crack New Download (Fully Activated 2025)
PPTX
Chapter 1 - Transaction Processing and Mgt.pptx
PPTX
R-Studio Crack Free Download 2025 Latest
PDF
AI Guide for Business Growth - Arna Softech
PDF
Microsoft Office 365 Crack Download Free
PDF
Sun and Bloombase Spitfire StoreSafe End-to-end Storage Security Solution
PPTX
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
PPTX
Viber For Windows 25.7.1 Crack + Serial Keygen
PDF
Website Design & Development_ Professional Web Design Services.pdf
DOCX
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
PDF
E-Commerce Website Development Companyin india
PPTX
Lecture 5 Software Requirement Engineering
PDF
What Makes a Great Data Visualization Consulting Service.pdf
PPTX
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
PPTX
Presentation by Samna Perveen And Subhan Afzal.pptx
MLforCyber_MLDataSetsandFeatures_Presentation.pptx
Human-Computer Interaction for Lecture 2
Cloud Native Aachen Meetup - Aug 21, 2025
SOFTWARE ENGINEERING Software Engineering (3rd Edition) by K.K. Aggarwal & Yo...
Internet Download Manager IDM Crack powerful download accelerator New Version...
CapCut PRO for PC Crack New Download (Fully Activated 2025)
Chapter 1 - Transaction Processing and Mgt.pptx
R-Studio Crack Free Download 2025 Latest
AI Guide for Business Growth - Arna Softech
Microsoft Office 365 Crack Download Free
Sun and Bloombase Spitfire StoreSafe End-to-end Storage Security Solution
DevOpsDays Halifax 2025 - Building 10x Organizations Using Modern Productivit...
Viber For Windows 25.7.1 Crack + Serial Keygen
Website Design & Development_ Professional Web Design Services.pdf
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
E-Commerce Website Development Companyin india
Lecture 5 Software Requirement Engineering
What Makes a Great Data Visualization Consulting Service.pdf
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
Presentation by Samna Perveen And Subhan Afzal.pptx

What are monads?

  • 3. What is functional programming?
  • 4. Functional programming! A programming paradigm that: treats functions as rst class citicens encourages immutability avoids global state makes your code honest Avoids side effects (encourages purity)
  • 5. What are side effects? Everything that your code does, apart of operating with values.
  • 6. How can we implement this? public int sum(Int a, Int b) { // ??? }
  • 7. public int sum(int a, int b) { launchMissiles(); // Side effect! return a + b; } public int sum(int a, int b) { this.pass = 4; // Side effect! return a + b; } public int sum(int a, int b) { throw new Exception("message"); // Side effect! return a + b; }
  • 9. Imagine this class IO { def read(): String = readLine()// reads from standard input def write(text: String): Unit = println(text)// writes to stan } you could use it in the following way: def hello(): String = { write("what is your name?") val name = read() val hello = "hello" + name write(hello) return hello }
  • 10. The good part: That that code is idiomatic and concise! The bad part: That code is not pure! (has side effects)
  • 11. How can we x this?
  • 12. Transform this class IO { def read(): String = readLine() def write(text: String): Unit = println(text) }
  • 13. Into this! This is our small IO language trait IO[A] case class Read() extends IO[String] case class Write(str: String) extends IO[Unit] This kind of construction is called Algebraic Data Type.
  • 14. Interpreter Since our previous ADT is not effectful, meaning that can not execute side effects, we need its companion, the interpreter! def interpret(op: IO[A]): A = op match { case Read() => readLine() case Write(text) => printLn(text) }
  • 15. Now, let's write our hello function! def pureHello(): IO[String] = { Write("what is your name?") val name: IO[String] = Read() Write("hello" + name) // ERROR!, you can not concat // String and IO[String] }
  • 16. How can we x it? Lets add a way to sequence IO operations to our ADT! trait IO[A] case class Read() extends IO[String] case class Write(str: String) extends IO[Unit] case class Sequence[A, B]( fa: IO[A], fn: A => IO[B]) extends IO[B]
  • 17. Can we write hello now? def pureHello: IO[String] = { Sequence( Write("What is your name?"), (_) => { Sequence( Read(), (name) => { Write("hello, " + name) } ) }) } We can't! we are returning an IO[Unit] , because of the last Write() ... Lets x that!
  • 18. Add another case to our ADT Let's add a way to put values inside IO trait IO[A] case class Read() extends IO[String] case class Write(str: String) extends IO[Unit] case class Sequence[A, B]( fa: IO[A], fn: A => IO[B]) extends IO[B] case class Point[A](a: A) extends IO[A]
  • 19. Finally, we can write it now! def pureHello: IO[String] = { Sequence( Write("What is your name?"), (_) => { Sequence( Read(), (name) => { Sequence( Write("hello, " + name), (_) => { Point("hello, " + name) } ) } ) }) }
  • 20. The good parts That code is pure! The bad parts THAT CODE IS UGLY
  • 22. Introducing TypeClasses Typeclasses are a way to give more behaviour to an class. You can think of them like interfaces in OOP. But better Much better
  • 23. Some typeclasses trait Eq[A] { def equals(a: A, b: A): Bool } val intEquality = new Eq[Int] { def equals(a: Int, b: Int) = a == b } trait Show[A] { def show(a: A): String } val showInt = new ToString[Int] { def show(a: Int) = a.toString }
  • 25. Monad is a typeclass!
  • 28. Monad trait Monad[M[_]] { def point[A](a: A): M[A] def flatMap[A, B](ma: M[A])(fn: A => M[B]): M[B] }
  • 29. What are monads? Monad is a typeclass for de ning imperative languages.
  • 30. Back to our IO example Remember all the weirdness we needed to do in order to do our pureHello function? We are really close to simplifying it a lot! Let's create a Monad instance for our IO language rst! val ioMonad = new Monad[IO] { def point[A](a: A): IO[A] = Point(a) def flatMap[A, B](ma: IO[A])(fn: A => IO[B]): IO[B] = Sequence(ma, fn) }
  • 31. Now, let's rewrite our pureHello def pureHello: IO[String] = for { _ <- Write("What's your name?") name <- Read() _ <- Write("Hello, " + name) } yield "Hello, " + name We only need to review our interpreter...
  • 32. New interpreter def ioInterp[A](op: IO[A]): A = op match { case Write(text) => println(text) case Read() => readLine() case Point(x) => x case Sequence(x, fn) => ioInterp(fn(ioInterp(x))) }
  • 33. And, combine! ioInterp(pureHello()) // this actually does IO // thanks to the interpreter
  • 34. Small recap Functional programming is cool Typeclasses are here to help Monad is for imperative programming Monads + Interpreters = WIN!