A software designer website

Polysemy: Bundle

Gautier DI FOLCO February 08, 2023 [Haskell] #haskell #polysemy #design #effects systems

Polysemy comes with an useful effect to group other effects: Bundle.

It comes with a useful function:

sendBundle :: forall e r' r a. (Member e r', Member (Bundle r') r) => Sem (e ': r) a -> Sem r a

It can be used as follows:

act :: Members '[Bundle '[Trace]] r => Sem r ()
act = do
  sendBundle @Trace @'[Trace] $ trace "Hello"

The real benefit associated with Bundle are the interpreters.

runBundle injects Bundles' effects:

intrHidden :: Sem '[Bundle '[Trace], Embed IO] a -> IO a
intrHidden = runM . traceToStdout . runBundle

While subsumeBundle reuse interpreters:

intrExplicit :: Sem '[Bundle '[Trace], Trace, Embed IO] a -> IO a
intrExplicit = runM . traceToStdout . subsumeBundle

See the full the code here.