Comment by Sharlin

8 hours ago

(2019)

It’s worth clarifying that for the most part, this article just discusses plain effects, ie. "reified side effects" or "resumable exceptions". Algebraic effects are about the composition (ie. algebra) of effects, exactly like algebraic data types are about the composition of types. This part is generally not meaningful in an untyped language like Javascript because effects are all YOLO and you never know what’s going to happen, what effects a function might throw, or whether there’s any handler up-stack to catch your effect.

I've never understood what's meant by "algebraic" in that sense, or what this algebra of combining effect is. Could you say more, or maybe share a link to a useful reference?

  • Here is an article by Andrej Bauer answering the exact question, "What is algebraic about algebraic effects and handlers?": https://arxiv.org/abs/1807.05923v2

    Contrary to the claim from the comment you are replying to, according to Andrej Bauer, "algebraic" does not refer to the composition of different algebraic effects via composing their handlers.

    When you see "algebra," think "algebraic structure," i.e., a set of operations and a set of equational laws on those operations.

    An algebraic effect consists of a set of effectful operations. In that sense, each algebraic effect (reader, state, etc.) defines its own algebraic structure. In theory, the operations of an effect should also be related to each other by laws. Here is an example for the state effect from Andrej Bauer's paper:

        lookup(ℓ,λs.lookup(ℓ,λt.κst)) = lookup(ℓ,λs.κss)
        lookup(ℓ,λs.update((ℓ,s),κ)) = κ()
        update((ℓ,s),λ_.lookup(ℓ,κ)) = update((ℓ,s),λ_.κs)
        update((ℓ,s),λ_.update((ℓ,t),κ)) = update((ℓ,t),κ)
    

    Therefore, the state effect has an algebraic structure.

    However, monads that cannot be defined in this equational style cannot be translated to algebraic effects. An example is the continuation monad: https://old.reddit.com/r/haskell/comments/44q2xr/is_it_possi... (I think you are involved in the Reddit comment chain that I'm linking to...)

    AFAIK, another example is the "exception" monad, because the way that you interact with it is through the handler itself. I once saw a thread on r/Haskell discussing this, but can't find it.

  • This is my favorite one, short and understandable: https://philipnilsson.github.io/Badness10k/posts/2017-05-07-...

    I show it when I teach Haskell, and it's what usually makes it "click" for students. Probably because motivating examples are in normal imperative pseudocode.

    • One thing this page makes clear is that do-syntax could mean all kinds of things, which seems like a disadvantage for readability. Assuming you know the specialized syntax, elvis operators looking different from async code or a nested for loop seems like an advantage? The performance implications are entirely different.

    • Your linked article hints at the advantages of using Monads and therefor ADTs (Algebraic Data Types), and does it really well.

      The wiki entry on effect systems[0] tells me that a focus of an effect system is something different from a focus of monads. "The term algebraic effect follows from the type system", where an effect system is effectively a type and effect system. It links to Monadic encapsulation of effects[1] and mentions the runST monad when it mentions support in Haskell, as that one seem to "simulate a type and effect system".

      Do have any such a link on the runTS monad?

      [0]: https://en.wikipedia.org/wiki/Effect_system

      [1]: https://www.cambridge.org/core/journals/journal-of-functiona...

    • Thanks, I definitely feel like I understand monads and their benefits, and even "effects", but I'm not sure what's "algebraic" in "algebraic effects".

      1 reply →

  • I'm no expert, but perhaps https://arxiv.org/abs/2302.01415 (especially section 2.2, "Non-Algebraic Effects") will help.

    • Thanks. It says

      > Algebraic effects & handlers use a free monad and an interpreter to separate the syntax of effects from their semantics

      I'm pretty sure not everybody who works with algebraic effects would say they have to be based on a free monad, so I'm skeptical how definitive this definition is.