SlideShare a Scribd company logo
March 20, 2018
Why Monads?
Luca Belli

Twitter Cortex
Luca Belli, Ph.D.
Senior Software Engineer at Twitter Cortex
@__lucab
• studied math a bunch of years
• lived in the area for 3 winters ❄
• moved to sunny California to do Machine Learning
Deck title
About me
X
6
but why?
8
COMPOSITION
9
game :: Maybe Deck
game = case addCard 5 0 of -- returns a Maybe Deck
Nothing -> Nothing
Just deck1 -> case addCard 4 deck1 of -- returns a Maybe Deck
Nothing -> Nothing
Just deck2 -> case addCard 3 deck2 of -- returns a Maybe Deck
Nothing -> Nothing
Just deck3 -> addCard 2 deck3 -- returns a Maybe Deck
Haskell Primer
10
game = emptyDeck >>= addFirstCard >>=
addSecondCard >>= addThirdCard
Haskell Primer
11
1
2
3
Haskell
Functor
Monads (Functor++)
Agenda
13
-- define a function of one variable
f :: Int -> Int
f x = x + 1
λ> f 2
3
Haskell Primer
14
-- two variables
g :: Int -> Int -> Int
g x y = x + y
λ> g 1 2
3
Haskell Primer
15
-- currying is free!
g :: Int -> Int -> Int
g x y = x + y
f :: Int -> Int
f y = y + 1
f = g 1
Haskell Primer
16
g :: Str -> Int
g x = length x
f :: Int -> Int
f x = x + 1
Haskell Primer
17
g :: Str -> Int
g x = length x
f :: Int -> Int
f x = x + 1
h :: Str -> Int
h x = f(g(x)) = f (g x) = (f . g) x
Haskell Primer
18
data Maybe a = Just a | Nothing
Haskell Primer
Represent failure
19
data Maybe a = Just a | Nothing
-- non secure connections are discarded
gMaybe :: Str -> Maybe Int
gMaybe str
| "https" `isPrefixOf` str = Just (length str)
| otherwise = Nothing
Haskell Primer
Variation on g
20
-- can we compose with f now?
gMaybe :: Str -> Maybe Int
f :: Int -> Int
f x = x + 1
Haskell Primer
21
-- let's try to change f
fMaybe :: Maybe Int -> Int
Haskell Primer
Adapting f
22
-- let's try to change f
fMaybe :: Maybe Int -> Int
fMaybe Just x = f x
Haskell Primer
Adapting f
23
-- let's try to change f
fMaybe :: Maybe Int -> Int
fMaybe Just x = f x
fMaybe Nothing = ??? -- 0? Infinity?
Haskell Primer
Adapting f
24
-- f :: Int -> Int
fMaybe :: Maybe Int -> Maybe Int
fMaybe Just x = Just (f x)
fMaybe Nothing = Nothing
Haskell Primer
25
gList :: Str -> [Int]
Haskell Primer
List
26
gList :: Str -> [Int]
fList :: [Int] -> Int
Haskell Primer
27
gList :: Str -> [Int]
fList :: [Int] -> Int
fList [] = ???
Haskell Primer
28
gList :: Str -> [Int]
fList :: [Int] -> Int
fList [] = ???
fList x:xs = ???
-- what do I do with those?
Haskell Primer
29
gList :: Str -> [Int]
fList :: [Int] -> [Int]
fList [] = []
fList xs = map f xs
Haskell Primer
Last example
30
g :: Str -> Int
f :: Int -> Int
gMaybe :: Str -> Maybe Int
fMaybe :: Maybe Int -> Maybe Int
Haskell Primer
31
g :: Str -> Int
f :: Int -> Int
gList :: Str -> [Int]
fList :: [Int] -> [Int]
Haskell Primer
32
• composed 2 functions f(g(x))
Recap
33
• composed 2 functions f(g(x))
• changed g to return enhanced types

• had to change f accordingly
Recap
34
• composed 2 functions f(g(x))
• changed g to return enhanced types

• had to change f accordingly
Recap
35
why f?
36
class Functor F where
map :: (a -> b) -> F a -> F b
Haskell Primer
Abstracting the pattern
37
class Functor F where
map :: (a -> b) -> (F a -> F b)
Haskell Primer
Abstracting the pattern
38
-- map :: (a -> b) -> F a -> F b
-- map :: (a -> b) -> Maybe a -> Maybe b
Haskell Primer
Abstracting the pattern
39
-- map :: (a -> b) -> F a -> F b
-- map :: (a -> b) -> Maybe a -> Maybe b
instance Functor Maybe where
map f Nothing = Nothing
Haskell Primer
Abstracting the pattern
40
-- map :: (a -> b) -> F a -> F b
-- map :: (a -> b) -> Maybe a -> Maybe b
instance Functor Maybe where
map f Nothing = Nothing
map f (Just x) = Just (f x)
Haskell Primer
Abstracting the pattern
41
-- map :: (a -> b) -> F a -> F b
-- map :: (a -> b) -> Maybe a -> Maybe b
instance Functor Maybe where
map f Nothing = Nothing
map f (Just x) = Just (f x)
fMaybe = map f
Haskell Primer
Abstracting the pattern
42
-- map :: (a -> b) -> F a -> F b
-- map :: (a -> b) -> [a] -> [b]
Haskell Primer
Abstracting the pattern
43
-- map :: (a -> b) -> F a -> F b
-- map :: (a -> b) -> [a] -> [b]
instance Functor List where
map _ [] = []
Haskell Primer
Abstracting the pattern
44
-- map :: (a -> b) -> F a -> F b
-- map :: (a -> b) -> [a] -> [b]
instance Functor List where
map _ [] = []
map f x:xs = f x : map xs
Haskell Primer
Abstracting the pattern
45
-- map :: (a -> b) -> F a -> F b
-- map :: (a -> b) -> [a] -> [b]
instance Functor List where
map _ [] = []
map f x:xs = f x : map xs
fList = map f
Haskell Primer
Abstracting the pattern
46
• enhanced type lift functions

• composition is possible via

map f
Recap
47
type Deck = Int
type Card = Int
Monads
Let’s play a game
48
type Deck = Int
type Card = Int
addCard :: Card -> Deck -> Deck
Let’s play a game
Monads
49
type Deck = Int
type Card = Int
addCard :: Card -> Deck -> Deck
addCard c d = c + d
Let’s play a game
Monads
50
-- Let's start a game:
oneCard :: Deck
oneCard = addCard 5 0
Monads
51
twoCard = addCard 4 oneCard
-- equivalent to
addCard 4 (addCard 5 0)
Monads
52
-- what happens if we add more?
addCard 2 (addCard 3 (addCard 4 (addCard
5 0 )))
Haskell Primer
The whole game
53
addCard :: Card -> Deck -> Maybe Deck
addCard c d
| c + d < 10 = Just (c + d)
| otherwise = Nothing -- you lose
Let’s make the game more interesting
Monads
54
addCard :: Card -> Deck -> Maybe Deck
oneCard :: Maybe Deck
oneCard = addCard 5 0
Monads
55
addCard :: Card -> Deck -> Maybe Deck
oneCard :: Maybe Deck
oneCard = addCard 5 0
-- Deck != Maybe Deck
addCard 3 oneCard
Monads
56
game :: Maybe Deck
game = case addCard 5 0 of -- returns a Maybe Deck
Nothing -> Nothing
Just deck1 -> case addCard 4 deck1 of -- returns a Maybe Deck
Nothing -> Nothing
Just deck2 -> case addCard 3 deck2 of -- returns a Maybe Deck
Nothing -> Nothing
Just deck3 -> addCard 2 deck3 -- returns a Maybe Deck
Monads
58
addFirstCard :: Deck -> Maybe Deck
addFirstCard = addCard 5
addSecondCard :: Deck -> Maybe Deck
addSecondCard = addCard 4
addThirdCard :: Deck -> Maybe Deck
addThirdCard = addCard 3
Monads
59
Maybe Deck -> -- previous total
(Deck -> Maybe Deck) -> -- addCard
Maybe Deck -- new total
Monads
60
(>>=) :: Maybe Deck -> -- previous total
(Deck -> Maybe Deck) -> -- addCard
Maybe Deck -- new total
Monads
61
emptyDeck :: Maybe Deck
addCard 5 :: Deck -> Maybe Deck
emptyDeck >>= addCard 5
Monads
62
game = emptyDeck >>= addFirstCard >>=
addSecondCard >>= addThirdCard
Monads
63
-- doesn’t look right
emptyDeck = addCard 0 0
Monads
64
-- the minimal context that contains a 0
emptyDeck = return 0
Monads
65
Monad = Functor +
return +
(>>=)
66
(>>=) :: Maybe a ->
(a -> Maybe b)
-> Maybe b
Monads
67
(>>=) :: Maybe a ->
(a -> Maybe b)
-> Maybe b
instance Monad Maybe where
return x = Just x
Monads
68
(>>=) :: Maybe a ->
(a -> Maybe b)
-> Maybe b
instance Monad Maybe where
return x = Just x
Nothing >>= _ = Nothing
Monads
69
(>>=) :: Maybe a ->
(a -> Maybe b)
-> Maybe b
instance Monad Maybe where
return x = Just x
Nothing >>= _ = Nothing
(Just x) >>= f = f x
Monads
70
(>>=) :: [a] -> (a -> [b]) -> [b]
What about Lists?
Monads
71
(>>=) :: [a] -> (a -> [b]) -> [b]
(>>=) = flatMap
Haskell Primer
What about Lists?
72
(>>=) :: [a] -> (a -> [b]) -> [b]
(>>=) = flatMap
instance Monad List where
return x = [x]
Haskell Primer
What about Lists?
73
(>>=) :: [a] -> (a -> [b]) -> [b]
(>>=) = flatMap
instance Monad List where
return x = [x]
xs >>= f = concat (map f xs)
Haskell Primer
What about Lists?
74
g :: a -> Maybe b
f :: b -> Maybe c
Recap
Recap
75
g :: a -> Maybe b
f :: b -> Maybe c
(>>=) :: m b -> (b -> m c) -> m c
extracts the b in the output of g and feeds it
to f
Recap
Recap
76
COMPOSITION
Luca Belli
March 20, 2018
Thank you!
@__lucab

More Related Content

What's hot

Excel function
Excel functionExcel function
Excel function
SirajRock
 
FEAL - CSAW CTF 2014 Quals Crypto300
FEAL - CSAW CTF 2014 Quals Crypto300FEAL - CSAW CTF 2014 Quals Crypto300
FEAL - CSAW CTF 2014 Quals Crypto300
YOKARO-MON
 
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
 
5 4 function notation
5 4 function notation5 4 function notation
5 4 function notationhisema01
 
Higher formal homeworks unit 1
Higher formal homeworks   unit 1Higher formal homeworks   unit 1
Higher formal homeworks unit 1
sjamaths
 
Monads from Definition
Monads from DefinitionMonads from Definition
Monads from Definition
Dierk König
 
June Overview SN5 Math
June Overview SN5 MathJune Overview SN5 Math
June Overview SN5 Mathamcsquared
 
Cs101 endsem 2014
Cs101 endsem 2014Cs101 endsem 2014
Cs101 endsem 2014
RamKumar42580
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data Analysis
SpbDotNet Community
 
MATHS SYMBOLS - EXPONENTIALS + LOGARITHMS and THEIR PROPERTIES
MATHS SYMBOLS - EXPONENTIALS + LOGARITHMS and THEIR PROPERTIESMATHS SYMBOLS - EXPONENTIALS + LOGARITHMS and THEIR PROPERTIES
MATHS SYMBOLS - EXPONENTIALS + LOGARITHMS and THEIR PROPERTIES
Ist. Superiore Marini-Gioia - Enzo Exposyto
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
Namgee Lee
 
Go Containers
Go ContainersGo Containers
Go Containers
jgrahamc
 
133467 p1a9
133467 p1a9133467 p1a9
Chapter2
Chapter2Chapter2
Chapter2
Krishna Kumar
 
Fields in cryptography
Fields in cryptographyFields in cryptography
Fields in cryptography
LekhikaShishodia
 
Scala kansai summit-2016
Scala kansai summit-2016Scala kansai summit-2016
Scala kansai summit-2016
Naoki Kitora
 

What's hot (18)

Excel function
Excel functionExcel function
Excel function
 
FEAL - CSAW CTF 2014 Quals Crypto300
FEAL - CSAW CTF 2014 Quals Crypto300FEAL - CSAW CTF 2014 Quals Crypto300
FEAL - CSAW CTF 2014 Quals Crypto300
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
5 4 function notation
5 4 function notation5 4 function notation
5 4 function notation
 
Higher formal homeworks unit 1
Higher formal homeworks   unit 1Higher formal homeworks   unit 1
Higher formal homeworks unit 1
 
F(x) terminology
F(x) terminologyF(x) terminology
F(x) terminology
 
Monads from Definition
Monads from DefinitionMonads from Definition
Monads from Definition
 
June Overview SN5 Math
June Overview SN5 MathJune Overview SN5 Math
June Overview SN5 Math
 
Cs101 endsem 2014
Cs101 endsem 2014Cs101 endsem 2014
Cs101 endsem 2014
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data Analysis
 
MATHS SYMBOLS - EXPONENTIALS + LOGARITHMS and THEIR PROPERTIES
MATHS SYMBOLS - EXPONENTIALS + LOGARITHMS and THEIR PROPERTIESMATHS SYMBOLS - EXPONENTIALS + LOGARITHMS and THEIR PROPERTIES
MATHS SYMBOLS - EXPONENTIALS + LOGARITHMS and THEIR PROPERTIES
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 
Chapter2
Chapter2Chapter2
Chapter2
 
Go Containers
Go ContainersGo Containers
Go Containers
 
133467 p1a9
133467 p1a9133467 p1a9
133467 p1a9
 
Chapter2
Chapter2Chapter2
Chapter2
 
Fields in cryptography
Fields in cryptographyFields in cryptography
Fields in cryptography
 
Scala kansai summit-2016
Scala kansai summit-2016Scala kansai summit-2016
Scala kansai summit-2016
 

Similar to TypeLevel Summit

CH04.ppt
CH04.pptCH04.ppt
Class XII CBSE Mathematics Sample question paper with solution
Class XII CBSE Mathematics Sample question paper with solutionClass XII CBSE Mathematics Sample question paper with solution
Class XII CBSE Mathematics Sample question paper with solution
Pratima Nayak ,Kendriya Vidyalaya Sangathan
 
functions limits and continuity
functions limits and continuityfunctions limits and continuity
functions limits and continuity
Pume Ananda
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Bryan O'Sullivan
 
Cryptography and Network Security chapter 4.ppt
Cryptography and Network Security chapter 4.pptCryptography and Network Security chapter 4.ppt
Cryptography and Network Security chapter 4.ppt
the9amit
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
romanandreg
 
Matematica
MatematicaMatematica
5 algebra of functions
5 algebra of functions5 algebra of functions
5 algebra of functions
Tzenma
 
10.5 more on language of functions x
10.5 more on language of functions x10.5 more on language of functions x
10.5 more on language of functions x
math260
 
1.5 notation and algebra of functions
1.5 notation and algebra of functions1.5 notation and algebra of functions
1.5 notation and algebra of functionsmath123c
 
FUNCTIONS .pdf
FUNCTIONS .pdfFUNCTIONS .pdf
FUNCTIONS .pdf
Alison Tutors
 
{- Do not change the skeleton code! The point of this assign.pdf
{-      Do not change the skeleton code! The point of this      assign.pdf{-      Do not change the skeleton code! The point of this      assign.pdf
{- Do not change the skeleton code! The point of this assign.pdf
atul2867
 
Colour in Mathematics
Colour in Mathematics Colour in Mathematics
Colour in Mathematics
Colleen Young
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
akaptur
 
Demystifying Zero Knowledge Proofs [FINAL].pptx
Demystifying Zero Knowledge Proofs [FINAL].pptxDemystifying Zero Knowledge Proofs [FINAL].pptx
Demystifying Zero Knowledge Proofs [FINAL].pptx
RedWhite12
 
Higher Maths 2.1.2 - Quadratic Functions
Higher Maths 2.1.2 - Quadratic FunctionsHigher Maths 2.1.2 - Quadratic Functions
Higher Maths 2.1.2 - Quadratic Functionstimschmitz
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
faradjpour
 

Similar to TypeLevel Summit (20)

CH04.ppt
CH04.pptCH04.ppt
CH04.ppt
 
Class XII CBSE Mathematics Sample question paper with solution
Class XII CBSE Mathematics Sample question paper with solutionClass XII CBSE Mathematics Sample question paper with solution
Class XII CBSE Mathematics Sample question paper with solution
 
Ch04
Ch04Ch04
Ch04
 
functions limits and continuity
functions limits and continuityfunctions limits and continuity
functions limits and continuity
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
 
Cryptography and Network Security chapter 4.ppt
Cryptography and Network Security chapter 4.pptCryptography and Network Security chapter 4.ppt
Cryptography and Network Security chapter 4.ppt
 
Functions limits and continuity
Functions limits and continuityFunctions limits and continuity
Functions limits and continuity
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Matematica
MatematicaMatematica
Matematica
 
5 algebra of functions
5 algebra of functions5 algebra of functions
5 algebra of functions
 
10.5 more on language of functions x
10.5 more on language of functions x10.5 more on language of functions x
10.5 more on language of functions x
 
1.5 notation and algebra of functions
1.5 notation and algebra of functions1.5 notation and algebra of functions
1.5 notation and algebra of functions
 
FUNCTIONS .pdf
FUNCTIONS .pdfFUNCTIONS .pdf
FUNCTIONS .pdf
 
{- Do not change the skeleton code! The point of this assign.pdf
{-      Do not change the skeleton code! The point of this      assign.pdf{-      Do not change the skeleton code! The point of this      assign.pdf
{- Do not change the skeleton code! The point of this assign.pdf
 
Colour in Mathematics
Colour in Mathematics Colour in Mathematics
Colour in Mathematics
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
 
Demystifying Zero Knowledge Proofs [FINAL].pptx
Demystifying Zero Knowledge Proofs [FINAL].pptxDemystifying Zero Knowledge Proofs [FINAL].pptx
Demystifying Zero Knowledge Proofs [FINAL].pptx
 
Integration
IntegrationIntegration
Integration
 
Higher Maths 2.1.2 - Quadratic Functions
Higher Maths 2.1.2 - Quadratic FunctionsHigher Maths 2.1.2 - Quadratic Functions
Higher Maths 2.1.2 - Quadratic Functions
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 

Recently uploaded

Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
top1002
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 

Recently uploaded (20)

Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 

TypeLevel Summit

  • 1. March 20, 2018 Why Monads? Luca Belli Twitter Cortex
  • 2. Luca Belli, Ph.D. Senior Software Engineer at Twitter Cortex @__lucab • studied math a bunch of years • lived in the area for 3 winters ❄ • moved to sunny California to do Machine Learning Deck title About me X
  • 3.
  • 4.
  • 5.
  • 7.
  • 9. 9 game :: Maybe Deck game = case addCard 5 0 of -- returns a Maybe Deck Nothing -> Nothing Just deck1 -> case addCard 4 deck1 of -- returns a Maybe Deck Nothing -> Nothing Just deck2 -> case addCard 3 deck2 of -- returns a Maybe Deck Nothing -> Nothing Just deck3 -> addCard 2 deck3 -- returns a Maybe Deck Haskell Primer
  • 10. 10 game = emptyDeck >>= addFirstCard >>= addSecondCard >>= addThirdCard Haskell Primer
  • 12.
  • 13. 13 -- define a function of one variable f :: Int -> Int f x = x + 1 λ> f 2 3 Haskell Primer
  • 14. 14 -- two variables g :: Int -> Int -> Int g x y = x + y λ> g 1 2 3 Haskell Primer
  • 15. 15 -- currying is free! g :: Int -> Int -> Int g x y = x + y f :: Int -> Int f y = y + 1 f = g 1 Haskell Primer
  • 16. 16 g :: Str -> Int g x = length x f :: Int -> Int f x = x + 1 Haskell Primer
  • 17. 17 g :: Str -> Int g x = length x f :: Int -> Int f x = x + 1 h :: Str -> Int h x = f(g(x)) = f (g x) = (f . g) x Haskell Primer
  • 18. 18 data Maybe a = Just a | Nothing Haskell Primer Represent failure
  • 19. 19 data Maybe a = Just a | Nothing -- non secure connections are discarded gMaybe :: Str -> Maybe Int gMaybe str | "https" `isPrefixOf` str = Just (length str) | otherwise = Nothing Haskell Primer Variation on g
  • 20. 20 -- can we compose with f now? gMaybe :: Str -> Maybe Int f :: Int -> Int f x = x + 1 Haskell Primer
  • 21. 21 -- let's try to change f fMaybe :: Maybe Int -> Int Haskell Primer Adapting f
  • 22. 22 -- let's try to change f fMaybe :: Maybe Int -> Int fMaybe Just x = f x Haskell Primer Adapting f
  • 23. 23 -- let's try to change f fMaybe :: Maybe Int -> Int fMaybe Just x = f x fMaybe Nothing = ??? -- 0? Infinity? Haskell Primer Adapting f
  • 24. 24 -- f :: Int -> Int fMaybe :: Maybe Int -> Maybe Int fMaybe Just x = Just (f x) fMaybe Nothing = Nothing Haskell Primer
  • 25. 25 gList :: Str -> [Int] Haskell Primer List
  • 26. 26 gList :: Str -> [Int] fList :: [Int] -> Int Haskell Primer
  • 27. 27 gList :: Str -> [Int] fList :: [Int] -> Int fList [] = ??? Haskell Primer
  • 28. 28 gList :: Str -> [Int] fList :: [Int] -> Int fList [] = ??? fList x:xs = ??? -- what do I do with those? Haskell Primer
  • 29. 29 gList :: Str -> [Int] fList :: [Int] -> [Int] fList [] = [] fList xs = map f xs Haskell Primer Last example
  • 30. 30 g :: Str -> Int f :: Int -> Int gMaybe :: Str -> Maybe Int fMaybe :: Maybe Int -> Maybe Int Haskell Primer
  • 31. 31 g :: Str -> Int f :: Int -> Int gList :: Str -> [Int] fList :: [Int] -> [Int] Haskell Primer
  • 32. 32 • composed 2 functions f(g(x)) Recap
  • 33. 33 • composed 2 functions f(g(x)) • changed g to return enhanced types • had to change f accordingly Recap
  • 34. 34 • composed 2 functions f(g(x)) • changed g to return enhanced types • had to change f accordingly Recap
  • 36. 36 class Functor F where map :: (a -> b) -> F a -> F b Haskell Primer Abstracting the pattern
  • 37. 37 class Functor F where map :: (a -> b) -> (F a -> F b) Haskell Primer Abstracting the pattern
  • 38. 38 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> Maybe a -> Maybe b Haskell Primer Abstracting the pattern
  • 39. 39 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> Maybe a -> Maybe b instance Functor Maybe where map f Nothing = Nothing Haskell Primer Abstracting the pattern
  • 40. 40 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> Maybe a -> Maybe b instance Functor Maybe where map f Nothing = Nothing map f (Just x) = Just (f x) Haskell Primer Abstracting the pattern
  • 41. 41 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> Maybe a -> Maybe b instance Functor Maybe where map f Nothing = Nothing map f (Just x) = Just (f x) fMaybe = map f Haskell Primer Abstracting the pattern
  • 42. 42 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> [a] -> [b] Haskell Primer Abstracting the pattern
  • 43. 43 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> [a] -> [b] instance Functor List where map _ [] = [] Haskell Primer Abstracting the pattern
  • 44. 44 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> [a] -> [b] instance Functor List where map _ [] = [] map f x:xs = f x : map xs Haskell Primer Abstracting the pattern
  • 45. 45 -- map :: (a -> b) -> F a -> F b -- map :: (a -> b) -> [a] -> [b] instance Functor List where map _ [] = [] map f x:xs = f x : map xs fList = map f Haskell Primer Abstracting the pattern
  • 46. 46 • enhanced type lift functions • composition is possible via map f Recap
  • 47. 47 type Deck = Int type Card = Int Monads Let’s play a game
  • 48. 48 type Deck = Int type Card = Int addCard :: Card -> Deck -> Deck Let’s play a game Monads
  • 49. 49 type Deck = Int type Card = Int addCard :: Card -> Deck -> Deck addCard c d = c + d Let’s play a game Monads
  • 50. 50 -- Let's start a game: oneCard :: Deck oneCard = addCard 5 0 Monads
  • 51. 51 twoCard = addCard 4 oneCard -- equivalent to addCard 4 (addCard 5 0) Monads
  • 52. 52 -- what happens if we add more? addCard 2 (addCard 3 (addCard 4 (addCard 5 0 ))) Haskell Primer The whole game
  • 53. 53 addCard :: Card -> Deck -> Maybe Deck addCard c d | c + d < 10 = Just (c + d) | otherwise = Nothing -- you lose Let’s make the game more interesting Monads
  • 54. 54 addCard :: Card -> Deck -> Maybe Deck oneCard :: Maybe Deck oneCard = addCard 5 0 Monads
  • 55. 55 addCard :: Card -> Deck -> Maybe Deck oneCard :: Maybe Deck oneCard = addCard 5 0 -- Deck != Maybe Deck addCard 3 oneCard Monads
  • 56. 56 game :: Maybe Deck game = case addCard 5 0 of -- returns a Maybe Deck Nothing -> Nothing Just deck1 -> case addCard 4 deck1 of -- returns a Maybe Deck Nothing -> Nothing Just deck2 -> case addCard 3 deck2 of -- returns a Maybe Deck Nothing -> Nothing Just deck3 -> addCard 2 deck3 -- returns a Maybe Deck Monads
  • 57.
  • 58. 58 addFirstCard :: Deck -> Maybe Deck addFirstCard = addCard 5 addSecondCard :: Deck -> Maybe Deck addSecondCard = addCard 4 addThirdCard :: Deck -> Maybe Deck addThirdCard = addCard 3 Monads
  • 59. 59 Maybe Deck -> -- previous total (Deck -> Maybe Deck) -> -- addCard Maybe Deck -- new total Monads
  • 60. 60 (>>=) :: Maybe Deck -> -- previous total (Deck -> Maybe Deck) -> -- addCard Maybe Deck -- new total Monads
  • 61. 61 emptyDeck :: Maybe Deck addCard 5 :: Deck -> Maybe Deck emptyDeck >>= addCard 5 Monads
  • 62. 62 game = emptyDeck >>= addFirstCard >>= addSecondCard >>= addThirdCard Monads
  • 63. 63 -- doesn’t look right emptyDeck = addCard 0 0 Monads
  • 64. 64 -- the minimal context that contains a 0 emptyDeck = return 0 Monads
  • 65. 65 Monad = Functor + return + (>>=)
  • 66. 66 (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b Monads
  • 67. 67 (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b instance Monad Maybe where return x = Just x Monads
  • 68. 68 (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b instance Monad Maybe where return x = Just x Nothing >>= _ = Nothing Monads
  • 69. 69 (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b instance Monad Maybe where return x = Just x Nothing >>= _ = Nothing (Just x) >>= f = f x Monads
  • 70. 70 (>>=) :: [a] -> (a -> [b]) -> [b] What about Lists? Monads
  • 71. 71 (>>=) :: [a] -> (a -> [b]) -> [b] (>>=) = flatMap Haskell Primer What about Lists?
  • 72. 72 (>>=) :: [a] -> (a -> [b]) -> [b] (>>=) = flatMap instance Monad List where return x = [x] Haskell Primer What about Lists?
  • 73. 73 (>>=) :: [a] -> (a -> [b]) -> [b] (>>=) = flatMap instance Monad List where return x = [x] xs >>= f = concat (map f xs) Haskell Primer What about Lists?
  • 74. 74 g :: a -> Maybe b f :: b -> Maybe c Recap Recap
  • 75. 75 g :: a -> Maybe b f :: b -> Maybe c (>>=) :: m b -> (b -> m c) -> m c extracts the b in the output of g and feeds it to f Recap Recap
  • 77. Luca Belli March 20, 2018 Thank you! @__lucab