Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
skip to main content
research-article
Open access

High-level effect handlers in C++

Published: 31 October 2022 Publication History

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.
[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
[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
[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
[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.
[6]
Boost. 2022. Boost.context library. https://github.com/boostorg/context
[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
[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
[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.
[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
[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
[12]
GitHub. 2022. Semantic library. https://github.com/github/semantic
[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
[14]
Daniel Hillerström and Sam Lindley. 2016. Liberating effects with rows and handlers. In TyDe. https://doi.org/10.1145/2976022.2976033
[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
[16]
Ohad Kammar, Sam Lindley, and Nicolas Oury. 2013. Handlers in action. In ICFP. ACM, 145–158. https://doi.org/10.1145/2544174.2500590
[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
[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
[19]
Daan Leijen. 2017. Type directed compilation of row-typed algebraic effects. In POPL. ACM, 486–499. https://doi.org/10.1145/3093333.3009872
[20]
Daan Leijen. 2019. libhandler. https://github.com/koka-lang/libhandler
[21]
Daan Leijen and KC Sivamarakrishnan. 2022. libmprompt. https://github.com/koka-lang/libmprompt
[22]
Meta. 2022. React library. https://reactjs.org/
[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
[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
[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
[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
[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
[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
[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
[30]
WebAssembly Community Group. 2022. WebAssembly Stack Switching Extension. https://github.com/WebAssembly/stack-switching
[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
[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
[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

Cited By

View all
  • (2024)Parallel Algebraic Effect HandlersProceedings of the ACM on Programming Languages10.1145/36746518:ICFP(756-788)Online publication date: 15-Aug-2024
  • (2024)Algebraic Effects Meet Hoare Logic in Cubical AgdaProceedings of the ACM on Programming Languages10.1145/36328988:POPL(1663-1695)Online publication date: 5-Jan-2024
  • (2024)Soundly Handling LinearityProceedings of the ACM on Programming Languages10.1145/36328968:POPL(1600-1628)Online publication date: 5-Jan-2024
  • Show More Cited By

Recommendations

Comments

Information & Contributors

Information

Published In

cover image Proceedings of the ACM on Programming Languages
Proceedings of the ACM on Programming Languages  Volume 6, Issue OOPSLA2
October 2022
1932 pages
EISSN:2475-1421
DOI:10.1145/3554307
Issue’s Table of Contents
This work is licensed under a Creative Commons Attribution 4.0 International License.

Publisher

Association for Computing Machinery

New York, NY, United States

Publication History

Published: 31 October 2022
Published in PACMPL Volume 6, Issue OOPSLA2

Permissions

Request permissions for this article.

Check for updates

Badges

Author Tags

  1. Effect handlers
  2. algebraic effects
  3. context switching
  4. lightweight concurrency

Qualifiers

  • Research-article

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)516
  • Downloads (Last 6 weeks)47
Reflects downloads up to 22 Sep 2024

Other Metrics

Citations

Cited By

View all
  • (2024)Parallel Algebraic Effect HandlersProceedings of the ACM on Programming Languages10.1145/36746518:ICFP(756-788)Online publication date: 15-Aug-2024
  • (2024)Algebraic Effects Meet Hoare Logic in Cubical AgdaProceedings of the ACM on Programming Languages10.1145/36328988:POPL(1663-1695)Online publication date: 5-Jan-2024
  • (2024)Soundly Handling LinearityProceedings of the ACM on Programming Languages10.1145/36328968:POPL(1600-1628)Online publication date: 5-Jan-2024
  • (2023)Continuing WebAssembly with Effect HandlersProceedings of the ACM on Programming Languages10.1145/36228147:OOPSLA2(460-485)Online publication date: 16-Oct-2023

View Options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Get Access

Login options

Full Access

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media