Algebraic Effects and Handlers

Algebraic effects represent operations with their continuations. An effect handler gives those operations meaning and determines whether each continuation runs zero, one, or many times.

Source
Anbloq
Language
English
Created
Updated
Chapters
3
Reading time
1 min
In this article3 chapters
  1. Operations and resumptions
  2. One operation admits several interpretations
  3. Effects change how programs compose

Operations and resumptions

An effectful computation can be represented as a tree. Leaves return values. A node stores an operation, its argument and a continuation from each possible result to the rest of the computation. A handler supplies a return case and interpretations of operations. For a deep handler, the resumption re-enters the handler.

\[ h(\operatorname{op}(a,k)) =h_{\operatorname{op}}\bigl(a,x\mapsto h(k(x))\bigr). \]

This is a form of folding over the operation tree. Its validity depends on the operation equations when effects are specified by a theory.

An effect handler interprets an operation and determines when its suspended computation resumes. Continuation-passing style provides a concrete representation of what remains to be done.

One operation admits several interpretations

A nondeterministic choice can return either of two values, one or two. Suppose the continuation adds ten. A list-collecting handler invokes both resumptions and returns the list eleven, twelve. A first-result handler uses one resumption and returns eleven. An exception-like interpretation may use none. If a resumption captures a resource that must be used once, running it twice requires a type or ownership discipline that makes duplication legitimate.

Effects change how programs compose

An effect-polymorphic function may preserve the unknown part of an effect row while adding or handling only specified operations. This permits libraries to abstract over effects without claiming purity. Handler order can be observable. State local to each nondeterministic branch is a different model from state shared across exploring branches. These observations interact with contextual equivalence, resource use in linear type systems, and code movement in staged compilation.[1]

References

  1. Gordon Plotkin and Matija Pretnar, Handling Algebraic Effects ↩