I’ve been reading about algebraic effects lately, and while I lack sufficient background in computer science to fully understand articles about them, I think a picture is beginning to emerge in my head. (mostly after looking at the Eff examples.)

They remind me very much of monadic effects in Haskell (and monads in general), but I have difficulty articulating exactly what the relationship between the two effect systems are.

It’s like they both provide a… programmable context (or something)… for effects. At one point I was about ready to say “algebraic effects are just a certain type of monad,” because are so similar in function, but I really have trouble seeing how monad laws apply to algebraic effects, if at all…

*edit: forgot to mention this*

1 Like

[I’m not an expert on algebraic effects/effect system.]

Monads can be used to model some effects, but not all that people are interested in. Algebraic effects are more general.

Do you have some examples of effects that monads can’t model? My gut feeling is that monads are more general if anything—though perhaps not, since the continuation passing style used in algebraic effects could be used for almost anything, just the terminology makes it sound like it’s limited to effects. I think there could be other potential usecases for, eh… “algebraic continuations”, but managing effects is probably the most practical.

Sorry if this is a bit terse - I want to respond quickly, but don’t have much time right now. If you have any further questions I am happy to answer them. There are a number of similarities and differences between monads and algebraic effects.

The most obvious practical difference is where the effectful behaviour is defined. For monads, it is in the definition of bind and return, which in turn statically determine the behaviour of the monadic code inside the `do`

block. In contrast, code that uses algebraic effects can call effectful operations that by themselves carry no inherent behaviour. Instead, the behaviour is determined dynamically by the nearest encompassing handler. This gives greater flexibility in the composition of effectful code.

You are correct in thinking that algebraic effects are monadic. Effect handlers are *macro-expressible* in terms of monads and vice-versa (see http://homepages.inf.ed.ac.uk/slindley/papers/effmondel-jfp.pdf). Macro expressivity means that you can translate any program using handlers into a monadic one by doing local structural translation, keeping all other constructs the same (a counterexample would be a CPS translation). There is a slight mismatch between the two constructs as each one is naturally polymorphic (in the same way say that branches in `if-then-else`

can have an arbitrary type even if your language does not support polymorphism), but each one is polymorphic in its own way. Therefore, the obvious translation between the two cannot be typed, though if you add sufficient polymorphism to your language, it could be.

15 Likes

Thanks for this great answer!