One-Shot Algebraic Effects as Coroutines
- 3 Mentions
- 389 Downloads
Abstract
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.
Keywords
Algebraic effect and handler Coroutine Continuation Control operator Macro expressibilityNotes
Acknowledgement
We are grateful for the reviewers of earlier versions of this paper for constructive comments and numerous suggestions. The second author is supported in part by JSPS Grants-in-Aid (B) 18H03218.
References
- 1.Bauer, A., Pretnar, M.: Programming with Algebraic Effects and Handlers. J. Log. Algebr. Methods Program. 84, 108–123 (2012)MathSciNetzbMATHCrossRefGoogle Scholar
- 2.Berdine, J., O’Hearn, P., Reddy, U., Thielecke, H.: Linear continuation-passing. Higher-Order Symbolic Comput. 15, 181–208 (2002). https://doi.org/10.1023/A:1020891112409zbMATHCrossRefGoogle Scholar
- 3.Brachthäuser, J., Schuster, P.: Effekt: extensible algebraic effects in Scala (short paper). In: Proceedings of the 8th ACM SIGPLAN Symposium on Scala, pp. 67–72 (2017)Google Scholar
- 4.Brachthäuser, J., Schuster, P., Ostermann, K.: Effect handlers for the masses. Proc. ACM Program. Lang. 2, 1–27 (2018)CrossRefGoogle Scholar
- 5.Bruggeman, C., Waddell, O., Dybvig, R.: Representing control in the presence of one-shot continuations, vol. 31, pp. 99–107 (1970)Google Scholar
- 6.Danvy, O., Filinski, A.: Abstracting control. In: Proceedings of the 1990 ACM Conference on LISP and Functional Programming, pp. 151–160 (1990)Google Scholar
- 7.Dolan, S., White, L., Madhavapeddy, A.: Multicore OCaml. In: OCaml Users and Developers Workshop (2014)Google Scholar
- 8.Felleisen, M.: On the Expressive Power of Programming Languages. In: Selected Papers from the Symposium on 3rd European Symposium on Programming, ESOP 1990, pp. 35–75. Elsevier North-Holland Inc., USA (1991)Google Scholar
- 9.Felleisen, M., Friedman, D.P.: Control operators, the SECD-machine, and the \(\lambda \)-calculus. In: Formal Description of Programming Concepts (1987)Google Scholar
- 10.Forster, Y., Kammar, O., Lindley, S., Pretnar, M.: On the expressive power of user-defined effects: Effect handlers, monadic reflection, delimited control. J. Funct. Program. 29, e15 (2019). https://doi.org/10.1017/S0956796819000121
- 11.Gabriel, R.P.: The Design of Parallel Programming Languages, pp. 91–108. Academic Press Professional Inc., Cambridge (1991)zbMATHGoogle Scholar
- 12.Hillerström, D., Lindley, S.: Shallow effect handlers. In: Ryu, S. (ed.) APLAS 2018. LNCS, vol. 11275, pp. 415–435. Springer, Cham (2018). https://doi.org/10.1007/978-3-030-02768-1_22CrossRefGoogle Scholar
- 13.James, R., Sabry, A.: Yield: mainstream delimited continuations. In: Proceedings of Workshop on the Theory and Practice of Delimited Continuations, pp. 1–12 (2011)Google Scholar
- 14.Kammar, O., Lindley, S., Oury, N.: Handlers in action. In: ACM SIGPLAN International Conference on Functional Programming, pp. 145–158 (2013)Google Scholar
- 15.Kiselyov, O., Ishii, H.: Freer monads, more extensible effects. In: ACM SIGPLAN International Symposium on Haskell, pp. 94–105 (2015)Google Scholar
- 16.Kiselyov, O., Sivaramakrishnan, K.: Eff directly in OCaml. Electron. Proc. Theor. Comput. Sci. 285, 23–58 (2018)CrossRefGoogle Scholar
- 17.Leijen, D.: Algebraic effects for functional programming. Technical report, p. 15 (2016)Google Scholar
- 18.Leijen, D.: Implementing algebraic effects in C. In: Chang, B.-Y.E. (ed.) APLAS 2017. LNCS, vol. 10695, pp. 339–363. Springer, Cham (2017). https://doi.org/10.1007/978-3-319-71237-6_17CrossRefGoogle Scholar
- 19.Materzok, M., Biernacki, D.: Subtyping delimited continuations. In: Chakravarty, M.M.T., Hu, Z., Danvy, O. (eds.) Proceeding of the 16th ACM SIGPLAN International Conference on Functional Programming, pp. 81–93. ACM (2011). https://doi.org/10.1145/2034773.2034786
- 20.Moura, A.D., Ierusalimschy, R.: Revisiting coroutines. ACM Trans. Program. Lang. Syst. 31, 1–31 (2004)CrossRefGoogle Scholar
- 21.Plotkin, G., Power, J.: Algebraic operations and generic effects. Appl. Categor. Struct. 11, 69–94 (2003). https://doi.org/10.1023/A:1023064908962MathSciNetzbMATHCrossRefGoogle Scholar
- 22.Plotkin, G., Pretnar, M.: Handling algebraic effects. Log. Methods Comput. Sci. 9, 1–36 (2013)MathSciNetzbMATHCrossRefGoogle Scholar
- 23.Pretnar, M., Saleh, A.H., Faes, A., Schrijvers, T.: Efficient compilation of algebraic effects and handlers. CW reports, volume CW708, Department of Computer Science, KU Leuven (2017)Google Scholar