skip to main content

High-level effect handlers in C++

Published:31 October 2022Publication History
Skip Abstract Section

Abstract

Effect handlers allow the programmer to implement computational effects, such as custom error handling, various forms of lightweight concurrency, and dynamic binding, inside the programming language. We introduce cpp-effects, a C++ library for effect handlers with a typed high-level, object-oriented interface. We demonstrate that effect handlers can be successfully applied in imperative systems programming languages with manual memory management. Through a collection of examples, we explore how to program effectively with effect handlers in C++, discuss the intricacies and challenges of the implementation, and show that despite its limitations, cpp-effects performance is competitive and in some cases even outperforms state-of-the-art approaches such as C++20 coroutines and the libmprompt library for multiprompt delimited control.

References

  1. Joe Armstrong, Robert Virding, Claes Wikström, and Mike Williams. 1996. Concurrent Programming in Erlang, Second Edition. Prentice Hall International, Hertfordshire, UK. Google ScholarGoogle Scholar
  2. Andrej Bauer and Matija Pretnar. 2015. Programming with algebraic effects and handlers. J. Log. Algebr. Meth. Program., 84, 1 (2015), 108–123. https://doi.org/10.1016/j.jlamp.2014.02.001 Google ScholarGoogle ScholarCross RefCross Ref
  3. Dariusz Biernacki, Maciej Piróg, Piotr Polesiuk, and Filip Sieczkowski. 2019. Abstracting algebraic effects. Proc. ACM Program. Lang., 3, POPL (2019), 6:1–6:28. https://doi.org/10.1145/3290319 Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Dariusz Biernacki, Maciej Piróg, Piotr Polesiuk, and Filip Sieczkowski. 2020. Binders by day, labels by night: effect instances via lexically scoped handlers. Proc. ACM Program. Lang., 4, POPL (2020), 48:1–48:29. https://doi.org/10.1145/3371116 Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Eli Bingham, Jonathan P. Chen, Martin Jankowiak, Fritz Obermeyer, Neeraj Pradhan, Theofanis Karaletsos, Rohit Singh, Paul A. Szerlip, Paul Horsfall, and Noah D. Goodman. 2019. Pyro: Deep Universal Probabilistic Programming. J. Mach. Learn. Res., 20 (2019), 28:1–6. Google ScholarGoogle Scholar
  6. Boost. 2022. Boost.context library. https://github.com/boostorg/context Google ScholarGoogle Scholar
  7. Jonathan Immanuel Brachthäuser, Philipp Schuster, and Klaus Ostermann. 2018. Effect handlers for the masses. Proc. ACM Program. Lang., 2, OOPSLA (2018), 111:1–111:27. https://doi.org/10.1145/3276481 Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Jonathan Immanuel Brachthäuser, Philipp Schuster, and Klaus Ostermann. 2020. Effekt: Capability-passing style for type- and effect-safe, extensible effect handlers in Scala. J. Funct. Program., 30 (2020), e8. https://doi.org/10.1017/S0956796820000027 Google ScholarGoogle ScholarCross RefCross Ref
  9. Guillaume Combette and Guillaume Munch-Maccagnoni. 2018. A resource modality for RAII. In LOLA 2018: Workshop on Syntax and Semantics of Low-Level Languages. 1–4. Google ScholarGoogle Scholar
  10. Lukas Convent, Sam Lindley, Conor McBride, and Craig McLaughlin. 2020. Doo bee doo bee doo. J. Funct. Program., 30 (2020), e9. https://doi.org/10.1017/S0956796820000039 Google ScholarGoogle ScholarCross RefCross Ref
  11. Dan Ghica, Sam Lindley, Marcos Maroñas Bravo, and Maciej Piróg. 2022. High-Level Effect Handlers in C++ (Artifact). https://doi.org/10.1145/3554330 Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. GitHub. 2022. Semantic library. https://github.com/github/semantic Google ScholarGoogle Scholar
  13. Daniel Hillerström, Daan Leijen, Sam Lindley, Matija Pretnar, Andreas Rossberg, and KC Sivamarakrishnan. 2022. WebAssembly Typed Continuations Proposal. https://github.com/effect-handlers/wasm-spec/proposals/continuations/Explainer.md Google ScholarGoogle Scholar
  14. Daniel Hillerström and Sam Lindley. 2016. Liberating effects with rows and handlers. In TyDe. https://doi.org/10.1145/2976022.2976033 Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Pablo Inostroza and Tijs van der Storm. 2018. JEff: objects for effect. In Onward!. ACM, 111–124. https://doi.org/10.1145/3276954.3276955 Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Ohad Kammar, Sam Lindley, and Nicolas Oury. 2013. Handlers in action. In ICFP. ACM, 145–158. https://doi.org/10.1145/2544174.2500590 Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Casey Klein, John Clements, Christos Dimoulas, Carl Eastlund, Matthias Felleisen, Matthew Flatt, Jay A. McCarthy, Jon Rafkind, Sam Tobin-Hochstadt, and Robert Bruce Findler. 2012. Run your research: on the effectiveness of lightweight mechanization. In POPL. ACM, 285–296. https://doi.org/10.1145/2103621.2103691 Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. Daan Leijen. 2017. Implementing Algebraic Effects in C — "Monads for Free in C". In APLAS (Lecture Notes in Computer Science, Vol. 10695). Springer, 339–363. https://doi.org/10.1007/978-3-319-71237-6_17 Google ScholarGoogle ScholarCross RefCross Ref
  19. Daan Leijen. 2017. Type directed compilation of row-typed algebraic effects. In POPL. ACM, 486–499. https://doi.org/10.1145/3093333.3009872 Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. Daan Leijen. 2019. libhandler. https://github.com/koka-lang/libhandler Google ScholarGoogle Scholar
  21. Daan Leijen and KC Sivamarakrishnan. 2022. libmprompt. https://github.com/koka-lang/libmprompt Google ScholarGoogle Scholar
  22. Meta. 2022. React library. https://reactjs.org/ Google ScholarGoogle Scholar
  23. Gordon D. Plotkin and John Power. 2001. Semantics for Algebraic Operations. Electr. Notes Theor. Comput. Sci., 45 (2001), 332–345. https://doi.org/10.1016/S1571-0661(04)80970-8 Google ScholarGoogle ScholarCross RefCross Ref
  24. Gordon D. Plotkin and John Power. 2002. Notions of Computation Determine Monads. In FoSSaCS (Lecture Notes in Computer Science, Vol. 2303). Springer, 342–356. https://doi.org/10.1007/3-540-45931-6_24 Google ScholarGoogle ScholarCross RefCross Ref
  25. Gordon D. Plotkin and John Power. 2003. Algebraic Operations and Generic Effects. Applied Categorical Structures, 11, 1 (2003), 69–94. https://doi.org/10.1023/A:1023064908962 Google ScholarGoogle ScholarCross RefCross Ref
  26. Gordon D. Plotkin and Matija Pretnar. 2009. Handlers of Algebraic Effects. In ESOP (Lecture Notes in Computer Science, Vol. 5502). Springer, 80–94. https://doi.org/10.1007/978-3-642-00590-9_7 Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Gordon D. Plotkin and Matija Pretnar. 2013. Handling Algebraic Effects. Logical Methods in Computer Science, 9, 4 (2013), https://doi.org/10.2168/LMCS-9(4:23)2013 Google ScholarGoogle ScholarCross RefCross Ref
  28. Andreas Rossberg, Ben L. Titzer, Andreas Haas, Derek L. Schuff, Dan Gohman, Luke Wagner, Alon Zakai, J. F. Bastien, and Michael Holman. 2018. Bringing the web up to speed with WebAssembly. Commun. ACM, 61, 12 (2018), 107–115. https://doi.org/10.1145/3062341.3062363 Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. KC Sivaramakrishnan, Stephen Dolan, Leo White, Tom Kelly, Sadiq Jaffer, and Anil Madhavapeddy. 2021. Retrofitting effect handlers onto OCaml. In PLDI. ACM, 206–221. https://doi.org/10.1145/3453483.3454039 Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. WebAssembly Community Group. 2022. WebAssembly Stack Switching Extension. https://github.com/WebAssembly/stack-switching Google ScholarGoogle Scholar
  31. Ningning Xie, Jonathan Immanuel Brachthäuser, Daniel Hillerström, Philipp Schuster, and Daan Leijen. 2020. Effect handlers, evidently. Proc. ACM Program. Lang., 4, ICFP (2020), 99:1–99:29. https://doi.org/10.1145/3408981 Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. Ningning Xie and Daan Leijen. 2021. Generalized evidence passing for effect handlers: efficient compilation of effect handlers to C. Proc. ACM Program. Lang., 5, ICFP (2021), 1–30. https://doi.org/10.1145/3473576 Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. Yizhou Zhang and Andrew C. Myers. 2019. Abstraction-safe effect handlers via tunneling. Proc. ACM Program. Lang., 3, POPL (2019), 5:1–5:29. https://doi.org/10.1145/3290318 Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. High-level effect handlers in C++

        Recommendations

        Comments

        Login options

        Check if you have access through your login credentials or your institution to get full access on this article.

        Sign in

        Full Access

        • Article Metrics

          • Downloads (Last 12 months)441
          • Downloads (Last 6 weeks)51

          Other Metrics

        PDF Format

        View or Download as a PDF file.

        PDF

        eReader

        View online with eReader.

        eReader
        About Cookies On This Site

        We use cookies to ensure that we give you the best experience on our website.

        Learn more

        Got it!