Polysemy: Introduction to interpreters
As seen in the previous log we have an expression as follows:
displayFile path = do
trace $ "Displaying " <> path
content <- embed $ readFile path
embed $ putStr content
return $ length content
In order to do something useful we need to interpret them in term of IO:
intrDisplayFile =
runM
. traceToStdout
It can be read that way:
traceToStdoutinterpretsTrace(after the application we haveSem '[Embed IO] a)runMlowers theEmbed IO
One of the benefit of effects systems is to be able to change the behavior (via the interpretation) without changing the expression.
As such Trace could be:
- Redirected to
stderrwithtraceToStderr - Redirected to a
HandlewithtraceToHandle - Dropped with
ignoreTrace - Accumulated with
runTraceList - etc.
See the full the code here.