Abstract
Pressed by the difficulty of writing asynchronous, event-driven code, mainstream languages have recently been building in support for a variety of advanced control-flow features. Meanwhile, experimental language designs have suggested effect handlers as a unifying solution to programmer-defined control effects, subsuming exceptions, generators, and async–await. However, despite these trends, complex control flow—in particular, control flow that exhibits a bidirectional pattern—remains challenging to manage.
We introduce bidirectional algebraic effects, a new programming abstraction that supports bidirectional control transfer in a more natural way. Handlers of bidirectional effects can raise further effects to transfer control back to the site where the initiating effect was raised, and can use themselves to handle their own effects. We present applications of this expressive power, which falls out naturally as we push toward the unification of effectful programming with object-oriented programming. We pin down the mechanism and the unification formally using a core language that makes generalizations to effect operations and effect handlers.
The usual propagation semantics of control effects such as exceptions conflicts with modular reasoning in the presence of effect polymorphism—it breaks parametricity. Bidirectionality exacerbates the problem. Hence, we set out to show the core language, which builds on the existing tunneling semantics for algebraic effects, is not only type-safe (no effects go unhandled), but also abstraction-safe (no effects are accidentally handled). We devise a step-indexed logical-relations model, and construct its parametricity and soundness proofs. These core results are fully mechanized in Coq. While a full-featured compiler is left to future work, experiments show that as a first-class language feature, bidirectional handlers can be implemented efficiently.
Supplemental Material
- A. Ahmed. Step-indexed syntactic logical relations for recursive and quantified types. In 15th European Symposium on Programming, 2006. Extended/corrected version available as Harvard University TR-01-06.Google Scholar
Digital Library
- S. Alimadadi, D. Zhong, M. Madsen, and F. Tip. Finding broken promises in asynchronous JavaScript programs. Proc. ACM on Programming Languages, 2 (OOPSLA), Oct. 2018.Google Scholar
Digital Library
- A. W. Appel and D. McAllester. An indexed model of recursive types for foundational proof-carrying code. ACM Trans. on Programming Languages and Systems, 23 ( 5 ), Sept. 2001.Google Scholar
- A. W. Appel, P.-A. Melliès, C. D. Richards, and J. Vouillon. A very modal model of a modern, major, general type system. In 34th ACM Symp. on Principles of Programming Languages (POPL), 2007.Google Scholar
Digital Library
- K. Asai and Y. Kameyama. Polymorphic delimited continuations. In 5th Asian Symposium on Programming Languages and Systems (APLAS), 2007.Google Scholar
Cross Ref
- B. Aydemir, A. Charguéraud, B. C. Pierce, R. Pollack, and S. Weirich. Engineering formal metatheory. In 35th ACM Symp. on Principles of Programming Languages (POPL), 2008.Google Scholar
Digital Library
- A. Bauer and M. Pretnar. Programming with algebraic efects and handlers. Journal of Logical and Algebraic Methods in Programming, 84 ( 1 ), 2015.Google Scholar
- G. Bierman, C. Russo, G. Mainland, E. Meijer, and M. Torgersen. Pause 'n' play: Formalizing asynchronous C#. In 26th European Conf. on Object-Oriented Programming, 2012.Google Scholar
Digital Library
- D. Biernacki, M. Piróg, P. Polesiuk, and F. Sieczkowski. Handle with care: Relational interpretation of algebraic efects and handlers. Proc. ACM on Programming Languages, 2 (POPL), Jan. 2018.Google Scholar
Digital Library
- D. Biernacki, M. Piróg, P. Polesiuk, and F. Sieczkowski. Abstracting algebraic efects. Proc. ACM on Programming Languages, 3 (POPL), Jan. 2019.Google Scholar
- D. Biernacki, M. Piróg, P. Polesiuk, and F. Sieczkowski. Binders by day, labels by night: efect instances via lexically scoped handlers. Proc. ACM on Programming Languages, 4 (POPL), Jan. 2020.Google Scholar
Digital Library
- J. I. Brachthäuser, P. Schuster, and K. Ostermann. Algebraic efects for the masses. Proc. ACM on Programming Languages, 2 (OOPSLA), Oct. 2018.Google Scholar
- J. I. Brachthäuser, P. Schuster, and K. Ostermann. Efekt: Capability-passing style for type-and efect-safe, extensible efect handlers in Scala. J. Functional Programming, 30, Mar. 2020.Google Scholar
Cross Ref
- O. Bračevac, N. Amin, G. Salvaneschi, S. Erdweg, P. Eugster, and M. Mezini. Versatile event correlation with algebraic efects. Proc. ACM on Programming Languages, 2 (ICFP), Aug. 2018.Google Scholar
- P. A. Buhr. µ C+ + annotated reference manual, version 7.0.0. Technical report, School of Computer Science, University of Waterloo, 2019.Google Scholar
- B. Cabral and P. Marques. Hidden truth behind.NET's exception handling today. IET Software, 1 ( 6 ), 2007.Google Scholar
- L. Convent, S. Lindley, C. McBride, and C. McLaughlin. Doo bee doo bee doo. J. Functional Programming, 30, Mar. 2020.Google Scholar
Cross Ref
- O. Danvy and A. Filinski. Abstracting control. In ACM Conf. on LISP and Functional Programming, pages 151-160, 1990.Google Scholar
Digital Library
- S. Dolan, S. Eliopoulos, D. Hillerström, A. Madhavapeddy, K. C. Sivaramakrishnan, and L. White. Concurrent system programming with efect handlers. In Trends in Functional Programming, 2017.Google Scholar
- D. Dreyer. Milner award lecture: The type soundness theorem that you really want to prove (and now you can). In 45th ACM Symp. on Principles of Programming Languages (POPL), 2018.Google Scholar
- D. Dreyer, A. Ahmed, and L. Birkedal. Logical step-indexed logical relations. In 24th Annual IEEE Symposium on Logic In Computer Science (LICS), 2009.Google Scholar
Digital Library
- ECMA International. ECMAScript 2018 language specification. Standard-ECMA 262, June 2018.Google Scholar
- M. Felleisen. The theory and practice of first-class prompts. In 15th ACM Symp. on Principles of Programming Languages (POPL), pages 180-190, 1988.Google Scholar
Digital Library
- M. Felleisen. On the expressive power of programming languages. Science of Computer Programming, 17 ( 1 ), 1991.Google Scholar
- Y. Forster, O. Kammar, S. Lindley, and M. Pretnar. On the expressive power of user-defined efects: Efect handlers, monadic reflection, delimited control. Proc. ACM on Programming Languages, 1 (ICFP), Aug. 2017.Google Scholar
- S. Fowler, S. Lindley, J. G. Morris, and S. Decova. Exceptional asynchronous session types. In 46th ACM Symp. on Principles of Programming Languages (POPL), Jan. 2019.Google Scholar
- J. Gosling, B. Joy, G. Steele, G. Bracha, A. Buckley, and D. Smith. The Java Language Specification. Oracle America, se 11 edition, Aug 2018.Google Scholar
- R. E. Griswold, D. R. Hanson, and J. T. Korb. Generators in Icon. ACM Trans. on Programming Languages and Systems, 3 ( 2 ): 144-161, Apr. 1981.Google Scholar
Digital Library
- D. Grossman, G. Morrisett, T. Jim, M. Hicks, Y. Wang, and J. Cheney. Region-based memory management in Cyclone. In ACM SIGPLAN Conf. on Programming Language Design and Implementation (PLDI), pages 282-293. ACM Press, 2002.Google Scholar
Digital Library
- C. A. Gunter, D. Rémy, and J. G. Riecke. A generalization of exceptions and control in ML-like languages. In 7th Conf. on Functional Programming Languages and Computer Architecture (FPCA), 1995.Google Scholar
Digital Library
- C. T. Haynes, D. P. Friedman, and M. Wand. Obtaining coroutines from continuations. Journal of Computer Languages, 11 ( 3-4 ): 143-153, 1986.Google Scholar
- A. Hejlsberg, S. Wiltamuth, and P. Golde. The C # Programming Language. Addison-Wesley, 1st edition, Oct. 2003. ISBN 0321154916.Google Scholar
- D. Hillerström and S. Lindley. Shallow efect handlers. In Asian Symp. on Programming Languages and Systems, 2018.Google Scholar
Cross Ref
- K. Honda, V. T. Vasconcelos, and M. Kubo. Language primitives and type discipline for structured communication-based programming. In European Symposium on Programming, 1999.Google Scholar
- O. Kammar, S. Lindley, and N. Oury. Handlers in action. In 18th ACM SIGPLAN Int'l Conf. on Functional Programming, 2013.Google Scholar
Digital Library
- S. Klabnik and C. Nichols. The Rust Programming Language (Covers Rust 2018 ). No Starch Press, 2019.Google Scholar
- C. Lattner and J. Grof. Async/await for Swift. https://gist.github.com/lattner/429b9070918248274f25b714dcfc7619, 2019.Google Scholar
- D. Leijen. Koka: Programming with row polymorphic efect types. In 5th Workshop on Mathematically Structured Functional Programming. EPTCS, 2014.Google Scholar
Cross Ref
- D. Leijen. Structured asynchrony with algebraic efects. In Proceedings of the 2nd ACM SIGPLAN International Workshop on Type-Driven Development, TyDe 2017, 2017a.Google Scholar
Digital Library
- D. Leijen. Type directed compilation of row-typed algebraic efects. In 44th ACM Symp. on Principles of Programming Languages (POPL), 2017b.Google Scholar
- D. Leijen. Implementing algebraic efects in C. In 15th Asian Symposium on Programming Languages and Systems (APLAS), 2017c.Google Scholar
Cross Ref
- S. Lindley, C. McBride, and C. McLaughlin. Do be do be do. In 44th ACM Symp. on Principles of Programming Languages (POPL), 2017.Google Scholar
Digital Library
- B. Liskov and L. Shrira. Promises: Linguistic support for eficient asynchronous procedure calls in distributed systems. In ACM SIGPLAN Conf. on Programming Language Design and Implementation (PLDI), June 1988.Google Scholar
Digital Library
- B. Liskov, A. Snyder, R. Atkinson, and J. C. Schafert. Abstraction mechanisms in CLU. Comm. of the ACM, 20 ( 8 ): 564-576, Aug. 1977. Also in S. Zdonik and D. Maier, eds., Readings in Object-Oriented Database Systems.Google Scholar
Digital Library
- J. Liu, A. Kimball, and A. C. Myers. Interruptible iterators. In 33rd ACM Symp. on Principles of Programming Languages (POPL), POPL '06, pages 283-294, Jan. 2006.Google Scholar
Digital Library
- J. M. Lucassen and D. K. Giford. Polymorphic efect systems. In 15th ACM Symp. on Principles of Programming Languages (POPL), POPL '88, pages 47-57, 1988.Google Scholar
- E. Meijer, M. Fokkinga, and R. Paterson. Functional programming with bananas, lenses, envelopes and barbed wire. In 5th Conf. on Functional Programming Languages and Computer Architecture (FPCA), 1991.Google Scholar
Cross Ref
- J. G. Mitchell, W. Maybury, and R. Sweet. Mesa language manual version 5.0. Technical Report CSL-79-3, Xerox Research Center, Palo Alto, Ca., 1979.Google Scholar
- J. H. Morris, Jr. Lambda-Calculus Models of Programming Languages. PhD thesis, Massachusetts Institute of Technology, 1968.Google Scholar
- S. Murer, S. Omohundro, D. Stoutamire, and C. Szyperski. Iteration abstraction in Sather. ACM Trans. on Programming Languages and Systems, 18 ( 1 ): 1-15, Jan. 1996.Google Scholar
Digital Library
- S. Okur, D. L. Hartveld, D. Dig, and A. v. Deursen. A study and toolkit for asynchronous programming in C#. In 36th Int'l Conf. on Software Engineering (ICSE), 2014.Google Scholar
Digital Library
- M. Piróg, P. Polesiuk, and F. Sieczkowski. Typed equivalence of efect handlers and delimited control. In 4th International Conference on Formal Structures for Computation and Deduction (FSCD), 2019.Google Scholar
- A. M. Pitts and I. Stark. Operational reasoning for functions with local state. Higher order operational techniques in semantics, pages 227-273, 1998.Google Scholar
- G. Plotkin and J. Power. Algebraic operations and generic efects. Applied Categorical Structures, 11 ( 1 ): 69-94, Feb 2003.Google Scholar
Cross Ref
- G. Plotkin and M. Pretnar. Handling algebraic efects. Logical Methods in Computer Science, Volume 9, Issue 4, Dec. 2013.Google Scholar
Cross Ref
- P. Polesiuk. IxFree: Step-indexed logical relations in Coq. In 3rd International Workshop on Coq for Programming Languages (CoqPL), 2017.Google Scholar
- J. C. Reynolds. Types, abstraction and parametric polymorphism. In IFIP Congress, pages 513-523, 1983.Google Scholar
- Rust language team. Async & await in Rust: a full proposal. https://boats.gitlab.io/blog/post/2018-04-06-async-await-final, 2018.Google Scholar
- M. Shaw, W. Wulf, and R. London. Abstraction and verification in Alphard: Defining and specifying iteration and generators. Comm. of the ACM, 20 ( 8 ), Aug. 1977.Google Scholar
- B. Stroustrup. The C+ + Programming Language. Addison-Wesley, 1987.Google Scholar
- D. Thomas, C. Fowler, and A. Hunt. Programming Ruby: The Pragmatic Programmers' Guide. The Pragmatic Programmers, 2nd edition, 2004. ISBN 0-974-51405-5.Google Scholar
- M. Tofte and J.-P. Talpin. Region-based memory management. Information and Computation, 132 ( 2 ): 109-176, 1997.Google Scholar
- G. van Rossum. The Python Language Reference Manual. Network Theory, Ltd., Sept. 2003.Google Scholar
- M. Wand. Type inference for record concatenation and multiple inheritance. Information and Computation, 93 ( 1 ), 1991.Google Scholar
- Y. Zhang and A. C. Myers. Abstraction-safe efect handlers via tunneling. Proc. ACM on Programming Languages, 3 (POPL), Jan. 2019.Google Scholar
- Y. Zhang, G. Salvaneschi, Q. Beightol, B. Liskov, and A. C. Myers. Accepting blame for safe tunneled exceptions. In 37th ACM SIGPLAN Conf. on Programming Language Design and Implementation (PLDI), pages 281-295, June 2016.Google Scholar
Digital Library
- Y. Zhang, G. Salvaneschi, and A. C. Myers. Handling bidirectional control flow: Technical report. https://arxiv.org/abs/ 2010. 09073, 2020.Google Scholar
Index Terms
Handling bidirectional control flow
Recommendations
Effects as capabilities: effect handlers and lightweight effect polymorphism
Effect handlers have recently gained popularity amongst programming language researchers. Existing type- and effect systems for effect handlers are often complicated and potentially hinder a wide-spread adoption.
We present the language Effekt with ...
Abstraction-safe effect handlers via tunneling
Algebraic effect handlers offer a unified approach to expressing control-flow transfer idioms such as exceptions, iteration, and async/await. Unfortunately, previous attempts to make these handlers type-safe have failed to support the fundamental ...
A reasonably exceptional type theory
Traditional approaches to compensate for the lack of exceptions in type theories for proof assistants have severe drawbacks from both a programming and a reasoning perspective.
Pédrot and Tabareau recently extended the Calculus of Inductive ...






Comments