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:
traceToStdout
interpretsTrace
(after the application we haveSem '[Embed IO] a
)runM
lowers 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
stderr
withtraceToStderr
- Redirected to a
Handle
withtraceToHandle
- Dropped with
ignoreTrace
- Accumulated with
runTraceList
- etc.
See the full the code here.