The Unreasonable Effectiveness of Monads

A presentation at Øredev 2019 in November 2019 in Malmö, Sweden by Brooklyn Zelenka

Slide 1

Slide 1

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

Slide 2

Simplicity is prerequisite for reliability 🔑 EDSGER DIJKSTRA

Slide 3

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

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

Slide 5

THE UNREASONABLE EFFECTIVENESS OF MONADS S A L E S P I TC H

Slide 6

Slide 6

THE UNREASONABLE EFFECTIVENESS OF MONADS S A L E S P I TC H

  1. 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

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

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

Slide 9

THE UNREASONABLE EFFECTIVENESS OF MONADS E F F E C T- F O C U S E D

Slide 10

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

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

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

Slide 13

THE UNREASONABLE EFFECTIVENESS OF MONADS TA B L E O F C O N T E N T S

Slide 14

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

Slide 15

STRUCTURED ABSTRACTION

Slide 16

Slide 16

STRUCTURED ABSTRACTION 🧱

Slide 17

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

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

Slide 19

STRUCTURED ABSTRACTION TRADE-OFFS • “GOTOs considered harmful” • Exchange control for understanding Libraries Applications

Slide 20

Slide 20

STRUCTURED ABSTRACTION TRADE-OFFS • “GOTOs considered harmful” • Exchange control for understanding Libraries GENERALITY 🌏 ⚖ POWER 🚀 Applications

Slide 21

Slide 21

STRUCTURED ABSTRACTION TRADE-OFFS • “GOTOs considered harmful” • Exchange control for understanding Libraries GENERALITY 🌏 ⚖ POWER 🚀 Applications

Slide 22

Slide 22

STRUCTURED ABSTRACTION TRADE-OFFS • “GOTOs considered harmful” • Exchange control for understanding Functor/Enum Libraries GENERALITY 🌏 ⚖ POWER 🚀 Applications

Slide 23

Slide 23

STRUCTURED ABSTRACTION TRADE-OFFS • “GOTOs considered harmful” • Exchange control for understanding Functor/Enum Libraries GENERALITY 🌏 Persistent/Ecto ⚖ POWER 🚀 Applications

Slide 24

Slide 24

STRUCTURED ABSTRACTION TRADE-OFFS • “GOTOs considered harmful” • Exchange control for understanding Monad 😉 Functor/Enum Libraries GENERALITY 🌏 Persistent/Ecto ⚖ POWER 🚀 Applications

Slide 25

Slide 25

STRUCTURED ABSTRACTION SIMPLE EXAMPLE: SEMIGROUP

Slide 26

Slide 26

STRUCTURED ABSTRACTION SIMPLE EXAMPLE: SEMIGROUP • Not a data structure

Slide 27

Slide 27

STRUCTURED ABSTRACTION SIMPLE EXAMPLE: SEMIGROUP • Not a data structure • Not a function

Slide 28

Slide 28

STRUCTURED ABSTRACTION SIMPLE EXAMPLE: SEMIGROUP • Not a data structure • Not a function • An interface & rules!

Slide 29

Slide 29

STRUCTURED ABSTRACTION SIMPLE EXAMPLE: SEMIGROUP • Not a data structure • Not a function • An interface & rules!

Slide 30

Slide 30

STRUCTURED ABSTRACTION A SEMIGROUP ON…

Slide 31

Slide 31

STRUCTURED ABSTRACTION A SEMIGROUP ON…

Slide 32

Slide 32

STRUCTURED ABSTRACTION A SEMIGROUP ON…

Slide 33

Slide 33

STRUCTURED ABSTRACTION A SEMIGROUP ON…

Slide 34

Slide 34

STRUCTURED ABSTRACTION UNLAWFUL COUNTEREXAMPLE 🚨

Slide 35

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

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

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

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

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

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

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

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

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

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

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

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

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

Slide 48

STRUCTURED ABSTRACTION YOU’RE ALREADY DOING THIS

Slide 49

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

Slide 50

FRONT EFFECTS

Slide 51

Slide 51

FRONT EFFECTS 👋

Slide 52

Slide 52

FRONT EFFECTS SIDE EFFECTS

Slide 53

Slide 53

FRONT EFFECTS SIDE EFFECTS • Implicit effects that happen “off to the side”

Slide 54

Slide 54

FRONT EFFECTS SIDE EFFECTS • Implicit effects that happen “off to the side” • Built into the language / platform

Slide 55

Slide 55

FRONT EFFECTS SIDE EFFECTS • Implicit effects that happen “off to the side” • Built into the language / platform • Hard to inspect

Slide 56

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

Slide 57

FRONT EFFECTS E F F E C T S - A S - D ATA

Slide 58

Slide 58

FRONT EFFECTS E F F E C T S - A S - D ATA • Data is simple

Slide 59

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

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

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

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

Slide 63

FRONT EFFECTS ENTER THE MONAD

Slide 64

Slide 64

FRONT EFFECTS ENTER THE MONAD • A common interface to make this straightforward!

Slide 65

Slide 65

FRONT EFFECTS ENTER THE MONAD • A common interface to make this straightforward! • Learn once, use everywhere

Slide 66

Slide 66

THE FUNCTOR TOWER

Slide 67

Slide 67

THE FUNCTOR TOWER 🗼 LET ’S COVER SOME INTERNALS

Slide 68

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

Slide 69

THE FUNCTOR TOWER F U N CTO R

Slide 70

Slide 70

THE FUNCTOR TOWER F U N CTO R • Always returns the same shape!

Slide 71

Slide 71

THE FUNCTOR TOWER F U N CTO R • Always returns the same shape! Functor.map

Slide 72

Slide 72

THE FUNCTOR TOWER F U N CTO R • Always returns the same shape! Functor.map

Slide 73

Slide 73

THE FUNCTOR TOWER A P P LY Functor.map

Slide 74

Slide 74

THE FUNCTOR TOWER A P P LY Functor.map Apply.apply

Slide 75

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

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

Slide 77

THE FUNCTOR TOWER CHAIN Functor.map Apply.apply Applicative.wrap

Slide 78

Slide 78

THE FUNCTOR TOWER CHAIN Functor.map Apply.apply Applicative.wrap Chain.bind

Slide 79

Slide 79

THE FUNCTOR TOWER MONAD 6 Functor.map Apply.apply Applicative.wrap Chain.bind

Slide 80

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

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

Slide 82

THE FUNCTOR TOWER M O N A D ( F I N A L LY ! )

Slide 83

Slide 83

E I T H E R / R E S U LT

Slide 84

Slide 84

E I T H E R / R E S U LT 🚂 RAILROAD PROGRAMMING

Slide 85

Slide 85

E I T H E R / R E S U LT RAILROAD PROGRAMMING

Slide 86

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

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

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

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

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

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

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

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

Slide 94

E I T H E R / R E S U LT C A R R I E R D ATA

Slide 95

Slide 95

E I T H E R / R E S U LT I N S TA N C E S

Slide 96

Slide 96

E I T H E R / R E S U LT USE

Slide 97

Slide 97

E I T H E R / R E S U LT USE

Slide 98

Slide 98

WRITER

Slide 99

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

Slide 100

WRITER MONAD 🖊

Slide 101

Slide 101

WRITER MONAD 🖊 Log Program

Slide 102

Slide 102

WRITER MONAD 🖊 Log Program

Slide 103

Slide 103

WRITER C A R R I E R D ATA

Slide 104

Slide 104

WRITER I N S TA N C E S

Slide 105

Slide 105

WRITER USE

Slide 106

Slide 106

READER

Slide 107

Slide 107

READER 📚 CONFIG OR CONTEXT INJECTION

Slide 108

Slide 108

READER MONAD

Slide 109

Slide 109

READER MONAD Context Program

Slide 110

Slide 110

READER MONAD Context Program

Slide 111

Slide 111

READER C A R R I E R D ATA

Slide 112

Slide 112

READER I N S TA N C E S

Slide 113

Slide 113

READER USE

Slide 114

Slide 114

MONADS

Slide 115

Slide 115

MONADS 🚀 A LOT OF POWER FOR A HANDFUL OF FUNCTIONS

Slide 116

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