Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Michał Płachta

December 15, 2017
Tweet

More Decks by Michał Płachta

Other Decks in Programming

Transcript

  1. @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 “
  2. @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
  3. @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
  4. @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
  5. @miciek Journey Prisoner’s Dilemma 3 apps 15 overlapping concerns using

    Cats & Freestyle more Freestyle Tagless Final hardcore concern separation
  6. @miciek Prisoner’s Dilemma GUILTY! GUILTY! 3 years 3 years ...

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

    ... 1 year 1 year GUILTY! ... FREE 3 years ... GUILTY! 3 years FREE
  8. @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!
  9. @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
  10. @miciek Hot Seat Game Concerns Hot Seat Logic Computing Verdict

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

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

    Player Interaction Player Communication using Console Free[F, A]
  13. @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]
  14. @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!
  15. @miciek Reusing Values Computing Verdict Player Interaction Player Communication using

    Console Unknown Opponent Logic Opponent Interaction #1 #2 #3 Bot Opponent
  16. @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
  17. @miciek Different Interpreter for Opponent? Computing Verdict Player Interaction Player

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

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

    JoinWaitingPlayer CheckIfOpponentJoined UnregisterWaiting found Player A found Player B
  20. @miciek Matchmaking Programs Player A Player B waitForOpponent findOpponent RegisterAsWaiting

    GetWaitingPlayers JoinWaitingPlayer CheckIfOpponentJoined UnregisterWaiting
  21. @miciek Remote Opponent Computing Verdict Player Interaction Player Communication using

    Console Unknown Opponent Logic Opponent Interaction Remote Opponent Bot Opponent
  22. @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
  23. @miciek Recap: Game with a Bot Unknown Opponent Logic Opponent

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

    Opponent var state: Map ... Remote Opponent is interpreting into another Free program! Remote Opponent
  25. @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
  26. @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
  27. @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
  28. @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!