An Intuitive Guide to Combining Free Monad and Free Applicative Who - - PowerPoint PPT Presentation

an intuitive guide to combining free monad and free
SMART_READER_LITE
LIVE PREVIEW

An Intuitive Guide to Combining Free Monad and Free Applicative Who - - PowerPoint PPT Presentation

An Intuitive Guide to Combining Free Monad and Free Applicative Who Am I? I'm Cameron Linkedin - https://www.linkedin.com/in/cameron-joannidis/ Twitter - @CamJo89 Consult across a range of areas and have built many big data


slide-1
SLIDE 1

An Intuitive Guide to Combining Free Monad and Free Applicative

slide-2
SLIDE 2

Who Am I?

  • I'm Cameron
  • Linkedin - https://www.linkedin.com/in/cameron-joannidis/
  • Twitter - @CamJo89

  • Consult across a range of areas and have built many big data and machine

learning systems


  • Buzz Word Bingo
  • Big Data
  • Machine Learning
  • Functional Programming
slide-3
SLIDE 3

Agenda

  • Abstracting effects from domain definitions
  • Abstracting effects with Free Structures
  • Combining Free and FreeAp
slide-4
SLIDE 4

Before We Start

  • I am not advocating that you should be using Free Monad and/or Free

Applicative over final tagless. Every team and project is different and both approaches are sufficiently powerful for most requirements


  • I'm hoping to introduce you more generally to some more interesting things you

can do with Free Structures


  • Source Code: https://github.com/camjo/presentations
slide-5
SLIDE 5

A Basic Domain

slide-6
SLIDE 6

A Basic Implementation

slide-7
SLIDE 7

Capturing Failure

slide-8
SLIDE 8

Capturing Async Effects

slide-9
SLIDE 9

Combining Multiple Effects

slide-10
SLIDE 10

What Happened to our Domain?

slide-11
SLIDE 11
slide-12
SLIDE 12

Problems

  • Tightly couples implementation details to domain

  • Can’t test domain logic in isolation of implementation
slide-13
SLIDE 13

Final Tagless

slide-14
SLIDE 14

Final Tagless Implementation

slide-15
SLIDE 15

Free Monad

slide-16
SLIDE 16

Operations as Data

slide-17
SLIDE 17

Lifting Operations into Free Monad

slide-18
SLIDE 18

Sequencing Operations

slide-19
SLIDE 19

Our Program

slide-20
SLIDE 20

Interpreting Program (Data)

slide-21
SLIDE 21

Interpreting Program (Data)

slide-22
SLIDE 22

Interpreting Program (Data)

slide-23
SLIDE 23

Free (Monad) / FreeAp (Applicative)

  • Encode domain as ADT

  • Lift domain into Free structure

  • Write logic as a Free program
  • Create interpreters to interpret domain instructions with some effect

  • Interpret Free program to produce output effect

slide-24
SLIDE 24

Free (Monad) / FreeAp (Applicative)

  • Encode domain as ADT

  • Lift domain into Free structure

  • Write logic as a Free program
  • Create interpreters to interpret domain instructions with some effect

  • Interpret Free program to produce output effect

slide-25
SLIDE 25

Operations as Data

slide-26
SLIDE 26

Our Domain as an ADT

slide-27
SLIDE 27

Our Domain as an ADT

slide-28
SLIDE 28

Free (Monad) / FreeAp (Applicative)

  • Encode domain as ADT

  • Lift domain into Free structure

  • Write logic as a Free program
  • Create interpreters to interpret domain instructions with some effect
  • Interpret Free program to produce output effect

slide-29
SLIDE 29

Lifting Operations into Free Monad

slide-30
SLIDE 30

Lifting Operations into Free Monad

slide-31
SLIDE 31

Lifting Operations into FreeAp

slide-32
SLIDE 32

Lifting Operations into FreeAp

slide-33
SLIDE 33

A Little Trick

  • Lets us defer the choice of using a monad effect type or an applicative effect

type until we actually use it


  • Can be extended to any Free structure in theory
slide-34
SLIDE 34

Creating our Repo with ExecStrategy

Allows us to defer the choice of Free Monad vs. Free Applicative

slide-35
SLIDE 35

Free (Monad) / FreeAp (Applicative)

  • Encode domain as ADT

  • Lift domain into Free structure

  • Write logic as a Free program
  • Create interpreters to interpret domain instructions with some effect
  • Interpret Free program to produce output effect

slide-36
SLIDE 36

FREE MONAD

DEPENDENT OPERATIONS E.G. SEQUENTIAL

slide-37
SLIDE 37

Operations in Sequence

slide-38
SLIDE 38

FREE APPLICATIVE

INDEPENDENT OPERATIONS E.G. PARALLELISM

slide-39
SLIDE 39

Operations in Parallel

slide-40
SLIDE 40

HOW DO WE COMBINE SEQUENTIAL AND PARALLEL OPERATIONS?

slide-41
SLIDE 41

Instruction Set

slide-42
SLIDE 42

Two Main Choices

  • Parallel program of sequential steps



 
 
 


slide-43
SLIDE 43

Parallel Program of Sequential Steps

slide-44
SLIDE 44

Two Main Choices

  • Parallel program of sequential steps



 
 
 


  • Sequential program of parallel steps
slide-45
SLIDE 45

Sequential Program of Parallel Steps

slide-46
SLIDE 46

Lifting Operations into Program

  • Free needs to wrap internal Operation with FreeAp

  • FreeAp steps need to be wrapped in Free
slide-47
SLIDE 47

Lifting a Free Applicative into the Free Monad

  • Its as simple as the code below
  • The other .asProgramStep definition is a tad more involved. 


(See the code on Github for how to do it. )

slide-48
SLIDE 48

Two (Simple) Domains

slide-49
SLIDE 49

Lift Domain

  • Handling the combination of multiple domains by making the domain functor

generic as F

slide-50
SLIDE 50

Lift Domain

  • Handling the combination of multiple domains by making the domain functor

generic as F

slide-51
SLIDE 51

Writing Our Program

slide-52
SLIDE 52

Program Structure

slide-53
SLIDE 53

Writing Our Program (Another Way)

slide-54
SLIDE 54

Program Structure

slide-55
SLIDE 55

Free (Monad) / FreeAp (Applicative)

  • Encode domain as ADT

  • Lift domain into Free structure

  • Write logic as a Free program
  • Create interpreters to interpret domain instructions with some effect
  • Interpret Free program to produce output effect

slide-56
SLIDE 56

Interpreting an Operation

slide-57
SLIDE 57

User Interpreter

We make it slow so that we can easily observe the difference in runtime between the sequential and parallel versions

slide-58
SLIDE 58

Our Interpreter

We make it slow so that we can easily observe the difference in runtime between the sequential and parallel versions

slide-59
SLIDE 59

Free (Monad) / FreeAp (Applicative)

  • Encode domain as ADT

  • Lift domain into Free structure

  • Write logic as a Free program
  • Create interpreters to interpret domain instructions with some effect
  • Interpret Free program to produce output effect

slide-60
SLIDE 60

Combining Our Domains

  • Define our operations as a common instruction set

  • Combine out interpreters to read this instruction set
slide-61
SLIDE 61

Creating Program Instructions

slide-62
SLIDE 62

Creating Program Instructions

slide-63
SLIDE 63

Creating Program Instructions

slide-64
SLIDE 64

Interpreting Program Instructions

slide-65
SLIDE 65

Interpreting Program Instructions

slide-66
SLIDE 66

Interpreting Program Instructions

slide-67
SLIDE 67

Interpreting Program Instructions

slide-68
SLIDE 68

Interpreting Program Instructions

slide-69
SLIDE 69

Making it Parallel

  • Our Free Monads operation type was FreeAp[F, ?]

  • Provide a way to turn that into our output effect G in parallel (using Applicative)
slide-70
SLIDE 70

Running the Program

slide-71
SLIDE 71

Program 1

slide-72
SLIDE 72

Program Results

slide-73
SLIDE 73

Program 2

slide-74
SLIDE 74

Program Results

slide-75
SLIDE 75

Things to Note

  • Target effect must have an applicative instance

  • Default Task applicative instance runs sequentially left to right (i.e. not in parallel
  • since it is also a monad)

  • Need to explicitly import parallel applicative instance or create your own in

scope (See github code example for how)


  • We can remove most usages of .asProgramStep with implicits

slide-76
SLIDE 76

Freestyle - Removing the Boilerplate

  • Freestyle (frees.io) uses macros to remove most of the boilerplate

  • Defaults to using the Free[FreeAp[F, ?], A] approach that we used

  • Several integrations and library abstractions out of the box

  • Out of the box optimisations to reduce the overhead of using many domain

algebras
 


slide-77
SLIDE 77

What We’ve Covered

  • Why we abstract effects from domain

  • Abstracting effects with Final Tagless or Free Structures

  • Combining Free and FreeAp to introduce parallelism into our domain

  • Freestyle to remove boilerplate
slide-78
SLIDE 78

Thank You

Questions? @CamJo89