Marco Perone
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- type: slide title: crem tags: talks slideOptions: progress: true controls: false slideNumber: false --- <style> .reveal .code-wrapper {width: fit-content; max-width: 200%; font-size: xxx-large;} .reveal img {max-width: 200%;} .reveal pre {max-height: 600px;} .reveal pre code {max-height: 600px; padding: 10px !important;} .reveal .hljs-ln-numbers {display: none;} code {color: #c7254e;} .machine {position: relative; left: -100px;} .actionresult {position: relative; left: -70px;} .topology {position: relative; left: -50px;} .reveal table th, .reveal table td {border-right: 1px solid; border-color: white;} .reveal table thead tr th:last-child, .reveal table tbody tr td:last-child {border-right: none;} .reveal table tr td:first-child {font-weight: bold;} .highlight {color: #d9480f} .mermaid {text-align: center !important; background-color:#2d2d2d !important;} </style> # [`Crem`](https://github.com/tweag/crem) ## Composable Representable Executable Machines <img src="https://raw.githubusercontent.com/tweag/crem/main/logo/crem-transparent.png" width="200"> --- ![the picture that explains everything](http://marcosh.github.io/img/the-picture-that-explains-everything.png) --- ### <span style="color: orange">Domain events</span> ![event](https://github.com/marcosh/ddd-machines-dddeurope/raw/main/images/the-picture-that-explains-everything-event.png) Events relevant for domain experts --- ### <span style="color: dodgerblue">Commands</span> ![command](https://github.com/marcosh/ddd-machines-dddeurope/raw/main/images/the-picture-that-explains-everything-command.png) User intentions/actions/decisions --- ### <span style="color: lightyellow">Read models</span> ![read model](https://github.com/marcosh/ddd-machines-dddeurope/raw/main/images/the-picture-that-explains-everything-read-model.png) Data needed in order to make decisions --- ### <span style="color: yellow">Aggregates</span> ![aggregate](https://github.com/marcosh/ddd-machines-dddeurope/raw/main/images/the-picture-that-explains-everything-aggregate.png) Decide what happens on commands --- ### <span style="color: orchid">Policies</span> ![policy](https://github.com/marcosh/ddd-machines-dddeurope/raw/main/images/the-picture-that-explains-everything-policy.png) Reactive logic that takes place after an event --- ### <span style="color: lightgreen">Projections</span> ![projection](https://github.com/marcosh/ddd-machines-dddeurope/raw/main/images/the-picture-that-explains-everything-projection.png) Aggregate data from events --- ## <span style="color: yellow">Aggregates</span>, <span style="color: lightgreen">projections</span> and <span style="color: orchid">policies</span> could be implemented as state machines --- ### <span style="color: yellow">Aggregates</span> ![aggregate-command-event](https://github.com/marcosh/ddd-machines-dddeurope/raw/main/images/the-picture-that-explains-everything-aggregate-command-event.png) From <span style="color: dodgerblue">commands</span> to <span style="color: orange">events</span> --- ### <span style="color: lightgreen">Projections</span> ![projection-event-read-model](https://github.com/marcosh/ddd-machines-dddeurope/raw/main/images/the-picture-that-explains-everything-projection-event-read-model.png) From <span style="color: orange">events</span> to <span style="color: lightyellow">read models</span> --- ### <span style="color: orchid">Policies</span> ![policy-event-command](https://github.com/marcosh/ddd-machines-dddeurope/raw/main/images/the-picture-that-explains-everything-policy-event-command.png) From <span style="color: orange">events</span> to <span style="color: dodgerblue">commands</span> --- ## How should we encode a state machine? --- ### Mealy machines ```haskell [1|2|3] data Mealy state input output = Mealy { initialState :: state , action :: state -> input -> (state, output) } ``` --- ### [Mealy machines](https://hackage.haskell.org/package/machines-0.7.3/docs/Data-Machine-Mealy.html#t:Mealy) ```haskell [1|2-4] newtype Mealy input output = Mealy { runMealy :: input -> (output, Mealy input output) } ``` --- ## Machines are composable --- ```haskell sequential :: Mealy a b -> Mealy b c -> Mealy a c ``` ```mermaid %%{init: {'theme':'dark'}}%% stateDiagram-v2 direction LR a --> b b --> c ``` --- ```haskell parallel :: Mealy a b -> Mealy c d -> Mealy (a, c) (b, d) ``` ```mermaid %%{init: {'theme':'dark'}}%% stateDiagram-v2 direction LR state fork <<fork>> (a,c) --> fork fork --> a fork --> c a --> b c --> d state join <<join>> b --> join d --> join join --> (b,d) ``` --- ```haskell alternative :: Mealy a b -> Mealy c d -> Mealy (Either a c) (Either c d) ``` ```mermaid %%{init: {'theme':'dark'}}%% stateDiagram-v2 direction LR eitherac: Either a c state fork <<choice>> eitherac --> fork fork --> a fork --> c a --> b c --> d state join <<choice>> b --> join d --> join join --> eitherbd eitherbd: Either b d ``` --- ```haskell feedback :: Mealy a [b] -> Mealy b [a] -> Mealy a [b] ``` ```mermaid %%{init: {'theme':'dark'}}%% stateDiagram-v2 direction LR a --> b: [] b --> a: [] ``` --- ## This encoding can only be run note: Once we defined a machine, the only way to extract information about it, is to run it, giving it inputs and observing its outputs --- ### What about documentation? --- ### What about invariant enforcement? --- ## Strengthening the type ```haskell data Machine (topology :: Topology vertex) input output ``` --- ```haskell {-# LANGUAGE DataKinds #-} newtype Topology vertex = Topology { edges :: [(vertex, [vertex])] } ``` --- ### Topology ```mermaid %%{init: {'theme':'dark'}}%% stateDiagram-v2 NoData --> CollectedUserData CollectedUserData --> CollectedLoanDetailsFirst CollectedUserData --> ReceivedCreditBureauDataFirst CollectedLoanDetailsFirst --> CollectedAllData ReceivedCreditBureauDataFirst --> CollectedAllData ``` --- ### Adding the `Topology` in the type --- Allows us to enforce execution of allowed transitions --- ```haskell [1|2|3|4-8] data Machine topology a b = forall state. Machine { initialState :: InitialState state , action :: forall initialVertex . state initialVertex -> a -> ActionResult topology state initialVertex b } ``` <!-- .element: class="machine" --> --- ```haskell [1|2|4|3] data ActionResult topology state initial b where ActionResult :: AllowedTransition topology initial final => (b, state final) -> ActionResult topology state initial b ``` <!-- .element: class="actionresult" --> --- Allows us to retrieve information about our state machine without running it --- ```haskell [1|7-8|9|3-5] topology :: forall vertex topology a b . ( Demote vertex ~ vertex , SingKind vertex , SingI topology ) => Machine (topology :: Topology vertex) a b -> Topology vertex baseMachineTopology _ = demote @topology ``` <!-- .element: class="topology" --> --- ### But... --- ### Composition becomes harder ```haskell sequential :: Machine t1 a b -> Machine t2 b c -> Machine ??? a c ``` --- Requires computation at the type level --- Breaks usage of standard typeclasses like `Arrow` or `Category` --- ## Can we get the best of both worlds? --- ```haskell data StateMachine input output where ``` --- ```haskell [1|2-4] data StateMachine input output where Basic :: Machine topology input output -> StateMachine input output ``` --- ```haskell [1|2-3] foo :: StateMachine input output -> a foo stateMachine = case stateMachine of Basic machine -> _ ``` --- ```haskell [3-6] data StateMachine input output where ... Sequential :: StateMachine a b -> StateMachine b c -> StateMachine a c ``` --- ```haskell [3-6] data StateMachine input output where ... Parallel :: StateMachine a b -> StateMachine c d -> StateMachine (a, c) (b, d) ``` --- ```haskell [3-6] data StateMachine input output where ... Alternative :: StateMachine a b -> StateMachine c d -> StateMachine (Either a c) (Either b d) ``` --- ```haskell [3-6] data StateMachine input output where ... Feedback :: StateMachine a [b] -> StateMachine b [a] -> StateMachine a [b] ``` --- ```mermaid %%{init: {'theme':'dark'}}%% graph TD A[Alternative] --> B[Parallel] A --> C[Sequential] B --> D[Machine 1] B --> E[Machine 2] C --> F[Machine 3] C --> G[Machine 4] ``` --- ## Composable --- ```haskell [1|2|4] instance Category StateMachine where id = Basic identity (.) = flip Sequential ``` --- ```haskell [1|2] instance Strong StateMachine where second' = Parallel id ``` --- ```haskell [1|2] instance Choice StateMachine where right' = Alternative id ``` --- ## Executable --- ```haskell run :: StateMachine a b -> a -> (b, StateMachine a b) ``` --- ```haskell [1|2-3] run (Basic machine) input = _ -- use the `action` -- with `initialState` and `input` ``` --- ```haskell [1|3-4|5-6|8-9] run (Sequential machine1 machine2) input = let (output1, machine1') = run machine1 input (output2, machine2') = run machine2 output1 in ( output2 , Sequential machine1' machine2' ) ``` --- ```haskell [1|3-4|5-6|8-9] run (Parallel machine1 machine2) input = let (output1, machine1') = run machine1 (fst input) (output2, machine2') = run machine2 (snd input) in ( (output1, output2) , Parallel machine1' machine2' ) ``` --- ## Representable --- ```haskell [1-2|4] newtype Mermaid = Mermaid {getText :: Text} render :: StateMachine a b -> Mermaid ``` --- ```haskell [1|2] render (Basic machine) = renderTopology (topology machine) ``` --- ```haskell [1|3|4|6-8] render (Sequential machine1 machine2) = let mermaid1 = render machine1 mermaid2 = render machine2 in mermaid1 <> Mermaid " --> " <> mermaid2 ``` --- ### We can draw our machines --- <img alt="risk manager" src="https://mermaid.ink/svg/pako:eNqlVF1PwjAU_SvkPmkyCGCBbQ8mCvqkxkj0wSwhzXad1a4lXUdAsv9u9-HcCMgMfepOz73n3PbubsGXAYILsaYaZ4yGikbd1dATHbNysEPDUGGY7bYFnK0KXDzIGdX0BZXG9b7zqeQcfY3Bc4yqJfVOUjFDTRmPb5mK9eGQJ_SRrTCYKgyYvk4U0iQTORJWKV1x_renenmdbvfyxMqa1EMJ29ffLvGpt7TfzyH3R--0hZ1_5E7rzbqUnPmbeqcWyOLsvMH-bepMqOAUJ2WGhn5DQckPY4VJ0VCp0MWciZDjYwXs8bqjXlHrOpEvhU91XaSEdmupOcrSlSywIEIVURaY3ztP4oF-xwg9cM02oOrTA0-khpcsA6N4Y95CKnDfKI_RAppoOd8IH1ytEvwhlSOiYi2peJWy8Q3uFtbgkv6oN7ZtZ0xscjEZWbABdziY9IjjODaZkMFw0HdIasFXHt_vGQrmFu6LkZRPpvQbS36R_g" height="600"> --- ## Demo time ![the hobbit](https://ready64.org/giochi/full/h/hobbit_01.png) [The Hobbit code](https://github.com/tweag/crem/blob/main/examples/Crem/Example/TheHobbit.hs) [The Hobbit map](https://mermaid.live/edit#pako:eNp9ksFuwjAMhl-l8rlFUNrS5rDLNmkHkKZtpykXi3gQkTqoTdEY4t0Xyqhox3aKrf-L_Sf2AZZWEQioHTp60LiqsIx2sWTJbw0zmbne0BMaI3lufYqsai9V1pj63hBWmleSX_SOWNGJWuja7Re2YYeaL-QzunV3C3c0LB5E0V1wVb8LW-FPI2d14KWft8iVvRvqtcWOvFm6rw5e2ktvtz31-MfzUD5_VBf_MgshlFSVqJUf4EFyEEhwaypJgvChwmojQfLRc81W-fk-Ku1sBeIDTU0hYOPs656XIFzV0AX6WYKO2iK_W9vLQRzgE0RUTIpsNB4XsyzN4jSEPYikiEdpPomLPI_9kR5D-GpvF6NZkuTTdDpJsmk-G3ucWjuL8wK2e3j8Bgbo5M8) note: `cabal run hobbit-game` `cabal run hobbit-map` --- ## That's all! --- ## Questions and feedback :scroll: [marcosh.github.io](http://marcosh.github.io/) :bird: [@marcoshuttle](https://twitter.com/marcoshuttle) :elephant: [@marcosh@functional.cafe](https://functional.cafe/@marcosh)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully