A software designer website

Polysemy: Design heuristics: Grouping interpreters

Gautier DI FOLCO April 05, 2023 [Haskell] #haskell #polysemy #design #effects systems

In our previous log, we ended up with a lot of interpreters:

intrepretUserRegistration :: Member InternalUserEffect r => InterpreterFor UserRegistration r
intrepretUserRegistration =
  interpret $
      SignUp userInfo password -> internalSignUp userInfo password
      ConfirmSignUp confirmationInfo -> internalConfirmSignUp confirmationInfo

intrepretUserAuthenticationManagement :: Member InternalUserEffect r => InterpreterFor UserAuthenticationManagement r
intrepretUserAuthenticationManagement =
  interpret $
      SignIn emailAddress password -> internalSignIn emailAddress password
      SignOut token -> internalSignOut token

intrepretUserAuthenticationCheck :: Member InternalUserEffect r => InterpreterFor UserAuthenticationCheck r
intrepretUserAuthenticationCheck =
  interpret $
      AuthenticateUser token -> internalAuthenticateUser token

We can leverage InterpretersFor to provide a single (and simpler) interpreter:

interpretAllUserEffectsCognito :: InterpretersFor '[UserRegistration, UserAuthenticationManagement, UserAuthenticationCheck] r
interpretAllUserEffectsCognito =
    . intrepretUserAuthenticationCheck
    . intrepretUserAuthenticationManagement
    . intrepretUserRegistration
    . raise3Under @InternalUserEffect

See the full the code here.