$30 off During Our Annual Pro Sale. View Details »

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
    Michał Płachta
    Freestyle Free & Tagless
    www.michalplachta.com
    www.michalplachta.com
    Separation of Concerns
    on Steroids
    @miciek

    View Slide

  2. @miciek
    Separation of Concerns… not

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  7. @miciek
    Developer Experience
    Intended Design
    Real Life Usage
    https://i.imgur.com/JIXACph.jpg
    Developer Experience

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  11. @miciek
    Journey
    Prisoner’s
    Dilemma
    3 apps
    15 overlapping concerns
    using Cats & Freestyle
    more Freestyle
    Tagless
    Final
    hardcore concern separation

    View Slide

  12. @miciek
    Prisoner’s Dilemma
    Simple example with
    non-trivial mechanics...

    View Slide

  13. @miciek
    Prisoner’s Dilemma

    View Slide

  14. @miciek
    Prisoner’s Dilemma

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. @miciek
    Repeated Prisoner’s Dilemma
    http://ncase.me/trust/

    View Slide

  21. @miciek
    Journey
    Prisoner’s
    Dilemma
    3 apps
    15 overlapping concerns
    using Cats & Freestyle
    more Freestyle
    Tagless
    Final

    View Slide

  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!

    View Slide

  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

    View Slide

  24. @miciek
    Hot Seat Game

    View Slide

  25. @miciek
    Hot Seat Game

    View Slide

  26. @miciek
    Hot Seat Game

    View Slide

  27. @miciek
    Hot Seat Game Concerns
    Hot Seat
    Logic
    business concern
    technical concern
    knows about

    View Slide

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

    View Slide

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

    View Slide

  30. @miciek
    Hot Seat Game Concerns
    Hot Seat
    Logic
    Computing
    Verdict
    Player
    Interaction
    Player
    Communication
    using Console
    Free[F, A]

    View Slide

  31. @miciek
    Computing
    Verdict
    Concern #1

    View Slide

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

    View Slide

  33. @miciek
    Player Interaction
    Concern #2

    View Slide

  34. @miciek
    Hot Seat Logic
    Concern #3

    View Slide

  35. @miciek
    Hot Seat Logic
    Concern #3

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  39. @miciek
    Player Interaction using Console
    Concern #4

    View Slide

  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]

    View Slide

  41. @miciek
    End of the World

    View Slide

  42. @miciek
    End of the World

    View Slide

  43. @miciek
    End of the World

    View Slide

  44. @miciek
    Practical Problems with Free
    #1 Boilerplate

    View Slide

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

    View Slide

  46. @miciek
    After Freestyle
    No Boilerplate Algebra

    View Slide

  47. @miciek
    After Freestyle
    No Boilerplate Interpreter

    View Slide

  48. @miciek
    After Freestyle
    No Boilerplate Interpreter

    View Slide

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

    View Slide

  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!

    View Slide

  51. @miciek
    Reusing Values
    Computing
    Verdict
    Player
    Interaction
    Player
    Communication
    using Console
    Unknown Opponent
    Logic
    Opponent
    Interaction
    #1 #2
    #3
    Bot
    Opponent

    View Slide

  52. @miciek
    Opponent Interaction
    Freestyle generates all the case
    classes and other helpers!

    View Slide

  53. @miciek
    Unknown Opponent Logic

    View Slide

  54. @miciek
    Unknown Opponent Logic

    View Slide

  55. @miciek
    Friend’s Reaction

    View Slide

  56. @miciek
    End of the World

    View Slide

  57. @miciek
    End of the World

    View Slide

  58. @miciek
    End of the World
    ???

    View Slide

  59. @miciek
    Bot Opponent

    View Slide

  60. @miciek
    End of the World

    View Slide

  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

    View Slide

  62. @miciek
    Different Interpreter for Opponent?
    Computing
    Verdict
    Player
    Interaction
    Player
    Communication
    using Console
    Unknown Opponent
    Logic
    Opponent
    Interaction
    ?
    Bot
    Opponent

    View Slide

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

    View Slide

  64. @miciek
    Meeting Remote Opponent
    Player A Player B
    RegisterAsWaiting
    GetWaitingPlayers
    JoinWaitingPlayer
    CheckIfOpponentJoined
    UnregisterWaiting
    found
    Player A
    found
    Player B

    View Slide

  65. @miciek
    Matchmaking

    View Slide

  66. @miciek
    Matchmaking Programs
    Player A Player B
    waitForOpponent findOpponent
    RegisterAsWaiting
    GetWaitingPlayers
    JoinWaitingPlayer
    CheckIfOpponentJoined
    UnregisterWaiting

    View Slide

  67. @miciek
    Find Opponent

    View Slide

  68. @miciek
    Find Opponent

    View Slide

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

    View Slide

  70. @miciek
    Bot Opponent
    Recap:

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  74. @miciek
    Recap: Game with a Bot
    Unknown Opponent
    Logic
    Opponent
    Interaction
    Bot
    Opponent
    var state: Map
    ...

    View Slide

  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

    View Slide

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

    View Slide

  77. @miciek
    Multiplayer Game Mode
    Opponent
    Interaction
    Remote
    Opponent
    Matchmaking
    Remote Actor
    Matchmaker

    View Slide

  78. @miciek
    Matchmaking
    Recap:

    View Slide

  79. @miciek
    Matchmaking Server Handler

    View Slide

  80. @miciek
    End of the World

    View Slide

  81. @miciek
    End of the World

    View Slide

  82. @miciek
    End of the World

    View Slide

  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

    View Slide

  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

    View Slide

  85. @miciek
    Journey
    Prisoner’s
    Dilemma
    3 apps
    15 overlapping concerns
    using Cats & Freestyle
    more Freestyle
    Tagless
    Final

    View Slide

  86. @miciek
    Freestyle
    Composing Algebras using Modules

    View Slide

  87. @miciek
    Pure Cats
    Composing Algebras using Coproducts

    View Slide

  88. @miciek
    Pure Cats Free
    Creating Lots of Objects

    View Slide

  89. @miciek
    Journey
    Prisoner’s
    Dilemma
    3 apps
    15 overlapping concerns
    using Cats & Freestyle
    more Freestyle
    Tagless
    Final

    View Slide

  90. @miciek
    Final Tagless
    Instead of wrapping everything in Free

    View Slide

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

    View Slide

  92. @miciek
    Final Tagless using Freestyle

    View Slide

  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

    View Slide

  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!

    View Slide

  95. @miciek
    Learn More
    https://github.com/miciek/free-prisoners
    6 apps / 3 in Free / 3 in Freestyle / 2 Akka actors / 31 tests

    View Slide

  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

    View Slide