Polysemy: Introduction to Effect interpretation
In my last log we defined the Trace
effect:
data Trace (m :: Type -> Type) a where
Trace :: String -> Trace m ()
makeSem ''Trace
One of the main interest of effects systems is to be able to define multiple interpretations for the same effects.
The simplest interpreter we can write is to drop simply the action:
ignoreTrace = interpret $ \case
Trace _ -> pure ()
interpret
will "consume" Trace
contructor-by-constructor.
Note: we have a useful type alias InterpreterFor
for interpreters definitions.
However, Trace
is simple enough to have a dropping interpreter, let's come with a more useful definition:
traceToStdout = interpret $ \case
Trace m -> embed $ putStrLn m
We can also compose interpreters:
traceToStderr = traceToHandle stderr
traceToHandle handle = interpret $ \case
Trace m -> embed $ hPutStrLn handle m
See the full the code here.