Pro Yearly is on sale from $80 to $50! »

Freestyle, Free & Tagless: Separation of Concerns on Steroids

Freestyle, Free & Tagless: Separation of Concerns on Steroids

You will learn how to cut your applications into isolated, independent pieces using Freestyle library with free monads or tagless final style. The session is based on a multiplayer, purely functional version of Prisoner’s Dillema.

Our applications frequently consist of business logic, communication, persistence, caches & logging. We always want to keep them apart and reason about each of them individually. However, this is very difficult to achieve in practice.

Enter free monad. It lets us decouple those concerns by dividing our program into algebras and interpreters. I will show you an application built on top of free monad in Scala & Cats. You will learn how to create pure & declarative DSLs, side-effecting interpreters and how to use them together. Then I will introduce Freestyle library and show how it makes the code more concise. At the end I will compare the free monad approach to another technique used to decouple concerns: final tagless.

6f6dc1b13fd3fe35d36db3adafcb0c8e?s=128

Michał Płachta

December 15, 2017
Tweet

Transcript

  1. @miciek Michał Płachta Freestyle Free & Tagless www.michalplachta.com www.michalplachta.com Separation

    of Concerns on Steroids @miciek
  2. @miciek Separation of Concerns… not

  3. @miciek Intelligent Thinking It is, that one is willing to

    study in depth an aspect of one's subject matter in isolation for the sake of its own consistency, all the time knowing that one is occupying oneself only with one of the aspects. Portrait by Hamilton Richards - manuscripts of Edsger W. Dijkstra, University Texas at Austin, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=4204157 “
  4. @miciek Separate All the Things! High Level Logic Business #1

    Business #2 Logging Metrics DB Access Low Level Logic High Level Logic Low Level Logic business concern technical concern
  5. @miciek Separate All the Things! High Level Logic Business #1

    Business #2 Logging Metrics DB Access Low Level Logic High Level Logic Low Level Logic business concern technical concern knows about
  6. @miciek No outgoing arrows = separated High Level Logic Business

    #1 Business #2 Logging Metrics DB Access Low Level Logic High Level Logic Low Level Logic
  7. @miciek Developer Experience Intended Design Real Life Usage https://i.imgur.com/JIXACph.jpg Developer

    Experience
  8. @miciek Developer Experience Database https://i.imgur.com/JIXACph.jpg Business #1 High Level Logic

    Low Level Logic DB Access
  9. @miciek Developer Experience https://cdn-images-1.medium.com/max/1920/1*WeZlYd1Exr2JVup0MeSwaw.jpeg

  10. @miciek We’ll use Free Free[F, A] A program that uses

    language F to produce A
  11. @miciek Journey Prisoner’s Dilemma 3 apps 15 overlapping concerns using

    Cats & Freestyle more Freestyle Tagless Final hardcore concern separation
  12. @miciek Prisoner’s Dilemma Simple example with non-trivial mechanics...

  13. @miciek Prisoner’s Dilemma

  14. @miciek Prisoner’s Dilemma

  15. @miciek Prisoner’s Dilemma The other prisoner is GUILTY! I prefer

    to remain SILENT or
  16. @miciek Prisoner’s Dilemma GUILTY! GUILTY! 3 years 3 years

  17. @miciek Prisoner’s Dilemma GUILTY! GUILTY! 3 years 3 years ...

    ... 1 year 1 year
  18. @miciek Prisoner’s Dilemma GUILTY! GUILTY! 3 years 3 years ...

    ... 1 year 1 year GUILTY! ... FREE 3 years
  19. @miciek Prisoner’s Dilemma GUILTY! GUILTY! 3 years 3 years ...

    ... 1 year 1 year GUILTY! ... FREE 3 years ... GUILTY! 3 years FREE
  20. @miciek Repeated Prisoner’s Dilemma http://ncase.me/trust/

  21. @miciek Journey Prisoner’s Dilemma 3 apps 15 overlapping concerns using

    Cats & Freestyle more Freestyle Tagless Final
  22. @miciek Hot Seat Game Welcome to Hot Seat Game, Prisoner

    A! What's your name? > Adam Hello, Adam! Welcome to Hot Seat Game, Prisoner B! What's your name? > Jane Hello, Jane!
  23. @miciek Hot Seat Game Welcome to Hot Seat Game, Prisoner

    A! What's your name? > Adam Hello, Adam! Welcome to Hot Seat Game, Prisoner B! What's your name? > Jane Hello, Jane! Adam, is Jane guilty? > n Your decision: Silence Jane, is Adam guilty? > n Your decision: Silence Verdict for Adam is 1 year Verdict for Jane is 1 year
  24. @miciek Hot Seat Game

  25. @miciek Hot Seat Game

  26. @miciek Hot Seat Game

  27. @miciek Hot Seat Game Concerns Hot Seat Logic business concern

    technical concern knows about
  28. @miciek Hot Seat Game Concerns Hot Seat Logic Computing Verdict

    Player Interaction Player Communication using Console business concern technical concern knows about
  29. @miciek Hot Seat Game Concerns Hot Seat Logic Computing Verdict

    Player Interaction Player Communication using Console
  30. @miciek Hot Seat Game Concerns Hot Seat Logic Computing Verdict

    Player Interaction Player Communication using Console Free[F, A]
  31. @miciek Computing Verdict Concern #1

  32. @miciek Player Interaction Concern #2 Operation name Return value

  33. @miciek Player Interaction Concern #2

  34. @miciek Hot Seat Logic Concern #3

  35. @miciek Hot Seat Logic Concern #3

  36. @miciek Player Interaction using Console Concern #4 How to transform

    Player[A] into IO[A]?
  37. @miciek Player Interaction using Console Concern #4 How to transform

    Player[A] into IO[A]?
  38. @miciek Player Interaction using Console Concern #4 How to transform

    Player[A] into IO[A]?
  39. @miciek Player Interaction using Console Concern #4

  40. @miciek Connecting the Dots Hot Seat Logic Computing Verdict Player

    Interaction Player Communication using Console pure function Free[Player, A] Player[A] ~> IO[A] Free[S, Unit]
  41. @miciek End of the World

  42. @miciek End of the World

  43. @miciek End of the World

  44. @miciek Practical Problems with Free #1 Boilerplate

  45. @miciek Freestyle which is built on top of Cats

  46. @miciek After Freestyle No Boilerplate Algebra

  47. @miciek After Freestyle No Boilerplate Interpreter

  48. @miciek After Freestyle No Boilerplate Interpreter

  49. @miciek Playing with a Bot Adding new features is adding

    more concerns!
  50. @miciek Playing with a Bot Welcome to Single Player Game!

    What's your name? > Michal Hello, Michal! Michal, is WALL-E guilty? > n Your decision: Silence Verdict for Michal is 1 year Adding new features is adding more concerns!
  51. @miciek Reusing Values Computing Verdict Player Interaction Player Communication using

    Console Unknown Opponent Logic Opponent Interaction #1 #2 #3 Bot Opponent
  52. @miciek Opponent Interaction Freestyle generates all the case classes and

    other helpers!
  53. @miciek Unknown Opponent Logic

  54. @miciek Unknown Opponent Logic

  55. @miciek Friend’s Reaction

  56. @miciek End of the World

  57. @miciek End of the World

  58. @miciek End of the World ???

  59. @miciek Bot Opponent

  60. @miciek End of the World

  61. @miciek Workflow 1. Define operations 2. Create programs 3. Try

    to run them 4. Fix errors by implementing interpreters Player Interaction Unknown Opponent Unknown Opponent
  62. @miciek Different Interpreter for Opponent? Computing Verdict Player Interaction Player

    Communication using Console Unknown Opponent Logic Opponent Interaction ? Bot Opponent
  63. @miciek Remote Opponent Computing Verdict Player Interaction Player Communication using

    Console Unknown Opponent Logic Opponent Interaction Remote Opponent Bot Opponent
  64. @miciek Meeting Remote Opponent Player A Player B RegisterAsWaiting GetWaitingPlayers

    JoinWaitingPlayer CheckIfOpponentJoined UnregisterWaiting found Player A found Player B
  65. @miciek Matchmaking

  66. @miciek Matchmaking Programs Player A Player B waitForOpponent findOpponent RegisterAsWaiting

    GetWaitingPlayers JoinWaitingPlayer CheckIfOpponentJoined UnregisterWaiting
  67. @miciek Find Opponent

  68. @miciek Find Opponent

  69. @miciek Remote Opponent Computing Verdict Player Interaction Player Communication using

    Console Unknown Opponent Logic Opponent Interaction Remote Opponent Bot Opponent
  70. @miciek Bot Opponent Recap:

  71. @miciek Remote Opponent What should we interpret Opponent into? ???

  72. @miciek Remote Opponent We should interpret Opponent into Free

  73. @miciek Workflow 1. Define operations 2. Create programs 3. Try

    to run them 4. Fix errors by implementing interpreters Player Interaction Unknown Opponent Unknown Opponent
  74. @miciek Recap: Game with a Bot Unknown Opponent Logic Opponent

    Interaction Bot Opponent var state: Map ...
  75. @miciek Interpreting into Free Unknown Opponent Logic Opponent Interaction Bot

    Opponent var state: Map ... Remote Opponent is interpreting into another Free program! Remote Opponent
  76. @miciek Multiplayer Game Mode Opponent Interaction Remote Opponent Matchmaking ?

    Let’s zoom in!
  77. @miciek Multiplayer Game Mode Opponent Interaction Remote Opponent Matchmaking Remote

    Actor Matchmaker
  78. @miciek Matchmaking Recap:

  79. @miciek Matchmaking Server Handler

  80. @miciek End of the World

  81. @miciek End of the World

  82. @miciek End of the World

  83. @miciek 3 Game Modes = 15 Concerns + 31 Tests

    Computing Verdict Player Interaction Player Communication using Console Unknown Opponent Logic Opponent Interaction Remote Opponent Bot Opponent Decision Registry Matchmaking Matchmaking Server Decision Server Timing Hot Seat Logic EoW Thread Timing x3 Player Local Interaction
  84. @miciek 3 Game Modes = 15 Concerns + 31 Tests

    Computing Verdict Player Interaction Player Communication using Console Unknown Opponent Logic Opponent Interaction Remote Opponent Bot Opponent Decision Registry Matchmaking Matchmaking Server Decision Server Timing Hot Seat Logic EoW Thread Timing x3 Player Local Interaction Thread.sleep println var
  85. @miciek Journey Prisoner’s Dilemma 3 apps 15 overlapping concerns using

    Cats & Freestyle more Freestyle Tagless Final
  86. @miciek Freestyle Composing Algebras using Modules

  87. @miciek Pure Cats Composing Algebras using Coproducts

  88. @miciek Pure Cats Free Creating Lots of Objects

  89. @miciek Journey Prisoner’s Dilemma 3 apps 15 overlapping concerns using

    Cats & Freestyle more Freestyle Tagless Final
  90. @miciek Final Tagless Instead of wrapping everything in Free

  91. @miciek Final Tagless Let’s execute instructions directly! F can be

    anything!
  92. @miciek Final Tagless using Freestyle

  93. @miciek Course of Action ▪ Use Free directly using cats

    or scalaz ▪ Use Final Tagless directly ▪ When comfortable, start using freestyle ▪ Decide @tagless or @free per use case
  94. @miciek @free or @tagless? @free @tagless program is data program

    is an expression plain values expressions parametrized by F[_] stack-safe stack-safe iff F[_] is stack-safe Remember: Mix and match your algebras using Freestyle! More here: https://softwaremill.com/free-tagless-compared-how-not-to-commit-to-monad-too-early/ Note that Freestyle gets rid of some problems mentioned there!
  95. @miciek Learn More https://github.com/miciek/free-prisoners 6 apps / 3 in Free

    / 3 in Freestyle / 2 Akka actors / 31 tests
  96. @miciek Michał Płachta www.michalplachta.com THANKS! Freestyle Free & Tagless https://github.com/miciek/free-prisoners

    6 pure apps / 15 concerns / 2 Akka actors / 31 tests @miciek