Source-linked AI summary

On the Expressive Power of User-Defined Effects: Effect Handlers, Monadic Reflection, Delimited Control

Yannick Forster, Ohad Kammar, Sam Lindley, Matija Pretnar

arXiv:1610.09161v2cs.LOcs.PL

TL;DR

The paper asks how the expressive power of effect handlers, monadic reflection, and delimited control compares. It develops shared-core calculi and macro translations, finding equivalence without types but differences in typeability preservation.

  • Problem

    The paper asks how to compare the relative expressiveness of effect handlers, monadic reflection, and delimited control for user-defined computational effects.

    Source: §1 · p. 1
  • Method

    The authors extend a shared call-by-push-value core with three calculi and use Felleisen-style macro translations to compare their expressive power and typeability preservation.

    Source: §1 · pp. 2–3
  • Results

    All three abstractions are macro-expressible in one another without types, but neither monadic reflection nor delimited control macro-expresses effect handlers while preserving typeability.

    Source: §1 · p. 2, §7 · p. 24
  • Takeaways & Limitations

    Monadic reflection and delimited control have equivalent expressivity, while typeability makes effect handlers distinct in this formal comparison.

    Source: §7 · p. 24

    Among the remaining typed translation directions, the paper proves delimited control to monadic reflection and conjectures that the other directions do not preserve typeability.

    Source: §6.7 · p. 24

Abstract

from arXiv · show

We compare the expressive power of three programming abstractions for user-defined computational effects: Bauer and Pretnar's effect handlers, Filinski's monadic reflection, and delimited control without answer-type-modification. This comparison allows a precise discussion about the relative expressiveness of each programming abstraction. It also demonstrates the sensitivity of the relative expressiveness of user-defined effects to seemingly orthogonal language features. We present three calculi, one per abstraction, extending Levy's call-by-push-value. For each calculus, we present syntax, operational semantics, a natural type-and-effect system, and, for effect handlers and monadic reflection, a set-theoretic denotational semantics. We establish their basic meta-theoretic properties: safety, termination, and, where applicable, soundness and adequacy. Using Felleisen's notion of a macro translation, we show that these abstractions can macro-express each other, and show which translations preserve typeability. We use the adequate finitary set-theoretic denotational semantics for the monadic calculus to show that effect handlers cannot be macro-expressed while preserving typeability either by monadic reflection or by delimited control. We supplement our development with a mechanised Abella formalisation.

1 INTRODUCTION

The introduction frames a precise comparison of effect handlers, monadic reflection, and delimited control using extensions of a shared call-by-push-value core. It reports that all three are macro-expressible in one another without types, while typeability-preserving expressiveness differs.

1 INTRODUCTION

  • The motivating trade-off is that computational effects can sacrifice properties such as referential transparency and confluence, affecting reasoning and program transformations.The introduction names common subexpression elimination and code motion as transformations whose correctness-preserving applicability may be limited.Source: §1 · p. 1
  • Macro expressibility adapts Felleisen’s syntax-directed translation criterion to compare extensions of the same base calculus.The translation keeps the target language’s existing features fixed.Source: §1 · p. 2
  • Without types, all three abstractions macro-express one another through six translations, including a novel translation from monadic reflection to effect handlers.The effect-handler translation into delimited control improves on implementations requiring a global higher-order memory cell.Source: §1 · p. 2
  • Relative expressiveness depends on the calculi’s feature set; adding inductive types and primitive recursion would create gaps in the non-existence arguments.The authors conjecture that adding polymorphism could make some untyped translations typeability-preserving.Source: §1 · p. 3
  • The comparison studies user-defined effects through three formal calculi extending call-by-push-value with natural type-and-effect systems.The calculi provide syntax, semantics, and meta-theory for effect handlers, monadic reflection, and delimited control.Source: §1 · pp. 2–3
  • No typeability-preserving macro translation expresses effect handlers using either monadic reflection or delimited control.The paper also establishes typeability preservation from delimited control to monadic reflection.Source: §1 · pp. 2, 4

2 THE CORE-CALCULUS: MAM

The multi-adjunctive metalanguage (mam) extends call-by-push-value with a type-and-effect system and operational and set-theoretic semantics. Its development establishes safety, termination, soundness, and adequacy for the core calculus.

2 THE CORE-CALCULUS: MAM

  • mam extends call-by-push-value with a type-and-effect system, providing a common functional-imperative calculus for call-by-value and call-by-name.The calculus makes evaluation order explicit.Source: §2 · p. 4
  • Its syntax separates values from computations, including suspended computations, sequencing, and functions that take values as arguments.Computation products support product elimination in call-by-name languages.Source: §2 · p. 4
  • The operational semantics uses β-reduction and congruence, with evaluation contexts specifying the next computation position and yielding deterministic reduction.The core semantics is refactored to share definitions with the later calculus extensions.Source: §2 · p. 5
  • The boolean-state example uses state passing, globally threading state through the program; the paper distinguishes this from a macro translation.Later sections show each abstraction providing a different means of macro-expressing state.Source: §2 · p. 5
  • The type-and-effect system tracks effects that may occur; sequencing requires matching effect annotations, while return permits any annotation.The calculus omits effect coercion, subeffecting, and effect polymorphism.Source: §2 · p. 6
  • Well-typed mam programs are safe and terminate, reducing to a unique returned value.Safety is established by progress and preservation; termination excludes infinite reduction sequences.Source: §2 · p. 7
  • The set-theoretic semantics is sound and adequate: reduction preserves meaning, and denotational equivalence implies contextual equivalence.The semantics interprets types and terms using sets, monads, and algebra structures.Source: §2 · pp. 9–10

3 EFFECT HANDLERS: EFF

The eff calculus adds user-defined operations and deep handlers to mam. Its operational and set-theoretic semantics support proofs of safety, termination, soundness, and adequacy.

3 EFFECT HANDLERS: EFF

  • Effect operations expose user-defined effects, while handlers interpret operation calls and can resume the continuation at the call site.The handler is selected dynamically, and operation clauses receive both the supplied parameter and continuation.Source: §3 · p. 11
  • The boolean-state handler interprets get by returning the current state and put by installing the supplied state, then discards state on termination.The example reports that running the stateful toggle with True returns False and leaves True as the final result.Source: §3 · p. 11
  • Deep handlers rewrap the handler around a resumed continuation, so subsequent operations in that continuation are handled the same way.The paper focuses on deep handlers because they are closer to monadic reflection and have a clean denotational semantics.Source: §3 · p. 11
  • eff’s type system requires the handled operation set to agree exactly with the effect annotation, and handler clauses to match the declared parameter, return, and computation types.The calculus uses closed handlers; a handled computation that calls an unhandled operation halts or crashes, though a trivial handler can re-raise unhandled effects in this type system.Source: §3 · pp. 11–12
  • Well-typed eff programs are safe and terminate, reducing to a unique returned value.The paper gives an Abella formalisation of safety and derives termination using monadic lifting.Source: §3 · p. 13
  • The denotational semantics maps each effect to a signature-generated monad and each handler to an algebra structure plus a return-clause function.Operation calls denote signature terms; handling uses the handler function’s Kleisli extension.Source: §3 · pp. 13–14
  • The eff semantics is sound and adequate: reduction preserves denotation, and denotational equivalence implies contextual equivalence.The calculus also has compositional semantics, where a term’s meaning depends only on its subterms’ meanings.Source: §3 · p. 14

4 MONADIC REFLECTION: MON

Monadic reflection separates an effect’s opaque, native-like interface from its transparent monadic implementation, connecting them with reflect and reify. Its formal account includes a type-and-effect system and denotational semantics, with results applying to the proper fragment.

4 MONADIC REFLECTION: MON

  • Reflect grafts transparent implementation code into opaque-mode code, while reify turns opaque code into the result produced by the effect’s implementation.The opaque mode makes a user-defined effect appear native to the language.Source: §4 · p. 14
  • Monadic state uses concrete get and put functions, so its toggle example cannot abstract over how those operations are interpreted without a more sophisticated construction.With the initial state True, the example returns (True, False).Source: §4 · pp. 14–15
  • A monad is layered onto an effect stack with a type constructor and user-defined return and bind terms that provide its monad structure.The calculus uses structural, closed monads; it restricts reflection to the layer immediately below, a restriction described as not significantly changing expressiveness with the Filinski type system.Source: §4 · pp. 15–16
  • An acceptable operational interpretation of the monad laws remains an open problem, so the paper avoids it with a partial denotational semantics.Observational equivalence is not straightforward because contexts can define additional monads.Source: §4 · pp. 16–17
  • For proper mon terms, the paper establishes termination, soundness, and adequacy, with denotational equivalence implying contextual equivalence.The denotational semantics is finite for finite type-variable assignments.Source: §4 · pp. 17–18

5 DELIMITED CONTROL: DEL

The delimited-control calculus uses shift and reset to capture and delimit continuations, with effects represented as a stack of computation types. The paper presents a natural type system and establishes safety for well-typed programs.

5 DELIMITED CONTROL: DEL

  • Shift captures the current continuation and replaces it with its body; reset delimits captured continuations and runs a continuation after the delimited computation returns.The reset construct is also called dollar.Source: §5 · p. 18
  • The type system represents effects as a stack of computation types, where shift pops the top continuation type and reset pushes the delimited return type.The return type of the continuation remains fixed inside every reset in this system.Source: §5 · p. 19
  • The paper establishes safety: a closed, well-typed del computation either takes a well-typed step or is a returned value.The calculus inherits some meta-theory through its translation to monadic reflection.Source: §5 · p. 19

6 MACRO TRANSLATIONS

The paper compares the three calculi using local, syntax-directed macro translations between conservative extensions of a shared core. It gives six untyped translations and examines their simulation behavior and typeability.

6 MACRO TRANSLATIONS

  • Macro translation provides a local comparison of language extensions by preserving the shared core, extending Felleisen’s notion to translations between conservative extensions.The paper uses this notion to compare expressive power beyond computability and complexity reductions.Source: §6 · pp. 19–20
  • Of six untyped translations, three simulate source reductions directly, while the other three introduce suspended redexes and simulate only up to congruence.The directly simulating translations are mon→del, del→eff, and mon→eff; the remaining translations are del→mon, eff→del, and eff→mon.Source: §6 · p. 20

6.1 Delimited continuations as monadic reflection (del→mon)

  • The del→mon translation interprets shift as reflection and reset as reification in the continuation monad, and preserves typeability.Its simulation is up to congruence; the suspended redex arises when simulating reflection.Source: §6.1 · p. 20
  • The del→mon translation also supports a termination result: well-typed closed ground returners reduce to a unique normal form.The result extends the meta-theory of del.Source: §6.1 · p. 20

6.2 Monadic reflection as delimited continuations (mon→del)

The mon→del translation encodes monadic reflection using delimited continuations, but the presented translation does not preserve typeability. An alternative uses nested shifts and resets to handle reflection and reification.

6.2 Monadic reflection as delimited continuations (mon→del)

  • The mon→del translation implements reflection by capturing the continuation and abstracting over bind, and implements reification with reset.Its reset continuation contains the monad unit, and the reset is applied to the monad bind.Source: §6.2 · p. 21
  • The translation simulates monadic reflection up to congruence but does not preserve typeability because bind can be used at different types.The authors conjecture that another macro translation would share this issue and that predicative polymorphism could permit an adapted typeability-preserving translation.Source: §6.2 · p. 21

6.2.1 Alternative translation with nested delimited continuations.

  • An alternative uses two nested shifts for reflection and two nested resets for reification; an inner reset restores a continuation popped by the first shift.A shift guarding the unit also removes the bind once it is no longer needed.Source: §6.2 · p. 21, §6.2.1 · p. 21

6.3 Delimited continuations as effect handlers (del→eff)

The del→eff translation interprets shift as an operation and reset as a handler, and the effect-handler calculus simulates it on the nose. It does not preserve typeability when shifts within one reset are used at different types.

6.3 Delimited continuations as effect handlers (del→eff)

  • Effect handlers simulate delimited control on the nose under the del→eff translation.Shift is interpreted as an operation and reset as a straightforward handler.Source: §6.3 · p. 21
  • The translation fails to preserve typeability when shifts within one reset are used at different types.The authors conjecture polymorphic operations would suffice to make this translation typeability-preserving.Source: §6.3 · p. 21
  • A global translation can instead assign a separate operation to each static shift instance, avoiding polymorphic operations.Source: §6.3 · p. 21

6.4 Effect handlers as delimited continuations (eff→del)

The eff→del translation represents operation invocation by capturing a continuation and encoding the operation for a dispatcher, and represents handling with reset. Its simulation is up to congruence, and it does not preserve typeability; an alternative uses nested shifts and resets.

6.4 Effect handlers as delimited continuations (eff→del)

  • Operation invocation captures the current continuation and encodes the operation parameter with a wrapped continuation for a dispatcher.Handling uses reset around a continuation containing the return clause, with a dispatcher encoding the operation clauses.Source: §6.4 · p. 22
  • Delimited control simulates effect handlers up to congruence under eff→del.This translation is simpler than Kammar et al.’s approach, which uses a global higher-order memory cell to store the handler stack.Source: §6.4 · p. 22
  • The translation does not preserve typeability because operation interpretations must be polymorphic in the dispatcher’s return type.The authors conjecture that adding polymorphism to the base calculus would suffice to adapt it to a typeability-preserving translation.Source: §6.4 · p. 22

6.4.1 Alternative translation with nested delimited continuations.

  • An alternative eff→del translation uses two nested shifts for operations and two nested resets for handlers.Source: §6.4 · p. 22

6.5 Monadic reflection as effect handlers (mon→eff)

The mon→eff translation interprets reflection as an operation and reification as a handler whose unit and bind implement the handler and reflect operation. Effect handlers simulate monadic reflection on the nose, but the translation does not preserve typeability.

6.5 Monadic reflection as effect handlers (mon→eff)

  • Monadic reflection is translated by treating reflection as an operation and reification as a handler.The handler uses the monad’s unit, and its implementation of the reflect operation uses bind.Source: §6.5 · p. 22
  • Effect handlers simulate monadic reflection on the nose under mon→eff.Source: §6.5 · p. 22
  • The translation does not preserve typeability because reflection can occur at any type while a single operation is monomorphic.The authors conjecture that polymorphic operations would suffice to type this translation.Source: §6.5 · p. 23

6.6 Effect handlers as monadic reflection (eff→mon)

The eff→mon translation uses the continuation monad to encode captured continuations and dispatch effect operations; monadic reflection simulates handlers up to congruence. An alternative uses a free monad, but its recursive functions do not extend to the typed calculi as given.

6.6 Effect handlers as monadic reflection (eff→mon)

  • Operation invocation captures the current continuation through reflection and encodes it with the operation parameter for a dispatcher.Handling applies a reified continuation-monad computation to the return clause and a dispatcher encoding the operation clauses.Source: §6.6 · p. 23
  • The eff→mon translation uses the continuation monad in place of first-class continuations, and monadic reflection simulates handlers up to congruence.Source: §6.6 · p. 23
  • The translation does not preserve typeability because operation interpretations must be polymorphic in the dispatcher’s return type.The authors conjecture that adding polymorphism to the base calculus would suffice to adapt the translation.Source: §6.6 · p. 23

6.6.1 Alternative translation using a free monad.

  • The alternative free-monad translation uses recursive bind and interpretation functions implemented with a variation of the Y combinator.Source: §6.6.1 · p. 24
  • The free-monad translation does not extend to the typed calculi because they do not support recursion.The authors conjecture it could be adapted if the base calculus included inductive data types, since its recursive functions are structurally recursive.Source: §6.6.1 · p. 24

6.7 Nonexistence results

The section presents nonexistence results for macro translations and outlines a finite-denotation argument for one result. It also distinguishes an established typeability-preserving translation from conjectured absences.

6.7 Nonexistence results

  • Theorem 6.10 states that certain macro translations do not exist.The supplied theorem statement does not identify those translations.Source: §6.7 · p. 24
  • A finite-denotation argument makes two translated tick-term denotations observationally equivalent, although handlers distinguish the original terms.The terms share a type, and the assumed macro translation would carry the equivalence back to the effect-handlers calculus.Source: §6.7 · p. 24
  • The typed delimited-control-to-monadic-reflection translation preserves typeability, while three other typed translation directions are conjectured not to.Those conjectured directions are mon→del, del→eff, and mon→eff.Source: §6.7 · p. 24

7 CONCLUSION AND FURTHER WORK

The paper compares effect handlers, monadic reflection, and delimited control using formal calculi and finds monadic reflection and delimited control equally expressive. Without types, all three abstractions macro-express one another, but neither of the latter two can macro-express effect handlers while preserving typeability.

7 CONCLUSION AND FURTHER WORK

  • Monadic reflection and delimited control have equivalent expressive power; without types, all three abstractions macro-express one another, but the latter two cannot macro-express effect handlers while preserving typeability.The calculi include natural type-and-effect systems.Source: §7 · p. 24
  • The paper uses set-theoretic denotational semantics for inexpressivity results and formalises syntactic properties and translation correctness in Abella.The formalised properties include safety for each calculus.Source: §7 · p. 24
  • The authors propose extending the type systems and conjecture that polymorphism or inductive types with primitive recursion could enable additional typeability-preserving translations.The proposed extensions differ by calculus and translation direction.Source: §7 · p. 25
  • A proposed restricted calculus would pair each reify/reset with declared primitives, potentially enabling two-stage simulations into effect handlers that preserve typeability.The authors present this as a conjecture motivated by library designers exposing fixed primitives.Source: §7 · p. 25
  • The authors identify denotational models as a fruitful approach to inexpressivity proofs and propose applying similar techniques to compare local effects with effect handlers.They also point to game-semantic work on control operators and exceptions.Source: §7 · p. 25
Loading 1610.09161v2…