skip to main content
research-article
Open Access

Compiling effect handlers in capability-passing style

Published:03 August 2020Publication History
Skip Abstract Section

Abstract

Effect handlers encourage programmers to abstract over repeated patterns of complex control flow. As of today, this abstraction comes at a significant price in performance. In this paper, we aim to achieve abstraction without regret for effect handlers.

We present a language for effect handlers in _capability-passing style_ (λCap) and an implementation of this language as a translation to simply-typed lambda calculus in _iterated continuation-passing style_. A suite of benchmarks indicates that the novel combination of capability-passing style and iterated CPS enables significant speedups over existing languages with effect handlers or control operators. Our implementation technique is general and allows us to generate code in any language that supports first-class functions.

We then identify a subset of programs for which we can further improve the performance and guarantee full elimination of the effect handler abstraction. To formally capture this subset, we refine λCap to λ λCap with a more restrictive type system. We present a type-directed translation for λ λCap that inserts staging annotations and prove that no abstractions or applications related to effect handlers occur in the translated program. Using this second translation we observe additional speedups in some of the benchmarks.

Skip Supplemental Material Section

Supplemental Material

Presentation at ICFP '20

References

  1. Andrei Alexandrescu. 2010. The D Programming Language (1st ed.). Addison-Wesley Professional.Google ScholarGoogle Scholar
  2. Brian Anderson, Lars Bergstrom, Manish Goregaokar, Josh Matthews, Keegan McAllister, Jack Mofitt, and Simon Sapin. 2016. Engineering the Servo Web Browser Engine Using Rust. In Proceedings of the 38th International Conference on Software Engineering Companion (Austin, Texas) ( ICSE '16). Association for Computing Machinery, New York, NY, USA, 81-89. https://doi.org/10.1145/2889160.2889229 Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Henk P. Barendregt. 1992. Lambda Calculi with Types. In Handbook of Logic in Computer Science (vol. 2 ): Background: Computational Structures. Oxford University Press, New York, NY, USA, 117-309.Google ScholarGoogle Scholar
  4. Dariusz Biernacki, Maciej Piróg, Piotr Polesiuk, and Filip Sieczkowski. 2019. Abstracting Algebraic Efects. Proc. ACM Program. Lang. 3, POPL, Article 6 ( Jan. 2019 ), 28 pages.Google ScholarGoogle Scholar
  5. Dariusz Biernacki, Maciej Piróg, Piotr Polesiuk, and Filip Sieczkowski. 2020. Binders by Day, Labels by Night: Efect Instances via Lexically Scoped Handlers. In Proceedings of the Symposium on Principles of Programming Languages (to appear). ACM, New York, NY, USA.Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Jonathan Immanuel Brachthäuser and Philipp Schuster. 2017. Efekt: Extensible Algebraic Efects in Scala (Short Paper). In Proceedings of the International Symposium on Scala (Vancouver, BC, Canada). ACM, New York, NY, USA. https: //doi.org/10.1145/3136000.3136007 Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Jonathan Immanuel Brachthäuser, Philipp Schuster, and Klaus Ostermann. 2018. Efect Handlers for the Masses. Proc. ACM Program. Lang. 2, OOPSLA, Article 111 (Oct. 2018 ), 27 pages. https://doi.org/10.1145/3276481 Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Jonathan Immanuel Brachthäuser, Philipp Schuster, and Klaus Ostermann. 2020. Efekt: Capability-Passing Style for Typeand Efect-safe, Extensible Efect Handlers in Scala. Journal of Functional Programming ( 2020 ). https://doi.org/10.1017/ S0956796820000027 Google ScholarGoogle ScholarCross RefCross Ref
  9. Edwin Brady. 2013. Idris, a general-purpose dependently typed programming language: Design and implementation. Journal of Functional Programming 23, 5 ( 2013 ), 552-593.Google ScholarGoogle ScholarCross RefCross Ref
  10. Oliver Bračevac, Nada Amin, Guido Salvaneschi, Sebastian Erdweg, Patrick Eugster, and Mira Mezini. 2018. Versatile Event Correlation with Algebraic Efects. Proc. ACM Program. Lang. 2, ICFP, Article 67 ( July 2018 ), 31 pages.Google ScholarGoogle Scholar
  11. Youyou Cong, Leo Osvald, Grégory M. Essertel, and Tiark Rompf. 2019. Compiling with Continuations, or Without? Whatever. Proc. ACM Program. Lang. 3, ICFP, Article 79 ( July 2019 ), 28 pages. https://doi.org/10.1145/3341643 Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Lukas Convent, Sam Lindley, Conor McBride, and Craig McLaughlin. 2020. Doo Bee Doo Bee Doo. Journal of Functional Programming 30 ( 2020 ), e9. https://doi.org/10.1017/S0956796820000039 Google ScholarGoogle ScholarCross RefCross Ref
  13. Olivier Danvy and Andrzej Filinski. 1989. A functional abstraction of typed contexts. DIKU Rapport 89 /12, DIKU, University of Copenhagen ( 1989 ).Google ScholarGoogle Scholar
  14. Olivier Danvy and Andrzej Filinski. 1990. Abstracting Control. In Proceedings of the Conference on LISP and Functional Programming (Nice, France). ACM, New York, NY, USA, 151-160.Google ScholarGoogle Scholar
  15. Oliver Danvy and Andrzej Filinski. 1992. Representing control: A study of the CPS transformation. Mathematical Structures in Computer Science 2, 4 ( 1992 ), 361-391.Google ScholarGoogle Scholar
  16. Olivier Danvy, Karoline Malmkjaer, and Jens Palsberg. 1996. Eta-expansion Does The Trick. ACM Trans. Program. Lang. Syst. 18, 6 (Nov. 1996 ), 730-751.Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Stephen Dolan, Spiros Eliopoulos, Daniel Hillerström, Anil Madhavapeddy, KC Sivaramakrishnan, and Leo White. 2017. Concurrent system programming with efect handlers. In Proceedings of the Symposium on Trends in Functional Programming. Springer LNCS 10788.Google ScholarGoogle Scholar
  18. Stephen Dolan, Leo White, and Anil Madhavapeddy. 2014. Multicore OCaml. In OCaml Workshop.Google ScholarGoogle Scholar
  19. Stephen Dolan, Leo White, KC Sivaramakrishnan, Jeremy Yallop, and Anil Madhavapeddy. 2015. Efective concurrency through algebraic efects. In OCaml Workshop.Google ScholarGoogle Scholar
  20. R. Kent Dybvig. 2006. The Development of Chez Scheme. In Proceedings of the Eleventh ACM SIGPLAN International Conference on Functional Programming (Portland, Oregon, USA) ( ICFP '06). ACM, New York, NY, USA, 1-12. https: //doi.org/10.1145/1159803.1159805 Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. R. Kent Dybvig, Simon L. Peyton Jones, and Amr Sabry. 2007. A monadic framework for delimited continuations. Journal of Functional Programming 17, 6 ( 2007 ), 687-730.Google ScholarGoogle Scholar
  22. Yannick Forster, Ohad Kammar, Sam Lindley, and Matija Pretnar. 2017. On the Expressive Power of User-defined Efects: Efect Handlers, Monadic Reflection, Delimited Control. Proc. ACM Program. Lang. 1, ICFP, Article 13 ( Aug. 2017 ), 29 pages.Google ScholarGoogle Scholar
  23. Daniel Hillerström and Sam Lindley. 2016. Liberating Efects with Rows and Handlers. In Proceedings of the Workshop on Type-Driven Development (Nara, Japan). ACM, New York, NY, USA.Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Daniel Hillerström, Sam Lindley, Bob Atkey, and KC Sivaramakrishnan. 2017. Continuation Passing Style for Efect Handlers. In Formal Structures for Computation and Deduction (LIPIcs), Vol. 84. Schloss Dagstuhl-Leibniz-Zentrum für Informatik.Google ScholarGoogle Scholar
  25. Neil D. Jones, Carsten K. Gomard, and Peter Sestoft. 1993. Partial Evaluation and Automatic Program Generation. Prentice-Hall, Inc., Upper Saddle River, New Jersey, USA.Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Ohad Kammar, Sam Lindley, and Nicolas Oury. 2013. Handlers in Action. In Proceedings of the International Conference on Functional Programming (Boston, Massachusetts, USA). ACM, New York, NY, USA, 145-158.Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Ohad Kammar and Matija Pretnar. 2017. No value restriction is needed for algebraic efects and handlers. Journal of Functional Programming 27, 1 (Jan. 2017 ).Google ScholarGoogle ScholarCross RefCross Ref
  28. Andrew Kennedy. 2007. Compiling with Continuations, Continued. In Proceedings of the International Conference on Functional Programming (Freiburg, Germany). ACM, New York, NY, USA, 177-190.Google ScholarGoogle Scholar
  29. Oleg Kiselyov and Hiromi Ishii. 2015. Freer Monads, More Extensible Efects. In Proceedings of the Haskell Symposium (Vancouver, BC, Canada). ACM, New York, NY, USA, 94-105.Google ScholarGoogle Scholar
  30. Oleg Kiselyov and KC Sivaramakrishnan. 2018. Ef Directly in OCaml. In Proceedings of the ML Family Workshop / OCaml Users and Developers workshops (Electronic Proceedings in Theoretical Computer Science), Kenichi Asai and Mark Shinwell (Eds.), Vol. 285. Open Publishing Association, 23-58. https://doi.org/10.4204/EPTCS.285.2 Google ScholarGoogle ScholarCross RefCross Ref
  31. Daan Leijen. 2017a. Implementing Algebraic Efects in C. In Proceedings of the Asian Symposium on Programming Languages and Systems. Springer International Publishing, Cham, Switzerland, 339-363.Google ScholarGoogle ScholarCross RefCross Ref
  32. Daan Leijen. 2017b. Structured Asynchrony with Algebraic Efects. In Proceedings of the Workshop on Type-Driven Development (Oxford, UK). ACM, New York, NY, USA, 16-29.Google ScholarGoogle Scholar
  33. Daan Leijen. 2017c. Type directed compilation of row-typed algebraic efects. In Proceedings of the Symposium on Principles of Programming Languages. ACM, New York, NY, USA, 486-499.Google ScholarGoogle Scholar
  34. Daan Leijen. 2018. First Class Dynamic Efect Handlers: Or, Polymorphic Heaps with Dynamic Efect Handlers. In Proceedings of the Workshop on Type-Driven Development (St. Louis, Missouri, USA). ACM, New York, NY, USA, 51-64.Google ScholarGoogle Scholar
  35. Xavier Leroy, Damien Doligez, Alain Frisch, Jacques Garrigue, Didier Rémy, and Jérôme Vouillon. 2017. The OCaml system release 4.06. Institut National de Recherche en Informatique et en Automatique ( 2017 ).Google ScholarGoogle Scholar
  36. Paul Blain Levy, John Power, and Hayo Thielecke. 2003. Modelling environments in call-by-value programming languages. Information and Computation 185, 2 ( 2003 ), 182-210.Google ScholarGoogle Scholar
  37. Sheng Liang, Paul Hudak, and Mark Jones. 1995. Monad Transformers and Modular Interpreters. In Proceedings of the Symposium on Principles of Programming Languages (San Francisco, California, USA). ACM, New York, NY, USA, 333-343.Google ScholarGoogle ScholarDigital LibraryDigital Library
  38. Sam Lindley, Conor McBride, and Craig McLaughlin. 2017. Do Be Do Be Do. In Proceedings of the Symposium on Principles of Programming Languages (Paris, France). ACM, New York, NY, USA, 500-514.Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. Marek Materzok and Dariusz Biernacki. 2012. A dynamic interpretation of the CPS hierarchy. In Proceedings of the Asian Symposium on Programming Languages and Systems. Springer, 296-311.Google ScholarGoogle ScholarCross RefCross Ref
  40. Luke Maurer, Paul Downen, Zena M. Ariola, and Simon L. Peyton Jones. 2017. Compiling Without Continuations. In Proceedings of the 38th ACM SIGPLAN Conference on Programming Language Design and Implementation (Barcelona, Spain) ( PLDI 2017). ACM, New York, NY, USA, 482-494. https://doi.org/10.1145/3062341.3062380 Google ScholarGoogle ScholarDigital LibraryDigital Library
  41. Flemming Nielson and Hanne Riis Nielson. 1996. Multi-level lambda-calculi: an algebraic description. In Partial evaluation. Springer, 338-354.Google ScholarGoogle Scholar
  42. Leo Osvald, Grégory Essertel, Xilun Wu, Lilliam I González Alayón, and Tiark Rompf. 2016. Gentrification gone too far? afordable 2nd-class values for fun and (co-) efect. In Proceedings of the Conference on Object-Oriented Programming, Systems, Languages and Applications. ACM, New York, NY, USA, 234-251.Google ScholarGoogle Scholar
  43. Frank Pfenning and Conal Elliot. 1988. Higher-Order Abstract Syntax. In Proceedings of the Conference on Programming Language Design and Implementation (Atlanta, Georgia, USA). ACM, New York, NY, USA, 199-208. https://doi.org/10. 1145/53990.54010 Google ScholarGoogle ScholarDigital LibraryDigital Library
  44. Maciej Piróg, Tom Schrijvers, Nicolas Wu, and Mauro Jaskeliof. 2018. Syntax and Semantics for Operations with Scopes. In Proceedings of the 33rd Annual ACM/IEEE Symposium on Logic in Computer Science (Oxford, United Kingdom) (LICS '18). ACM, New York, NY, USA, 809-818. https://doi.org/10.1145/3209108.3209166 Google ScholarGoogle ScholarDigital LibraryDigital Library
  45. Gordon Plotkin and Matija Pretnar. 2009. Handlers of algebraic efects. In European Symposium on Programming. SpringerVerlag, 80-94.Google ScholarGoogle Scholar
  46. Gordon D. Plotkin and Matija Pretnar. 2013. Handling Algebraic Efects. Logical Methods in Computer Science 9, 4 ( 2013 ).Google ScholarGoogle Scholar
  47. Matija Pretnar. 2015. An introduction to algebraic efects and handlers. invited tutorial paper. Electronic Notes in Theoretical Computer Science 319 ( 2015 ), 19-35.Google ScholarGoogle Scholar
  48. Matija Pretnar, Amr Hany Shehata Saleh, Axel Faes, and Tom Schrijvers. 2017. Eficient compilation of algebraic efects and handlers. Technical Report. Department of Computer Science, KU Leuven; Leuven, Belgium.Google ScholarGoogle Scholar
  49. John C. Reynolds. 1972. Definitional Interpreters for Higher-Order Programming Languages. In Proceedings of the ACM annual conference (Boston, Massachusetts, USA). ACM, New York, NY, USA, 717-740.Google ScholarGoogle ScholarDigital LibraryDigital Library
  50. Amr Hany Saleh, Georgios Karachalias, Matija Pretnar, and Tom Schrijvers. 2018. Explicit Efect Subtyping. In Programming Languages and Systems, Amal Ahmed (Ed.). Springer International Publishing, Cham, Switzerland, 327-354.Google ScholarGoogle Scholar
  51. Philipp Schuster and Jonathan Immanuel Brachthäuser. 2018. Typing, Representing, and Abstracting Control. In Proceedings of the Workshop on Type-Driven Development (St. Louis, Missouri, USA). ACM, New York, NY, USA, 14-24. https: //doi.org/10.1145/3240719.3241788 Google ScholarGoogle ScholarDigital LibraryDigital Library
  52. Bjarne Stroustrup. 1997. The C+ + Programming Language, Third Edition (3rd ed.). Addison-Wesley Longman Publishing Co., Inc., USA.Google ScholarGoogle Scholar
  53. Walid Taha and Tim Sheard. 1997. Multi-stage Programming with Explicit Annotations. In Proceedings of the Workshop on Partial Evaluation and Program Manipulation (Amsterdam, The Netherlands). ACM, New York, NY, USA, 203-217.Google ScholarGoogle ScholarDigital LibraryDigital Library
  54. Walid Taha and Tim Sheard. 2000. MetaML and Multi-stage Programming with Explicit Annotations. Theoretical Computer Science 248, 1-2 (Oct. 2000 ), 211-242. http://dx.doi.org/10.1016/S0304-3975 ( 00 ) 00053-0 Google ScholarGoogle ScholarCross RefCross Ref
  55. Andrew K. Wright and Matthias Felleisen. 1994. A syntactic approach to type soundness. Inf. Comput. 115, 1 (Nov. 1994 ), 38-94.Google ScholarGoogle ScholarDigital LibraryDigital Library
  56. Nicolas Wu and Tom Schrijvers. 2015. Fusion for Free-Eficient Algebraic Efect Handlers. In Proceedings of the Conference on Mathematics of Program Construction (Königswinter, Germany). Springer LNCS 9129.Google ScholarGoogle Scholar
  57. Yizhou Zhang and Andrew C. Myers. 2019. Abstraction-safe Efect Handlers via Tunneling. Proc. ACM Program. Lang. 3, POPL, Article 5 ( Jan. 2019 ), 29 pages.Google ScholarGoogle Scholar

Index Terms

  1. Compiling effect handlers in capability-passing style

        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

        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!