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.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
The Wikipedia article on coroutine (access date: June 1, 2020) lists fifty programming languages which have native support for coroutines.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
Strictly speaking, our calculus is the one for stackful asymmetric coroutines according to de Moura and Ierusalimschy’s classification.
- 9.
See Sect. 5.3 in [20].
- 10.
Felleisen considers the case where each argument may be bound by the construct.
- 11.
- 12.
- 13.
References
Bauer, A., Pretnar, M.: Programming with Algebraic Effects and Handlers. J. Log. Algebr. Methods Program. 84, 108–123 (2012)
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:1020891112409
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)
Brachthäuser, J., Schuster, P., Ostermann, K.: Effect handlers for the masses. Proc. ACM Program. Lang. 2, 1–27 (2018)
Bruggeman, C., Waddell, O., Dybvig, R.: Representing control in the presence of one-shot continuations, vol. 31, pp. 99–107 (1970)
Danvy, O., Filinski, A.: Abstracting control. In: Proceedings of the 1990 ACM Conference on LISP and Functional Programming, pp. 151–160 (1990)
Dolan, S., White, L., Madhavapeddy, A.: Multicore OCaml. In: OCaml Users and Developers Workshop (2014)
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)
Felleisen, M., Friedman, D.P.: Control operators, the SECD-machine, and the \(\lambda \)-calculus. In: Formal Description of Programming Concepts (1987)
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
Gabriel, R.P.: The Design of Parallel Programming Languages, pp. 91–108. Academic Press Professional Inc., Cambridge (1991)
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_22
James, R., Sabry, A.: Yield: mainstream delimited continuations. In: Proceedings of Workshop on the Theory and Practice of Delimited Continuations, pp. 1–12 (2011)
Kammar, O., Lindley, S., Oury, N.: Handlers in action. In: ACM SIGPLAN International Conference on Functional Programming, pp. 145–158 (2013)
Kiselyov, O., Ishii, H.: Freer monads, more extensible effects. In: ACM SIGPLAN International Symposium on Haskell, pp. 94–105 (2015)
Kiselyov, O., Sivaramakrishnan, K.: Eff directly in OCaml. Electron. Proc. Theor. Comput. Sci. 285, 23–58 (2018)
Leijen, D.: Algebraic effects for functional programming. Technical report, p. 15 (2016)
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_17
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
Moura, A.D., Ierusalimschy, R.: Revisiting coroutines. ACM Trans. Program. Lang. Syst. 31, 1–31 (2004)
Plotkin, G., Power, J.: Algebraic operations and generic effects. Appl. Categor. Struct. 11, 69–94 (2003). https://doi.org/10.1023/A:1023064908962
Plotkin, G., Pretnar, M.: Handling algebraic effects. Log. Methods Comput. Sci. 9, 1–36 (2013)
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)
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.
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2020 Springer Nature Switzerland AG
About this paper
Cite this paper
Kawahara, S., Kameyama, Y. (2020). One-Shot Algebraic Effects as Coroutines. In: Byrski, A., Hughes, J. (eds) Trends in Functional Programming. TFP 2020. Lecture Notes in Computer Science(), vol 12222. Springer, Cham. https://doi.org/10.1007/978-3-030-57761-2_8
Download citation
DOI: https://doi.org/10.1007/978-3-030-57761-2_8
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-030-57760-5
Online ISBN: 978-3-030-57761-2
eBook Packages: Computer ScienceComputer Science (R0)