skip to main content

First-class names for effect handlers

Published:31 October 2022Publication History
Skip Abstract Section

Abstract

Algebraic effects and handlers are a promising technique for incorporating composable computational effects into functional programming languages. Effect handlers enable concisely programming with different effects, but they do not offer a convenient way to program with different instances of the same effect. As a solution to this inconvenience, previous studies have introduced _named effect handlers_, which allow the programmer to distinguish among different effect instances. However, existing formalizations of named handlers are both involved and restrictive, as they employ non-standard mechanisms to prevent the escaping of handler names.

In this paper, we propose a simple and flexible design of named handlers. Specifically, we treat handler names as first-class values, and prevent their escaping while staying within the ordinary λ-calculus. Such a design is enabled by combining named handlers with _scoped effects_, a novel variation of effects that maintain a scope via rank-2 polymorphism. We formalize two combinations of named handlers and scoped effects, and implement them in the Koka programming language. We also present practical applications of named handlers, including a neural network and a unification algorithm.

References

  1. Andrej Bauer and Matija Pretnar. 2014. An Effect System for Algebraic Effects and Handlers. Logical Methods in Computer Science, 10, 4 (2014). Google ScholarGoogle Scholar
  2. Andrej Bauer and Matija Pretnar. 2015. Programming with algebraic effects and handlers. Journal of Logical and Algebraic Methods in Programming, 84, 1 (2015), 108–123. Google ScholarGoogle ScholarCross RefCross Ref
  3. Dariusz Biernacki, Maciej Piróg, Piotr Polesiuk, and Filip Sieczkowski. 2017. Handle with Care: Relational Interpretation of Algebraic Effects and Handlers. Proc. ACM Program. Lang., 2, POPL’17 issue (2017), Article 8, Dec., 30 pages. issn:2475-1421 https://doi.org/10.1145/3158096 Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Dariusz Biernacki, Maciej Piróg, Piotr Polesiuk, and Filip Sieczkowski. 2019. Binders by Day, Labels by Night: Effect Instances via Lexically Scoped Handlers. Proc. ACM Program. Lang., 4, POPL (2019), Article 48, Dec., 29 pages. 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 Szerlip, Paul Horsfall, and Noah D Goodman. 2019. Pyro: Deep universal probabilistic programming. The Journal of Machine Learning Research, 20, 1 (2019), 973–978. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Oliver Bračevac, Nada Amin, Guido Salvaneschi, Sebastian Erdweg, Patrick Eugster, and Mira Mezini. 2018. Versatile event correlation with algebraic effects. Proceedings of the ACM on Programming Languages, 2, ICFP (2018), 67. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Jonathan Immanuel Brachthäuser, Philipp Schuster, and Klaus Ostermann. 2020. Effects as Capabilities: Effect Handlers and Lightweight Effect Polymorphism. Proc. ACM Program. Lang., 5 (2020). Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Lukas Convent, Sam Lindley, Conor McBride, and Craig McLaughlin. 2020. Doo bee doo bee doo. In the Journal of Functional Programming, Jan.. Google ScholarGoogle Scholar
  9. Stephen Dolan, Spiros Eliopoulos, Daniel Hillerström, Anil Madhavapeddy, KC Sivaramakrishnan, and Leo White. 2017. Effectively Tackling the Awkward Squad. In ML Workshop. Google ScholarGoogle Scholar
  10. Derek Dreyer. 2018. The Type Soundness Theorem That You Really Want to Prove (and now you can). Milner Award Lecture, POPL 2018 Google ScholarGoogle Scholar
  11. R Kent Dyvbig, 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 ScholarGoogle ScholarDigital LibraryDigital Library
  12. 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 ScholarGoogle ScholarCross RefCross Ref
  13. 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 Google ScholarGoogle Scholar
  14. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  15. Ralf Hinze. 2003. Fun with phantom types. In The Fun of Programming, Cornerstones of Computing, eremy Gibbons and Oege de Moor (Eds.). Palgrave Macmillan, 245–262. isbn:1-4039-0772-2 Google ScholarGoogle Scholar
  16. Mark P. Jones. 1996. Using Parameterized Signatures to Express Modular Structure. In Proceedings of the 23rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’96). 68–78. isbn:0897917693 https://doi.org/10.1145/237721.237731 Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. 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 ScholarGoogle ScholarCross RefCross Ref
  18. 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). ACM, New York, NY, USA. 26–37. isbn:1-59593-309-3 Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Oleg Kiselyov and KC Sivaramakrishnan. 2017. Eff directly in OCaml. In ML workshop 2016. http://kcsrk.info/papers/caml-eff17.pdf Extended version Google ScholarGoogle Scholar
  20. Koka. 2019. https://github.com/koka-lang/koka Google ScholarGoogle Scholar
  21. Daan Leijen. 2008. HMF: Simple Type Inference for First-Class Polymorphism. In Proceedings of the 13th ACM Symposium of the International Conference on Functional Programming (ICFP’08). https://doi.org/10.1145/1411204.1411245 Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Daan Leijen. 2014. Koka: Programming with Row Polymorphic Effect Types. In MSFP’14, 5th workshop on Mathematically Structured Functional Programming. https://doi.org/10.4204/EPTCS.153.8 Google ScholarGoogle ScholarCross RefCross Ref
  23. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  24. Daan Leijen. 2018. First Class Dynamic Effect Handlers: Or, Polymorphic Heaps with Dynamic Effect Handlers. In Proceedings of the 3rd ACM SIGPLAN International Workshop on Type-Driven Development (TyDe 2018). 51–64. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Daan Leijen and Erik Meijer. 1999. Domain Specific Embedded Compilers. In In proceedings of the 2nd conference on domain specific languages. 109–122. Google ScholarGoogle Scholar
  26. Sam Lindley, Connor McBride, and Craig McLaughlin. 2017. Do be do be do. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages (POPL’17). 500–514. https://doi.org/10.1145/3009837.3009897 Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. McCracken. 1984. The typechecking of programs with implicit type structure. In Lecture Notes in Computer Science (Semantics of Data Types, Vol. 173). Google ScholarGoogle Scholar
  28. Simon L Peyton Jones and John Launchbury. 1995. State in Haskell. Lisp and Symbolic Comp., 8, 4 (1995), 293–341. https://doi.org/10.1007/BF01018827 Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. 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
  30. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  31. 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 ScholarGoogle ScholarCross RefCross Ref
  32. Matija Pretnar. 2010. Logic and handling of algebraic effects. Ph. D. Dissertation. Google ScholarGoogle Scholar
  33. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  34. Alex Reinking, Ningning Xie, Leonardo de Moura, and Daan Leijen. 2021. Perceus: Garbage Free Reference Counting with Reuse. In Proceedings of the 42nd ACM SIGPLAN International Conference on Programming Language Design and Implementation (PLDI 2021). Association for Computing Machinery, New York, NY, USA. 96–111. isbn:9781450383912 https://doi.org/10.1145/3453483.3454032 Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. Taro Sekiyama and Atsushi Igarashi. 2019. Handling polymorphic algebraic effects. In European Symposium on Programming. 353–380. Google ScholarGoogle ScholarCross RefCross Ref
  36. Miley Semmelroth and Amr Sabry. 1999. Monadic Encapsulation in ML. In Proceedings of the Fourth ACM SIGPLAN International Conference on Functional Programming (ICFP ’99). Association for Computing Machinery, New York, NY, USA. 8–17. isbn:1581131119 https://doi.org/10.1145/317636.317777 Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. Chung-chieh Shan. 2007. A static simulation of dynamic delimited control. Higher-Order and Symbolic Computation, 20, 4 (2007), 371–401. issn:1573-0557 https://doi.org/10.1007/s10990-007-9010-4 Google ScholarGoogle ScholarDigital LibraryDigital Library
  38. Jesse Sigal. 2021. Automatic Differentiation via Effects and Handlers: An Implementation in Frank. In The ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation (PEPM ’21). Google ScholarGoogle Scholar
  39. Dorai Sitaram. 1993. Handling control. In Proceedings of the ACM SIGPLAN 1993 conference on Programming language design and implementation. 147–155. Google ScholarGoogle ScholarDigital LibraryDigital Library
  40. Asumu Takikawa, T Stephen Strickland, and Sam Tobin-Hochstadt. 2013. Constraining delimited control with contracts. In European Symposium on Programming. 229–248. Google ScholarGoogle ScholarDigital LibraryDigital Library
  41. Amin Timany, Léo Stefanesco, Morten Krogh-Jespersen, and Lars Birkedal. 2017. A Logical Relation for Monadic Encapsulation of State: Proving Contextual Equivalences in the Presence of RunST. Proc. ACM Program. Lang., 2, POPL (2017), Article 64, Dec., 28 pages. https://doi.org/10.1145/3158152 Google ScholarGoogle ScholarDigital LibraryDigital Library
  42. Mads Tofte and Jean-Pierre Talpin. 1997. Region-based memory management. Information and computation, 132, 2 (1997), 109–176. Google ScholarGoogle Scholar
  43. Fei Wang, Daniel Zheng, James Decker, Xilun Wu, Grégory M Essertel, and Tiark Rompf. 2019. Demystifying differentiable programming: Shift/reset the penultimate backpropagator. Proceedings of the ACM on Programming Languages, 3, ICFP (2019), 1–31. Google ScholarGoogle ScholarDigital LibraryDigital Library
  44. Andrew K. Wright and Matthias Felleisen. 1994. A syntactic approach to type soundness. Inf. Comput., 115, 1 (1994), Nov., 38–94. https://doi.org/10.1006/inco.1994.1093 Google ScholarGoogle ScholarDigital LibraryDigital Library
  45. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  46. Ningning Xie, Jonathan Brachthäuser, Phillip Schuster, Daniel Hillerström, and Daan Leijen. 2020. Effect Handlers, Evidently. In Proceedings of the 25th ACM SIGPLAN International Conference on Functional Programming (ICFP’2020) (ICFP ’20). Google ScholarGoogle Scholar
  47. Ningning Xie, Youyou Cong, Kazuki Ikemori, and Daan Leijen. 2022. Artifact for "First-class Names for Effect Handlers". https://doi.org/10.5281/zenodo.7062933 Also available at <https://hub.docker.com/r/daanx/oopsla22-namedh> Google ScholarGoogle ScholarDigital LibraryDigital Library
  48. Ningning Xie and Daan Leijen. 2020. Effect Handlers in Haskell, Evidently. In Proceedings of the 2020 ACM SIGPLAN Symposium on Haskell (Haskell’20). Google ScholarGoogle Scholar
  49. 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, Article 71, 30 pages. https://doi.org/10.1145/3473576 Google ScholarGoogle ScholarDigital LibraryDigital Library
  50. 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 ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. First-class names for effect handlers

        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)498
          • Downloads (Last 6 weeks)57

          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!