Skip to main content

One-Shot Algebraic Effects as Coroutines

  • Conference paper
  • First Online:
Trends in Functional Programming (TFP 2020)

Part of the book series: Lecture Notes in Computer Science ((LNTCS,volume 12222))

Included in the following conference series:

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    The Wikipedia article on coroutine (access date: June 1, 2020) lists fifty programming languages which have native support for coroutines.

  2. 2.

    https://github.com/nymphium/eff.lua.

  3. 3.

    https://github.com/nymphium/ruff.

  4. 4.

    https://github.com/MakeNowJust/eff.js.

  5. 5.

    https://github.com/pandaman64/effective-rust.

  6. 6.

    https://www.lua.org/manual/5.3/manual.html#3.4.9.

  7. 7.

    https://golang.org/ref/spec#Defer_statements.

  8. 8.

    Strictly speaking, our calculus is the one for stackful asymmetric coroutines according to de Moura and Ierusalimschy’s classification.

  9. 9.

    See Sect. 5.3 in [20].

  10. 10.

    Felleisen considers the case where each argument may be bound by the construct.

  11. 11.

    https://github.com/nymphium/effs-benchmark.

  12. 12.

    https://reactjs.org/docs/hooks-reference.html.

  13. 13.

    https://overreacted.io/algebraic-effects-for-the-rest-of-us/.

References

  1. Bauer, A., Pretnar, M.: Programming with Algebraic Effects and Handlers. J. Log. Algebr. Methods Program. 84, 108–123 (2012)

    Article  MathSciNet  MATH  Google 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:1020891112409

    Article  MATH  Google 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)

    Article  Google 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)

    MATH  Google 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_22

    Chapter  Google 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)

    Article  Google 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_17

    Chapter  Google 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)

    Article  Google Scholar 

  21. Plotkin, G., Power, J.: Algebraic operations and generic effects. Appl. Categor. Struct. 11, 69–94 (2003). https://doi.org/10.1023/A:1023064908962

    Article  MathSciNet  MATH  Google Scholar 

  22. Plotkin, G., Pretnar, M.: Handling algebraic effects. Log. Methods Comput. Sci. 9, 1–36 (2013)

    Article  MathSciNet  MATH  Google 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 

Download references

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

Authors

Corresponding author

Correspondence to Yukiyoshi Kameyama .

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2020 Springer Nature Switzerland AG

About this paper

Check for updates. Verify currency and authenticity via CrossMark

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)

Publish with us

Policies and ethics