an intuitive guide to combining free monad and free
play

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


  1. An Intuitive Guide to Combining Free Monad and Free Applicative

  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

  3. Agenda • Abstracting effects from domain definitions • Abstracting effects with Free Structures • Combining Free and FreeAp

  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

  5. A Basic Domain

  6. A Basic Implementation

  7. Capturing Failure

  8. Capturing Async Effects

  9. Combining Multiple Effects

  10. What Happened to our Domain?

  11. Problems • Tightly couples implementation details to domain 
 • Can’t test domain logic in isolation of implementation

  12. Final Tagless

  13. Final Tagless Implementation

  14. Free Monad

  15. Operations as Data

  16. Lifting Operations into Free Monad

  17. Sequencing Operations

  18. Our Program

  19. Interpreting Program (Data)

  20. Interpreting Program (Data)

  21. Interpreting Program (Data)

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


  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 


  24. Operations as Data

  25. Our Domain as an ADT

  26. Our Domain as an ADT

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


  28. Lifting Operations into Free Monad

  29. Lifting Operations into Free Monad

  30. Lifting Operations into FreeAp

  31. Lifting Operations into FreeAp

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

  33. Creating our Repo with ExecStrategy Allows us to defer the choice of Free Monad vs. Free Applicative

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


  35. DEPENDENT OPERATIONS E.G. SEQUENTIAL FREE MONAD

  36. Operations in Sequence

  37. INDEPENDENT OPERATIONS E.G. PARALLELISM FREE APPLICATIVE

  38. Operations in Parallel

  39. HOW DO WE COMBINE SEQUENTIAL AND PARALLEL OPERATIONS?

  40. Instruction Set

  41. 
 
 
 
 Two Main Choices • Parallel program of sequential steps 


  42. Parallel Program of Sequential Steps

  43. 
 
 
 
 Two Main Choices • Parallel program of sequential steps 
 • Sequential program of parallel steps

  44. Sequential Program of Parallel Steps

  45. Lifting Operations into Program • Free needs to wrap internal Operation with FreeAp 
 • FreeAp steps need to be wrapped in Free

  46. 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. )

  47. Two (Simple) Domains

  48. Lift Domain • Handling the combination of multiple domains by making the domain functor generic as F

  49. Lift Domain • Handling the combination of multiple domains by making the domain functor generic as F

  50. Writing Our Program

  51. Program Structure

  52. Writing Our Program (Another Way)

  53. Program Structure

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


  55. Interpreting an Operation

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

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

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


  59. Combining Our Domains • Define our operations as a common instruction set 
 • Combine out interpreters to read this instruction set

  60. Creating Program Instructions

  61. Creating Program Instructions

  62. Creating Program Instructions

  63. Interpreting Program Instructions

  64. Interpreting Program Instructions

  65. Interpreting Program Instructions

  66. Interpreting Program Instructions

  67. Interpreting Program Instructions

  68. 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)

  69. Running the Program

  70. Program 1

  71. Program Results

  72. Program 2

  73. Program Results

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


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


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

  77. @CamJo89 Thank You Questions?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend