r/purescript • u/natefaubion • Apr 17 '17
purescript-run: an extensible-effects implementation
https://github.com/natefaubion/purescript-run
13
Upvotes
r/purescript • u/natefaubion • Apr 17 '17
View all comments
Show parent comment
2
u/natefaubion Apr 23 '17
Sure!
Effin PureScript is equivalent toIOin Haskell, it only carries around a phantom type parameter that merely tracks the effects that takes place (as defined by the effect authors). There is no flexibility there, asEffis just an opaque blob of code that the runtime executes.Rundoes not replaceEff, rather it augments it. In the end,Effis how we run all effects that interact with the outside world, so if that's what you want to do, you'll have to get toEffeventually. But whatRunlets you do is compose algebraic effects. that just means they aren't opaque anymore. Instead you can think of them as instructions which you provide an interpreter for. The "effects" are just a sequence of data as defined by some algebraic data type.Interpretation means you can write the business logic once, and change what it does depending on the context. For example, I might write a database effect that uses mock data for testing and real data for production. These can then be swapped out without changing the core logic.
It's straightforward enough to do this without
Runby just usingFreedirectly and your own algebra. What gets hairy, however, is what you do when you want to compose many different algebras together. The only thing you can really do is define another data type that subsumes all the effects you want to combine, and then lift them all into place. This can be mitigated a somewhat by using MTL-like typeclasses for all your different algebras.All
Rundoes is take theFreeapproach and combine it with polymorphic variants. That just means you can lift different algebras into theRunMonad, and you don't have to define an additional monolithic algebra to subsume all the effects you might want to run. Each effect can be interpreted individually either purely (likeStateorExcept) or into the chosen base effect (likeEfforAff).