One-Shot Algebraic Effects as Coroutines

This paper presents a translation from algebraic effects and handlers to asymmetric coroutines, which provides a simple, efficient and widely applicable implementation for the former. Algebraic effects and handlers are emerging as main-stream language technology to model effectful computations and attract attention not only from researchers but also from programmers. They are implemented in various ways as part of compilers, interpreters, or as libraries. We present a direct embedding of one-shot algebraic effects and handlers in a language which has asymmetric coroutines. The key observation is that, by restricting the use of continuations to be one-shot, we obtain a simple and sufficiently general implementation via coroutines, which are available in many modern programming languages. Our translation is a macro-expressible translation, and we have implemented its embedding as a library in Lua and Ruby, which allows one to write effectful programs in a modular way using algebraic effects and handlers.

[1]  Richard P. Gabriel The Design of Parallel Programming Languages , 1991, Artificial and Mathematical Theory of Computation.

[2]  KiselyovOleg,et al.  Freer monads, more extensible effects , 2015 .

[3]  Jonathan Immanuel Brachthäuser,et al.  Effekt: extensible algebraic effects in Scala (short paper) , 2017, SCALA@SPLASH.

[4]  Sam Lindley,et al.  Shallow Effect Handlers , 2018, APLAS.

[5]  Amr Sabry,et al.  Yield: Mainstream Delimited Continuations , 1983 .

[6]  R. Kent Dybvig,et al.  Representing control in the presence of one-shot continuations , 1996, PLDI '96.

[7]  Oleg Kiselyov,et al.  Eff Directly in OCaml , 2016, ML/OCAML.

[8]  Peter W. O'Hearn,et al.  Linear Continuation-Passing , 2002, High. Order Symb. Comput..

[9]  Andrej Bauer,et al.  Programming with algebraic effects and handlers , 2012, J. Log. Algebraic Methods Program..

[10]  Ohad Kammar,et al.  Handlers in action , 2013, ICFP.

[11]  Klaus Ostermann,et al.  Effect handlers for the masses , 2018, Proc. ACM Program. Lang..

[12]  Ohad Kammar,et al.  On the expressive power of user-defined effects: effect handlers, monadic reflection, delimited control , 2016, Journal of Functional Programming.

[13]  Gordon D. Plotkin,et al.  Algebraic Operations and Generic Effects , 2003, Appl. Categorical Struct..

[14]  Ohad Kammar,et al.  On the expressive power of user-defined effects: Effect handlers, monadic reflection, delimited control , 2019, J. Funct. Program..

[15]  Dariusz Biernacki,et al.  Subtyping delimited continuations , 2011, ICFP.

[16]  Matthias Felleisen,et al.  Control operators, the SECD-machine, and the λ-calculus , 1987, Formal Description of Programming Concepts.

[17]  Olivier Danvy,et al.  Abstracting control , 1990, LISP and Functional Programming.

[18]  Roberto Ierusalimschy,et al.  Revisiting coroutines , 2009, TOPL.

[19]  Matthias Felleisen,et al.  On the Expressive Power of Programming Languages , 1990, European Symposium on Programming.

[20]  Frank Piessens,et al.  A programming model for concurrent object-oriented programs , 2008, TOPL.

[21]  LindleySam,et al.  Handlers in action , 2013 .

[22]  Gordon D. Plotkin,et al.  Handling Algebraic Effects , 2013, Log. Methods Comput. Sci..

[23]  Oleg Kiselyov,et al.  Freer monads, more extensible effects , 2015, Haskell.

[24]  P. Jorrand Design of parallel programming languages , 1990 .

[25]  Daan Leijen Algebraic Effects for Functional Programming , 2016 .