THE UNREASONABLE EFFECTIVENESS OF
MONADS 🔥 🚀 A U T O M A T I O N , S T R U C T U R E , & P U R E LY F U N C T I O N A L E F F E C T S ⚙ ✨
Slide 2
Simplicity is prerequisite for reliability 🔑 EDSGER DIJKSTRA
Slide 3
THE UNREASONABLE EFFECTIVENESS OF MONADS B R O O K LY N Z E L E N K A , @ e x p e d e
Slide 4
THE UNREASONABLE EFFECTIVENESS OF MONADS B R O O K LY N Z E L E N K A , @ e x p e d e
• Cofounder/CTO at Fission • https://fission.codes • PLT & VM enthusiast • Previously an Ethereum Core Dev • Primary author of Witchcraft Suite • Used to teach Elixir professionally • Now with a Haskell team 😉
Slide 5
THE UNREASONABLE EFFECTIVENESS OF MONADS
S A L E S P I TC H
Slide 6
THE UNREASONABLE EFFECTIVENESS OF MONADS
S A L E S P I TC H
Broad trend towards functional techniques 2. Handle increasing complexity 3. Familiar != “simple” 4. You’re already sitting in the “polyglot and fringe” track 😉
Slide 7
250
THE UNREASONABLE EFFECTIVENESS OF MONADS
E D U C AT I O N I N V E S T M E N T V S C O M P L E X I T Y
Structured
Unconstrained
Slide 8
THE UNREASONABLE EFFECTIVENESS OF MONADS
E D U C AT I O N I N V E S T M E N T V S C O M P L E X I T Y 1000
750
500
Unconstrained 250
Structured
Slide 9
THE UNREASONABLE EFFECTIVENESS OF MONADS
E F F E C T- F O C U S E D
Slide 10
THE UNREASONABLE EFFECTIVENESS OF MONADS
E F F E C T- F O C U S E D
• Monads are extremely well defined
Slide 11
THE UNREASONABLE EFFECTIVENESS OF MONADS
E F F E C T- F O C U S E D
• Monads are extremely well defined • Steer away from the math
🚫
Slide 12
THE UNREASONABLE EFFECTIVENESS OF MONADS
E F F E C T- F O C U S E D
• Monads are extremely well defined • Steer away from the math • Many uses, but the main practical one is effects
🚫
Slide 13
THE UNREASONABLE EFFECTIVENESS OF MONADS
TA B L E O F C O N T E N T S
Slide 14
THE UNREASONABLE EFFECTIVENESS OF MONADS
TA B L E O F C O N T E N T S
• Some seemingly unrelated — but already familiar — concepts • Structured abstraction • Technical prerequisites • The essence of the monadic style • Common examples
Slide 15
STRUCTURED ABSTRACTION
Slide 16
STRUCTURED ABSTRACTION 🧱
Slide 17
STRUCTURED ABSTRACTION
structure / ˈstrʌk tʃər / 1. A mode of building, construction, or organization; arrangement of parts, elements, or constituents e.g. a pyramidal structure. 🗼
Slide 18
STRUCTURED ABSTRACTION
abstraction / æbˈstræk ʃən / 1. Something that concentrates in itself the essential qualities of anything more extensive or more general, or of several things; its essence. ⚗
Slide 19
STRUCTURED ABSTRACTION
TRADE-OFFS
• “GOTOs considered harmful” • Exchange control for understanding
Libraries
Applications
Slide 20
STRUCTURED ABSTRACTION
TRADE-OFFS
• “GOTOs considered harmful” • Exchange control for understanding
Libraries
GENERALITY 🌏
⚖
POWER 🚀
Applications
Slide 21
STRUCTURED ABSTRACTION
TRADE-OFFS
• “GOTOs considered harmful” • Exchange control for understanding
Libraries
GENERALITY 🌏
⚖
POWER 🚀
Applications
Slide 22
STRUCTURED ABSTRACTION
TRADE-OFFS
• “GOTOs considered harmful” • Exchange control for understanding Functor/Enum Libraries
GENERALITY 🌏
⚖
POWER 🚀
Applications
Slide 23
STRUCTURED ABSTRACTION
TRADE-OFFS
• “GOTOs considered harmful” • Exchange control for understanding Functor/Enum Libraries
GENERALITY 🌏
Persistent/Ecto ⚖
POWER 🚀
Applications
Slide 24
STRUCTURED ABSTRACTION
TRADE-OFFS
• “GOTOs considered harmful” • Exchange control for understanding Monad 😉
Functor/Enum Libraries
GENERALITY 🌏
Persistent/Ecto ⚖
POWER 🚀
Applications
Slide 25
STRUCTURED ABSTRACTION
SIMPLE EXAMPLE: SEMIGROUP
Slide 26
STRUCTURED ABSTRACTION
SIMPLE EXAMPLE: SEMIGROUP
• Not a data structure
Slide 27
STRUCTURED ABSTRACTION
SIMPLE EXAMPLE: SEMIGROUP
• Not a data structure • Not a function
Slide 28
STRUCTURED ABSTRACTION
SIMPLE EXAMPLE: SEMIGROUP
• Not a data structure • Not a function • An interface & rules!
Slide 29
STRUCTURED ABSTRACTION
SIMPLE EXAMPLE: SEMIGROUP
• Not a data structure • Not a function • An interface & rules!
Slide 30
STRUCTURED ABSTRACTION
A SEMIGROUP ON…
Slide 31
STRUCTURED ABSTRACTION
A SEMIGROUP ON…
Slide 32
STRUCTURED ABSTRACTION
A SEMIGROUP ON…
Slide 33
STRUCTURED ABSTRACTION
A SEMIGROUP ON…
Slide 34
STRUCTURED ABSTRACTION
UNLAWFUL COUNTEREXAMPLE 🚨
Slide 35
STRUCTURED ABSTRACTION
W E U S E L O T S O F D I F F E R E N T F E AT U R E S D A I LY
Slide 36
STRUCTURED ABSTRACTION
W E U S E L O T S O F D I F F E R E N T F E AT U R E S D A I LY
• Promises (AKA async/await) • Network • Database • Long computation
Slide 37
STRUCTURED ABSTRACTION
W E U S E L O T S O F D I F F E R E N T F E AT U R E S D A I LY
• Promises (AKA async/await) • Network • Database • Long computation • throw/catch
Slide 38
STRUCTURED ABSTRACTION
W E U S E L O T S O F D I F F E R E N T F E AT U R E S D A I LY
• Promises (AKA async/await) • Network • Database • Long computation • throw/catch • Tracing
Slide 39
STRUCTURED ABSTRACTION
W E U S E L O T S O F D I F F E R E N T F E AT U R E S D A I LY
• Promises (AKA async/await) • Network • Database • Long computation • throw/catch • Tracing • Context or config values
Slide 40
STRUCTURED ABSTRACTION
B U T M AY B E N O T S O D I F F E R E N T
Slide 41
STRUCTURED ABSTRACTION
B U T M AY B E N O T S O D I F F E R E N T
Slide 42
STRUCTURED ABSTRACTION
B U T M AY B E N O T S O D I F F E R E N T
Slide 43
STRUCTURED ABSTRACTION
B U T M AY B E N O T S O D I F F E R E N T
Slide 44
STRUCTURED ABSTRACTION
B U T M AY B E N O T S O D I F F E R E N T
Slide 45
STRUCTURED ABSTRACTION
B U T M AY B E N O T S O D I F F E R E N T
Slide 46
STRUCTURED ABSTRACTION
B U T M AY B E N O T S O D I F F E R E N T
Slide 47
STRUCTURED ABSTRACTION
B U T M AY B E N O T S O D I F F E R E N T
🚂
⏲
⛓
🖊
Slide 48
STRUCTURED ABSTRACTION
YOU’RE ALREADY DOING THIS
Slide 49
STRUCTURED ABSTRACTION
YOU’RE ALREADY DOING THIS 🤾 S U R P R I S E AT T A C K E F F E C T S 💥
Slide 50
FRONT EFFECTS
Slide 51
FRONT EFFECTS 👋
Slide 52
FRONT EFFECTS
SIDE EFFECTS
Slide 53
FRONT EFFECTS
SIDE EFFECTS
• Implicit effects that happen “off to the side”
Slide 54
FRONT EFFECTS
SIDE EFFECTS
• Implicit effects that happen “off to the side” • Built into the language / platform
Slide 55
FRONT EFFECTS
SIDE EFFECTS
• Implicit effects that happen “off to the side” • Built into the language / platform • Hard to inspect
Slide 56
FRONT EFFECTS
SIDE EFFECTS
• Implicit effects that happen “off to the side” • Built into the language / platform • Hard to inspect • …thus hard to change, compose, or test
Slide 57
FRONT EFFECTS
E F F E C T S - A S - D ATA
Slide 58
FRONT EFFECTS
E F F E C T S - A S - D ATA
• Data is simple
Slide 59
FRONT EFFECTS
E F F E C T S - A S - D ATA
• Data is simple • Effects don’t need to be separate: express effects as data
Slide 60
FRONT EFFECTS
E F F E C T S - A S - D ATA
• Data is simple • Effects don’t need to be separate: express effects as data • Write your own effects
Slide 61
FRONT EFFECTS
E F F E C T S - A S - D ATA
• Data is simple • Effects don’t need to be separate: express effects as data • Write your own effects • Inspect them as needed
Slide 62
FRONT EFFECTS
E F F E C T S - A S - D ATA
• Data is simple • Effects don’t need to be separate: express effects as data • Write your own effects • Inspect them as needed • Compose as needed
Slide 63
FRONT EFFECTS
ENTER THE MONAD
Slide 64
FRONT EFFECTS
ENTER THE MONAD
• A common interface to make this straightforward!
Slide 65
FRONT EFFECTS
ENTER THE MONAD
• A common interface to make this straightforward! • Learn once, use everywhere
Slide 66
THE FUNCTOR TOWER
Slide 67
THE FUNCTOR TOWER 🗼
LET ’S COVER SOME INTERNALS
Slide 68
THE FUNCTOR TOWER
Progress is possible only if we train ourselves to think about programs without thinking of them as pieces of executable code 🚀 EDSGER DIJKSTRA
Slide 69
THE FUNCTOR TOWER
F U N CTO R
Slide 70
THE FUNCTOR TOWER
F U N CTO R
• Always returns the same shape!
Slide 71
THE FUNCTOR TOWER
F U N CTO R
• Always returns the same shape!
Functor.map
Slide 72
THE FUNCTOR TOWER
F U N CTO R
• Always returns the same shape!
Functor.map
Slide 73
THE FUNCTOR TOWER
A P P LY
Functor.map
Slide 74
THE FUNCTOR TOWER
A P P LY
Functor.map Apply.apply
Slide 75
THE FUNCTOR TOWER
A P P L I C AT I V E
Functor.map Apply.apply
🙋 AKA return, pure, of, unit
Slide 76
THE FUNCTOR TOWER
A P P L I C AT I V E
Functor.map Apply.apply Applicative.wrap 🙋 AKA return, pure, of, unit
Slide 77
THE FUNCTOR TOWER
CHAIN
Functor.map Apply.apply Applicative.wrap
Slide 78
THE FUNCTOR TOWER
CHAIN
Functor.map Apply.apply Applicative.wrap
Chain.bind
Slide 79
THE FUNCTOR TOWER
MONAD 6
Functor.map Apply.apply Applicative.wrap
Chain.bind
Slide 80
THE FUNCTOR TOWER
MONAD 6
Functor.map Apply.apply Applicative.wrap
Chain.bind
Monad
• Monads are the essence of Turing complete, effectual computation
Slide 81
THE FUNCTOR TOWER
N OT G RO U P E D BY AC C I D E N T Call
D ATA
f(x)
FUNCTION
R E S U LT
Functor
D ATA
map
FUNCTION
R E S U LT ( S )
Apply
D ATA
apply
FUNCTION(S)
R E S U LT ( S )
Chain
D ATA
bind
NK K II N NG G FF U UN N LL II N
R E S U LT ( S )
Slide 82
THE FUNCTOR TOWER
M O N A D ( F I N A L LY ! )
Slide 83
E I T H E R / R E S U LT
Slide 84
E I T H E R / R E S U LT 🚂 RAILROAD PROGRAMMING
Slide 85
E I T H E R / R E S U LT
RAILROAD PROGRAMMING
Slide 86
E I T H E R / R E S U LT
RAILROAD PROGRAMMING
Happy Path (Continue)
Error Case (Skip)
No Effect (Afterwards)
Slide 87
E I T H E R / R E S U LT
RAILROAD PROGRAMMING
Happy Path (Continue)
Error Case (Skip)
No Effect (Afterwards)
Slide 88
E I T H E R / R E S U LT
RAILROAD PROGRAMMING
Happy Path (Continue)
Error Case (Skip)
No Effect (Afterwards)
Slide 89
E I T H E R / R E S U LT
RAILROAD PROGRAMMING
Happy Path (Continue)
Error Case (Skip)
No Effect (Afterwards)
Slide 90
E I T H E R / R E S U LT
RAILROAD PROGRAMMING
Happy Path (Continue)
Error Case (Skip)
No Effect (Afterwards)
Slide 91
E I T H E R / R E S U LT
RAILROAD PROGRAMMING
Happy Path (Continue)
Error Case (Skip)
No Effect (Afterwards)
Slide 92
E I T H E R / R E S U LT
RAILROAD PROGRAMMING
Happy Path (Continue)
Error Case (Skip)
No Effect (Afterwards)
Slide 93
E I T H E R / R E S U LT
RAILROAD PROGRAMMING
Happy Path (Continue)
Error Case (Skip)
No Effect (Afterwards)
Slide 94
E I T H E R / R E S U LT
C A R R I E R D ATA
Slide 95
E I T H E R / R E S U LT
I N S TA N C E S
Slide 96
E I T H E R / R E S U LT
USE
Slide 97
E I T H E R / R E S U LT
USE
Slide 98
WRITER
Slide 99
WRITER 🖊 I N S P E C T A B L E , D A TA - O R I E N T E D L O G G I N G
Slide 100
WRITER
MONAD
🖊
Slide 101
WRITER
MONAD
🖊
Log
Program
Slide 102
WRITER
MONAD
🖊
Log
Program
Slide 103
WRITER
C A R R I E R D ATA
Slide 104
WRITER
I N S TA N C E S
Slide 105
WRITER
USE
Slide 106
READER
Slide 107
READER 📚 CONFIG OR CONTEXT INJECTION
Slide 108
READER
MONAD
Slide 109
READER
MONAD
Context
Program
Slide 110
READER
MONAD
Context
Program
Slide 111
READER
C A R R I E R D ATA
Slide 112
READER
I N S TA N C E S
Slide 113
READER
USE
Slide 114
MONADS
Slide 115
MONADS 🚀 A LOT OF POWER FOR A HANDFUL OF FUNCTIONS
Slide 116
https://fission.codes https://tools.fission.codes
8 THANK YOU, MALMÖ 🎉 brooklyn@fission.codes g i t h u b . c o m /e x p e d e @expede