Preamble
I’ve been trying for a long time to fix certain problems with implicit parameters (see Why not implicit parameters? - #15 by lortabac) without succeeding completely.
Even though imperfect, I have a library that addresses at least some of the issues: GitHub - lortabac/parameters: Implicit parameters with Reader semantics
This library provides a Reader-like interface:
-
HasParam
is a constraint that shows that a parameter is in scope -
runParam
introduces a parameter -
ask
retrieves the value of a parameter -
local
overrides the parameter locally (support is partial due to unsolved issues)
The effect system
While working on this library I realized that once you have implicit parameters and withDict
you can create a very simple yet powerful effect system. I called this effect system parameters-fx
.
parameters-fx
provides an IO
wrapper called Fx
. Fx
is really just a newtype over IO
except that the constructor is not exported, so if you want to run IO you need to use one of the smart constructors.
The types of the smart constructors ensure that each Fx
action must require a constraint.
There are three ways to construct an Fx
action:
- If the action depends on an implicit parameter:
fx :: forall p a r. (HasParam p a) => ((HasParam p a) => IO r) -> Fx r
- If the action doesn’t depend on an implicit parameter:
fxNoParam :: forall eff r. (HasEffect eff) => ((HasEffect eff) => IO r) -> Fx r
- If you want to use a “catch-all” constraint (support for this method needs to be enabled explicitly):
embedIO :: (HasIO) => IO a -> Fx a
That’s it. The whole effect system is 23 lines of code: parameters/parameters-fx/src/Param/Fx.hs at master · lortabac/parameters · GitHub
State
and Error
effects are provided for convenience. I think other effects should be implemented as separate libraries.
To be honest I don’t have a big interest in effect systems. But I discovered this trick and thought it may be useful. If there is enough interest I may decide to keep the project alive otherwise it will have been a fun experiment.
Please tell me what you think.