Abstract
This paper studies compilation techniques for algebraic effect handlers. In particular, we present a sequence of refinements of algebraic effects, going via multi-prompt delimited control, _generalized evidence passing_, yield bubbling, and finally a monadic translation into plain lambda calculus which can be compiled efficiently to many target platforms. Along the way we explore various interesting points in the design space. We provide two implementations of our techniques, one as a library in Haskell, and one as a C backend for the Koka programming language. We show that our techniques are effective, by comparing against three other best-in-class implementations of effect handlers: multi-core OCaml, the _Ev.Eff_ Haskell library, and the libhandler C library. We hope this work can serve as a basis for future designs and implementations of algebraic effects.
Supplemental Material
- Kenichi Asai and Yukiyoshi Kameyama. 2007. Polymorphic Delimited Continuations. 239–254. https://doi.org/10.1007/978-3-540-76637-7_16 Google Scholar
Cross Ref
- Arthur I. Baars and S. Doaitse Swierstra. 2002. Typing Dynamic Typing. In Proceedings of the Seventh ACM SIGPLAN International Conference on Functional Programming (ICFP’02). 157–166. isbn:1581134878 https://doi.org/10.1145/581478.581494 Google Scholar
Digital Library
- 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 Scholar
Cross Ref
- Jonathan Immanuel Brachthäuser, Philipp Schuster, and Klaus Ostermann. 2020. Effects as Capabilities: Effect Handlers and Lightweight Effect Polymorphism. Proc. ACM Program. Lang., 4, OOPSLA (2020), Article 126, Nov., 30 pages. https://doi.org/10.1145/3428194 Google Scholar
Digital Library
- Jonathan Immanuel Brachthäuser, Philipp Schuster, and Klaus Ostermann. 2020. Effekt: Capability-Passing Style for Type- and Effect-safe, Extensible Effect Handlers in Scala. Journal of Functional Programming.Google Scholar
Cross Ref
- Olivier Danvy and Andrzej Filinski. 1989. A Functional Abstraction of Typed Contexts. DIKU, University of Copenhagen.Google Scholar
- Stephen Dolan, Spiros Eliopoulos, Daniel Hillerström, Anil Madhavapeddy, KC Sivaramakrishnan, and Leo White. 2017. Effectively Tackling the Awkward Squad. In ML Workshop.Google Scholar
- Stephen Dolan, Leo White, KC Sivaramakrishnan, Jeremy Yallop, and Anil Madhavapeddy. 2015. Effective concurrency through algebraic effects. In OCaml Workshop.Google Scholar
- R Kent Dybvig, Simon Peyton Jones, and Amr Sabry. 2007. A monadic framework for delimited continuations. Journal of Functional Programming, 17, 6 (2007), 687–730. https://doi.org/10.1017/S0956796807006259 Google Scholar
Digital Library
- Kavon Farvardin and John Reppy. 2020. From Folklore to Fact: Comparing Implementations of Stacks and Continuations. In Proceedings of the 41st ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2020). 75–90. https://doi.org/10.1145/3385412.3385994 Google Scholar
Digital Library
- Matthias Felleisen, Daniel P. Friedman, Eugene E. Kohlbecker, and Bruce F. Duba. 1986. Reasoning with Continuations. In Proceedings of the 1st Symposium on Logic in Computer Science (LICS). 131–141.Google Scholar
- Matthew Flatt and R. Kent Dybvig. 2020. Compiler and Runtime Support for Continuation Marks. In Proceedings of the 41st ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2020). ACM, 45–58. isbn:9781450376136 https://doi.org/10.1145/3385412.3385981 Google Scholar
Digital Library
- Yannick Forster, Ohad Kammar, Sam Lindley, and Matija Pretnar. 2019. On the expressive power of user-defined effects: Effect handlers, monadic reflection, delimited control. Journal of Functional Programming, 29 (2019), 15. https://doi.org/10.1017/S0956796819000121 arxiv:1610.09161. Google Scholar
Cross Ref
- Carl A. Gunter, Didier Rémy, and Jon G. Riecke. 1995. A Generalization of Exceptions and Control in ML-like Languages. In Proceedings of the Seventh International Conference on Functional Programming Languages and Computer Architecture (FPCA ’95). ACM, 12–23. isbn:0897917197 https://doi.org/10.1145/224164.224173 Google Scholar
Digital Library
- Andreas Haas, Andreas Rossberg, Derek L. Schuff, Ben L. Titzer, Michael Holman, Dan Gohman, Luke Wagner, Alon Zakai, and JF Bastien. 2017. Bringing the Web up to Speed with WebAssembly. In Proceedings of the 38th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2017). Association for Computing Machinery, New York, NY, USA. 185–200. isbn:9781450349888 https://doi.org/10.1145/3062341.3062363 Google Scholar
Digital Library
- Daniel Hillerström and Sam Lindley. 2018. Shallow Effect Handlers. In 16th Asian Symposium on Programming Languages and Systems (APLAS’18). 415–435.Google Scholar
- Daniel Hillerströ, m, Sam Lindley, Bob Atkey, and KC Sivaramakrishnan. 2017. Continuation Passing Style for Effect Handlers. In Proceedings of the second International Conference on Formal Structures for Computation and Deduction (FSCD’17).Google Scholar
- Daniel Hillerström and Sam Lindley. 2016. Liberating Effects with Rows and Handlers. In Proceedings of the 1st International Workshop on Type-Driven Development (TyDe 2016). 15–27. https://doi.org/10.1145/2976022.2976033 Google Scholar
Digital Library
- Ohad Kammar, Sam Lindley, and Nicolas Oury. 2013. Handlers in Action. In Proceedings of the 18th ACM SIGPLAN International Conference on Functional Programming (ICFP ’13). ACM, New York, NY, USA. 145–158. isbn:978-1-4503-2326-0 https://doi.org/10.1145/2500365.2500590 Google Scholar
Digital Library
- Ohad Kammar and Matija Pretnar. 2017. No value restriction is needed for algebraic effects and handlers. Journal of Functional Programming, 27, 1 (2017), Jan., https://doi.org/10.1017/S0956796816000320 Google Scholar
Cross Ref
- Oleg Kiselyov and Hiromi Ishii. 2015. Freer Monads, More Extensible Effects. In Proceedings of the 2015 ACM SIGPLAN Symposium on Haskell (Haskell ’15). 94–105. https://doi.org/10.1145/2804302.2804319 Google Scholar
Digital Library
- Oleg Kiselyov, Chung-chieh Shan, and Amr Sabry. 2006. Delimited Dynamic Binding. In Proceedings of the Eleventh ACM SIGPLAN International Conference on Functional Programming (ICFP ’06). Association for Computing Machinery, New York, NY, USA. 26–37. isbn:1595933093 https://doi.org/10.1145/1159803.1159808 Google Scholar
Digital Library
- Oleg Kiselyov and KC Sivaramakrishnan. 2017. Eff directly in OCaml. In ML workshop 2016. http://kcsrk.info/papers/caml-eff17.pdf Extended version.Google Scholar
- Daan Leijen. 2005. Extensible records with scoped labels. In Proceedings of the 2005 Symposium on Trends in Functional Programming. 297–312.Google Scholar
- Daan Leijen. 2017. Implementing Algebraic Effects in C: or Monads for free in C. Programming Languages and Systems, 10695, 1 (2017), 339–363. APLAS’17.Google Scholar
- Daan Leijen. 2017. Type Directed Compilation of Row-typed Algebraic Effects. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages (POPL’17). 486–499. isbn:978-1-4503-4660-3 https://doi.org/10.1145/3009837.3009872 Google Scholar
Digital Library
- Daan Leijen. 2020. The Koka Programming Language. Nov., https://koka-lang.github.ioGoogle Scholar
- Daan Leijen. 2021. Effect Handler Benchmarks. May, https://github.com/daanx/effect-benchGoogle Scholar
- Paul Blain Levy. 2006. Call-by-push-value: Decomposing call-by-value and call-by-name. Higher-Order and Symbolic Computation, 19, 4 (2006), 377–414.Google Scholar
Digital Library
- Michel Parigot. 1992. λμ-Calculus: An algorithmic interpretation of classical natural deduction. In Logic Programming and Automated Reasoning, Andrei Voronkov (Ed.). Springer Berlin Heidelberg, 190–201. isbn:978-3-540-47279-7Google Scholar
- 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 Scholar
Cross Ref
- Gordon D. Plotkin and Matija Pretnar. 2009. Handlers of Algebraic Effects. In 18th European Symposium on Programming Languages and Systems (ESOP’09). 80–94. https://doi.org/10.1007/978-3-642-00590-9_7 Google Scholar
Digital Library
- Gordon D. Plotkin and Matija Pretnar. 2013. Handling Algebraic Effects. Logical Methods in Computer Science, 9, 4, https://doi.org/10.2168/LMCS-9(4:23)2013 Google Scholar
Cross Ref
- Matija Pretnar. 2015. An Introduction to Algebraic Effects and Handlers. Invited Tutorial Paper. Electron. Notes Theor. Comput. Sci., 319, C (2015), Dec., 19–35. issn:1571-0661 https://doi.org/10.1016/j.entcs.2015.12.003 Google Scholar
Digital Library
- Alex Reinking, Ningning Xie, Leonardo de Moura, and Daan Leijen. 2021. Perceus: Garbage Free Reference Counting with Reuse. In 42rd ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’21).Google Scholar
- Tom Schrijvers, Maciej Piróg, Nicolas Wu, and Mauro Jaskelioff. 2019. Monad Transformers and Modular Algebraic Effects: What Binds Them Together. In Proceedings of the 12th ACM SIGPLAN International Symposium on Haskell (Haskell 2019). Association for Computing Machinery, New York, NY, USA. 98–113. isbn:9781450368131 https://doi.org/10.1145/3331545.3342595 Google Scholar
Digital Library
- Philipp Schuster, Jonathan Immanuel Brachthäuser, and Klaus Ostermann. 2020. Compiling Effect Handlers in Capability-Passing Style. Proc. ACM Program. Lang., 4, ICFP (2020), Article 93, Aug., 28 pages. https://doi.org/10.1145/3408975 Google Scholar
Digital Library
- KC Sivaramakrishnan, Stephen Dolan, Leo White, Tom Kelly, Sadiq Jaffer, and Anil Madhavapeddy. 2021. Retrofitting Effect Handlers onto OCaml. In 42rd ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’21).Google Scholar
- Martin Sulzmann, Manuel Chakravarty, Simon Peyton Jones, and Kevin Donnelly. 2007. System F with type equality coercions. In ACM SIGPLAN International Workshop on Types in Language Design and Implementation (TLDI’07). ACM press, 53–66.Google Scholar
Digital Library
- Herb Sutter. 2019. Zero-overhead deterministic exceptions: Throwing values. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0709r4.pdf ISO/IEC WG2.1, P0709R4.Google Scholar
- Nicolas Wu and Tom Schrijvers. 2015. Fusion for Free: Efficient Algebraic Effect Handlers. In Proceedings of the 12th International Conference on Mathematics of Program Construction (MPC 2015, Vol. 9129). 302.Google Scholar
- Nicolas Wu, Tom Schrijvers, and Ralf Hinze. 2014. Effect Handlers in Scope. In Proceedings of the 2014 ACM SIGPLAN Symposium on Haskell (Haskell ’14). 1–12. isbn:978-1-4503-3041-1 https://doi.org/10.1145/2633357.2633358 Google Scholar
Digital Library
- Ningning Xie, Jonathan Brachthäuser, Phillip Schuster, Daniel Hillerström, and Daan Leijen. 2020. Effect Handlers, Evidently. In 25th ACM SIGPLAN International Conference on Functional Programming (ICFP’2020). https://doi.org/10.1145/3408981 Google Scholar
Digital Library
- Ningning Xie and Daan Leijen. 2020. Effect Handlers in Haskell, Evidently. In Proceedings of the 2020 ACM SIGPLAN Symposium on Haskell (Haskell’20). https://doi.org/10.1145/3406088.3409022 Google Scholar
Digital Library
- Ningning Xie and Daan Leijen. 2021. Generalized Evidence Passing for Effect Handlers. Microsoft Research. Extended version with proofs.Google Scholar
- Ningning Xie and Daan Leijen. 2021. Mp.Eff: Efficient effect handlers based on Evidence Passing Semantics. March, https://github.com/xnning/MpEffGoogle Scholar
- Yizhou Zhang and Andrew C. Myers. 2019. Abstraction-Safe Effect Handlers via Tunneling. Proc. ACM Program. Lang., 3, POPL (2019), Jan., 29 pages. https://doi.org/10.1145/3290318 Google Scholar
Digital Library
Index Terms
Generalized evidence passing for effect handlers: efficient compilation of effect handlers to C
Recommendations
Efficient compilation of algebraic effect handlers
The popularity of algebraic effect handlers as a programming language feature for user-defined computational effects is steadily growing. Yet, even though efficient runtime representations have already been studied, most handler-based programs are still ...
Effect handlers, evidently
Algebraic effect handlers are a powerful way to incorporate effects in a programming language. Sometimes perhaps even _too_ powerful. In this article we define a restriction of general effect handlers with _scoped resumptions_. We argue one can still ...
Liberating effects with rows and handlers
TyDe 2016: Proceedings of the 1st International Workshop on Type-Driven DevelopmentAlgebraic effects and effect handlers provide a modular abstraction for effectful programming. They support user-defined effects, as in Haskell, in conjunction with direct-style effectful programming, as in ML. They also present a structured interface ...






Comments