96
@miciek Michał Płachta Freestyle Free & Tagless www.michalplachta.com www.michalplachta.com Separation of Concerns on Steroids @miciek

on Steroids Freestyle Free & Tagless Separation of Concerns...Practical Problems with Free #1 Boilerplate @miciek Freestyle which is built on top of Cats @miciek After Freestyle No

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

  • @miciek

    Michał Płachta

    Freestyle Free & Tagless

    www.michalplachta.comwww.michalplachta.com

    Separation of Concerns on Steroids

    @miciek

  • @miciek

    Separation of Concerns… not

  • @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

  • @miciek

    Separate All the Things!

    High LevelLogic

    Business #1 Business #2

    Logging

    Metrics

    DB Access

    Low Level Logic

    High LevelLogic

    Low Level Logic

    business concern

    technical concern

  • @miciek

    Separate All the Things!

    High LevelLogic

    Business #1 Business #2

    Logging

    Metrics

    DB Access

    Low Level Logic

    High LevelLogic

    Low Level Logic

    business concern

    technical concern

    knows about

  • @miciek

    No outgoing arrows = separated

    High LevelLogic

    Business #1 Business #2

    Logging

    Metrics

    DB Access

    Low Level Logic

    High LevelLogic

    Low Level Logic

  • @miciek

    Developer Experience

    Intended Design

    Real Life Usage

    https://i.imgur.com/JIXACph.jpg

    Developer Experience

  • @miciek

    Developer Experience

    Database

    https://i.imgur.com/JIXACph.jpg

    Business #1

    High LevelLogic

    Low Level Logic

    DB Access

  • @miciek

    Developer Experience

    https://cdn-images-1.medium.com/max/1920/1*WeZlYd1Exr2JVup0MeSwaw.jpeg

  • @miciek

    We’ll use Free

    Free[F, A]A program that uses

    language F to produce A

  • @miciek

    Journey

    Prisoner’s Dilemma

    3 apps15 overlapping concernsusing Cats & Freestyle

    more Freestyle Tagless Final

    hardcore concern separation

  • @miciek

    Prisoner’s Dilemma

    Simple example with non-trivial mechanics...

  • @miciek

    Prisoner’s Dilemma

  • @miciek

    Prisoner’s Dilemma

  • @miciek

    Prisoner’s Dilemma

    The other prisoner is GUILTY!

    I prefer to remainSILENT

    or

  • @miciek

    Prisoner’s Dilemma

    GUILTY!

    GUILTY!

    3 years

    3 years

  • @miciek

    Prisoner’s Dilemma

    GUILTY!

    GUILTY!

    3 years

    3 years

    ...

    ...

    1 year

    1 year

  • @miciek

    Prisoner’s Dilemma

    GUILTY!

    GUILTY!

    3 years

    3 years

    ...

    ...

    1 year

    1 year

    GUILTY!

    ...

    FREE

    3 years

  • @miciek

    Prisoner’s Dilemma

    GUILTY!

    GUILTY!

    3 years

    3 years

    ...

    ...

    1 year

    1 year

    GUILTY!

    ...

    FREE

    3 years

    ...

    GUILTY!

    3 years

    FREE

  • @miciek

    Repeated Prisoner’s Dilemmahttp://ncase.me/trust/

  • @miciek

    Journey

    Prisoner’s Dilemma

    3 apps15 overlapping concernsusing Cats & Freestyle

    more Freestyle Tagless Final

  • @miciek

    Hot Seat GameWelcome to Hot Seat Game, Prisoner A!What's your name?> AdamHello, Adam!Welcome to Hot Seat Game, Prisoner B!What's your name?> JaneHello, Jane!

  • @miciek

    Hot Seat GameWelcome to Hot Seat Game, Prisoner A!What's your name?> AdamHello, Adam!Welcome to Hot Seat Game, Prisoner B!What's your name?> JaneHello, Jane!Adam, is Jane guilty?> nYour decision: SilenceJane, is Adam guilty?> nYour decision: SilenceVerdict for Adam is 1 yearVerdict for Jane is 1 year

  • @miciek

    Hot Seat Game

  • @miciek

    Hot Seat Game

  • @miciek

    Hot Seat Game

  • @miciek

    Hot Seat Game Concerns

    Hot SeatLogic

    business concern

    technical concern

    knows about

  • @miciek

    Hot Seat Game Concerns

    Hot SeatLogicComputing

    VerdictPlayer

    Interaction

    Player Communicationusing Console

    business concern

    technical concern

    knows about

  • @miciek

    Hot Seat Game Concerns

    Hot SeatLogicComputing

    VerdictPlayer

    Interaction

    Player Communicationusing Console

  • @miciek

    Hot Seat Game Concerns

    Hot SeatLogicComputing

    VerdictPlayer

    Interaction

    Player Communicationusing Console

    Free[F, A]

  • @miciek

    ComputingVerdictConcern #1

  • @miciek

    Player InteractionConcern #2

    Operation name Return value

  • @miciek

    Player InteractionConcern #2

  • @miciek

    Hot Seat LogicConcern #3

  • @miciek

    Hot Seat LogicConcern #3

  • @miciek

    Player Interaction using ConsoleConcern #4

    How to transform Player[A] into IO[A]?

  • @miciek

    Player Interaction using ConsoleConcern #4

    How to transform Player[A] into IO[A]?

  • @miciek

    Player Interaction using ConsoleConcern #4

    How to transform Player[A] into IO[A]?

  • @miciek

    Player Interaction using ConsoleConcern #4

  • @miciek

    Connecting the Dots

    Hot SeatLogicComputing

    VerdictPlayer

    Interaction

    Player Communicationusing Console

    pure function Free[Player, A]

    Player[A] ~> IO[A]

    Free[S, Unit]

  • @miciek

    End of the World

  • @miciek

    End of the World

  • @miciek

    End of the World

  • @miciek

    Practical Problems with Free#1 Boilerplate

  • @miciek

    Freestyle

    which is built on top of Cats

  • @miciek

    After FreestyleNo Boilerplate Algebra

  • @miciek

    After FreestyleNo Boilerplate Interpreter

  • @miciek

    After FreestyleNo Boilerplate Interpreter

  • @miciek

    Playing with a Bot

    Adding new features is adding more concerns!

  • @miciek

    Playing with a Bot

    Welcome to Single Player Game!What's your name?> MichalHello, Michal!Michal, is WALL-E guilty?> nYour decision: SilenceVerdict for Michal is 1 year

    Adding new features is adding more concerns!

  • @miciek

    Reusing Values

    ComputingVerdict

    Player Interaction

    Player Communicationusing Console

    Unknown OpponentLogic

    Opponent Interaction

    #1 #2

    #3Bot

    Opponent

  • @miciek

    Opponent Interaction

    Freestyle generates all the case classes and other helpers!

  • @miciek

    Unknown Opponent Logic

  • @miciek

    Unknown Opponent Logic

  • @miciek

    Friend’s Reaction

  • @miciek

    End of the World

  • @miciek

    End of the World

  • @miciek

    End of the World

    ???

  • @miciek

    Bot Opponent

  • @miciek

    End of the World

  • @miciek

    Workflow

    1. Define operations2. Create programs3. Try to run them4. Fix errors by implementing interpreters

    Player Interaction

    Unknown Opponent

    Unknown Opponent

  • @miciek

    Different Interpreter for Opponent?

    ComputingVerdict

    Player Interaction

    Player Communicationusing Console

    Unknown OpponentLogic

    Opponent Interaction

    ? Bot Opponent

  • @miciek

    Remote Opponent

    ComputingVerdict

    Player Interaction

    Player Communicationusing Console

    Unknown OpponentLogic

    Opponent Interaction

    Remote Opponent

    Bot Opponent

  • @miciek

    Meeting Remote Opponent

    Player A Player B

    RegisterAsWaiting

    GetWaitingPlayers

    JoinWaitingPlayer

    CheckIfOpponentJoined

    UnregisterWaiting

    foundPlayer A

    foundPlayer B

  • @miciek

    Matchmaking

  • @miciek

    Matchmaking Programs

    Player A Player B

    waitForOpponent findOpponent

    RegisterAsWaiting

    GetWaitingPlayers

    JoinWaitingPlayer

    CheckIfOpponentJoined

    UnregisterWaiting

  • @miciek

    Find Opponent

  • @miciek

    Find Opponent

  • @miciek

    Remote Opponent

    ComputingVerdict

    Player Interaction

    Player Communicationusing Console

    Unknown OpponentLogic

    Opponent Interaction

    Remote Opponent

    Bot Opponent

  • @miciek

    Bot OpponentRecap:

  • @miciek

    Remote Opponent

    What should we interpret Opponent into?

    ???

  • @miciek

    Remote Opponent

    We should interpret Opponent into Free

  • @miciek

    Workflow

    1. Define operations2. Create programs3. Try to run them4. Fix errors by implementing interpreters

    Player Interaction

    Unknown Opponent

    Unknown Opponent

  • @miciek

    Recap: Game with a Bot

    Unknown OpponentLogic

    Opponent Interaction

    Bot Opponent

    var state: Map...

  • @miciek

    Interpreting into Free

    Unknown OpponentLogic

    Opponent Interaction

    Bot Opponent

    var state: Map...

    Remote Opponent is interpreting into

    another Free program!

    Remote Opponent

  • @miciek

    Multiplayer Game Mode

    Opponent Interaction

    Remote Opponent

    Matchmaking

    ?

    Let’s zoom in!

  • @miciek

    Multiplayer Game Mode

    Opponent Interaction

    Remote Opponent

    Matchmaking

    Remote Actor Matchmaker

  • @miciek

    MatchmakingRecap:

  • @miciek

    Matchmaking Server Handler

  • @miciek

    End of the World

  • @miciek

    End of the World

  • @miciek

    End of the World

  • @miciek

    3 Game Modes = 15 Concerns + 31 Tests

    ComputingVerdict

    Player Interaction

    Player Communicationusing Console

    Unknown OpponentLogic

    Opponent Interaction

    Remote Opponent

    Bot Opponent

    Decision Registry

    Matchmaking

    Matchmaking Server

    Decision Server Timing

    Hot SeatLogic

    EoW

    Thread Timing

    x3

    Player Local Interaction

  • @miciek

    3 Game Modes = 15 Concerns + 31 Tests

    ComputingVerdict

    Player Interaction

    Player Communicationusing Console

    Unknown OpponentLogic

    Opponent Interaction

    Remote Opponent

    Bot Opponent

    Decision Registry

    Matchmaking

    Matchmaking Server

    Decision Server Timing

    Hot SeatLogic

    EoW

    Thread Timing

    x3

    Player Local Interaction

    Thread.sleep

    printlnvar

  • @miciek

    Journey

    Prisoner’s Dilemma

    3 apps15 overlapping concernsusing Cats & Freestyle

    more Freestyle Tagless Final

  • @miciek

    FreestyleComposing Algebras using Modules

  • @miciek

    Pure CatsComposing Algebras using Coproducts

  • @miciek

    Pure Cats FreeCreating Lots of Objects

  • @miciek

    Journey

    Prisoner’s Dilemma

    3 apps15 overlapping concernsusing Cats & Freestyle

    more Freestyle Tagless Final

  • @miciek

    Final TaglessInstead of wrapping everything in Free

  • @miciek

    Final TaglessLet’s execute instructions directly!

    F can be anything!

  • @miciek

    Final Tagless using Freestyle

  • @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

  • @miciek

    @free or @tagless?

    @free @taglessprogram 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!

    https://softwaremill.com/free-tagless-compared-how-not-to-commit-to-monad-too-early/

  • @miciek

    Learn More

    https://github.com/miciek/free-prisoners6 apps / 3 in Free / 3 in Freestyle / 2 Akka actors / 31 tests

    https://github.com/miciek/free-prisoners

  • @miciek

    Michał Płachta

    www.michalplachta.com

    THANKS!

    Freestyle Free & Tagless

    https://github.com/miciek/free-prisoners6 pure apps / 15 concerns / 2 Akka actors / 31 tests

    @miciek

    https://github.com/miciek/free-prisoners