Abstract
Algebraic effects and handlers are a powerful and convenient abstraction for user-defined effects. We aim to understand effect handlers through the lens of control operators, a similar but more well-studied tool for expressing effects. In this paper, we establish two program transformations and a type system for effect handlers, all by reusing the existing results about control operators and their relationship to effect handlers.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Similar content being viewed by others
Notes
- 1.
The dollar operator was originally proposed by Kiselyov and Shan [20]. In their formalization, the construct takes the form \(N \, \$ \, M\), where M is the main computation and N is an arbitrary expression representing an ending continuation. We use the bracket notation of Forster et al., restricting N to be an abstraction \(\lambda x.\, N\). This makes it easier to compare dollar with effect handlers.
- 2.
To see what a meta-context is, consider the following program:
$$\begin{aligned} {\langle 2 * \langle 1 + \mathcal {S}_0 k_1.\, \mathcal {S}_0 k_2.\, k_2 \ 3 \mid x.\texttt {return}\ x \rangle \mid x.\texttt {return}\ x \rangle \leadsto ^*6} \end{aligned}$$The first shift0 operator captures the delimited context \(\langle 1 + [] \mid x.\texttt {return}\ x \rangle \), whereas the second one captures the meta-context \(\langle 2 * [] \mid x.\texttt {return}\ x \rangle \) surrounding the innermost dollar operator.
- 3.
The macro translation is originally defined as:
$$\begin{aligned} (\lambda k.\, \langle (\lambda x.\, \mathcal {S}_0 z.\, k \ x) \ \llbracket M \rrbracket _m \rangle ) \ \llbracket N \rrbracket _m \end{aligned}$$We adapted the translation by sequencing the application, and by incorporating the fact that N is always a \(\lambda \)-abstraction.
- 4.
If we wish to apply these translations to a typed language, we would need to slightly modify the definition. The reason is that, the translation of regular handlers yields a single pattern variable x representing the arguments of the ret and op operations, which have different types in general.
- 5.
An anonymous reviewer suggests a different translation that does not use labels:
where \(M \ V\) is a shorthand for \(\texttt {let}\ x = M \ \texttt {in}\ x \ V\) and 0 can be replaced by any constant. The translation implements the return clause by creating thunks (to exit from a handler) and passing around a dummy argument (to trigger computation). This eliminates the need for labels, but in our view, it also makes the translation slightly harder to understand.
- 6.
By “unoptimized translation”, we mean the first-order translation in Fig. 5 of Hillerström et al. [17].
- 7.
The CPS translation of the original dollar operator \(N \, \$ \, M\) is defined as \(\lambda k.\, \llbracket N \rrbracket _c \ (\lambda f.\, \llbracket M \rrbracket _c \ f \ k)\).
- 8.
The original translation of Hillerström et al. [17] packages the two arguments to h into a tuple and associates the tuple with an operation label.
- 9.
In the original type system of Forster et al. [13], effects are attached to the typing judgment instead of being part of a computation type.
- 10.
The effect representation does not allow answer-type modification. A more general (and involved) type system supporting answer-type modification is given by Materzok and Biernacki [30].
- 11.
The interpreter is available at https://github.com/YouyouCong/tfp22.
References
Asai, K.: On typing delimited continuations: three new solutions to the printf problem. Higher-Order Symb. Comput. 22(3), 275–291 (2009). https://doi.org/10.1007/s10990-009-9049-5
Asai, K., Uehara, C.: Selective CPS transformation for shift and reset. In: Proceedings of the ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation, pp. 40–52. PEPM 2018, ACM, New York (2017). https://doi.org/10.1145/3162069
Bauer, A., Pretnar, M.: An effect system for algebraic effects and handlers. In: Heckel, R., Milius, S. (eds.) CALCO 2013. LNCS, vol. 8089, pp. 1–16. Springer, Heidelberg (2013). https://doi.org/10.1007/978-3-642-40206-7_1
Biernacki, D., Danvy, O., Millikin, K.: A dynamic continuation-passing style for dynamic delimited continuations. ACM Trans. Program. Lang. Syst. 38(1), 1–25 (2015). https://doi.org/10.1145/2794078
Biernacki, D., Piróg, M., Polesiuk, P., Sieczkowski, F.: Handle with care: relational interpretation of algebraic effects and handlers. Proc. ACM Program. Lang. 2(POPL), 1–30 (2017). https://doi.org/10.1145/3158096
Biernacki, D., Pyzik, M., Sieczkowski, F.: Reflecting stacked continuations in a fine-grained direct-style reduction theory. In: PPDP 2021, Association for Computing Machinery, pp. 1–13. New York (2021). https://doi.org/10.1145/3479394.3479399
Brachthäuser, J.I., Schuster, P., Ostermann, K.: Effect handlers for the masses. Proc. ACM Program. Lang. 2(OOPSLA), 1–27 (2018)
Brachthäuser, J.I., Schuster, P., Ostermann, K.: Effects as capabilities: effect handlers and lightweight effect polymorphism. Proc. ACM Program. Lang. 4(OOPSLA), 1–30 (2020). https://doi.org/10.1145/3428194
Danvy, O., Filinski, A.: A functional abstraction of typed contexts. BRICS 89/12 (1989)
Danvy, O., Filinski, A.: Abstracting control. In: Proceedings of the 1990 ACM Conference on LISP and Functional Programming, pp. 151–160. ACM (1990)
Dyvbig, R.K., Peyton Jones, S., Sabry, A.: A monadic framework for delimited continuations. J. Funct. Program. 17(6), 687–730 (2007). https://doi.org/10.1017/S0956796807006259
Felleisen, M.: On the expressive power of programming languages. In: Selected Papers from the Symposium on 3rd European Symposium on Programming, pp. 35–75. ESOP 1990, Elsevier North-Holland Inc, New York (1991)
Forster, Y., Kammar, O., Lindley, S., Pretnar, M.: On the expressive power of user-defined effects: effect handlers, monadic reflection, delimited control. J. Funct. Program. 29, e15 (2019). https://doi.org/10.1017/S0956796819000121
Hillerström, D., Lindley, S.: Liberating effects with rows and handlers. In: Proceedings of the 1st International Workshop on Type-Driven Development, pp. 15–27. TyDe 2016, ACM, New York (2016). https://doi.org/10.1145/2976022.2976033
Hillerström, D., Lindley, S.: Shallow effect handlers. In: Ryu, S. (ed.) APLAS 2018. LNCS, vol. 11275, pp. 415–435. Springer, Cham (2018). https://doi.org/10.1007/978-3-030-02768-1_22
Hillerström, D., Lindley, S., Atkey, R.: Effect handlers via generalised continuations. J. Funct. Program. 30, e5 (2020). https://doi.org/10.1017/S0956796820000040
Hillerström, D., Lindley, S., Atkey, R., Sivaramakrishnan, K.: Continuation passing style for effect handlers. In: Proceedings of 2nd International Conference on Formal Structures for Computation and Deduction, pp. 18:1–18:19. FSCD 2017, Schloss Dagstuhl-Leibniz-Zentrum fuer Informatik (2017)
Kameyama, Y., Yonezawa, T.: Typed dynamic control operators for delimited continuations. In: Garrigue, J., Hermenegildo, M.V. (eds.) FLOPS 2008. LNCS, vol. 4989, pp. 239–254. Springer, Heidelberg (2008). https://doi.org/10.1007/978-3-540-78969-7_18
Kammar, O., Lindley, S., Oury, N.: Handlers in action. In: Proceedings of the 18th ACM SIGPLAN International Conference on Functional Programming, pp. 145–158. ICFP 2013, ACM, New York (2013). https://doi.org/10.1145/2500365.2500590
Kiselyov, O., Shan, C.: A substructural type system for delimited continuations. In: Della Rocca, S.R. (ed.) TLCA 2007. LNCS, vol. 4583, pp. 223–239. Springer, Heidelberg (2007). https://doi.org/10.1007/978-3-540-73228-0_17
Kiselyov, O., Sivaramakrishnan, K.C.: Eff directly in OCaml. In: Asai, K., Shinwell, M. (eds.) Proceedings of ML/OCaml 2016, pp. 23–58 (2018). https://doi.org/10.4204/EPTCS.285.2
Kobori, I., Kameyama, Y., Kiselyov, O.: Answer-type modification without tears: prompt-passing style translation for typed delimited-control operators. In: Electronic Proceedings in Theoretical Computer Science EPTCS 212 (Post-Proceedings of the Workshop on Continuations 2015), pp. 36–52 (2016). https://doi.org/10.4204/EPTCS.212.3
Leijen, D.: Koka: programming with row polymorphic effect types. In: 5th Workshop on Mathematically Structured Functional Programming. MSFP 2014 (2014). https://doi.org/10.4204/EPTCS.153.8
Leijen, D.: Type directed compilation of row-typed algebraic effects. In: Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages, pp. 486–499. POPL 2017, ACM, New York (2017). https://doi.org/10.1145/3009837.3009872
Levy, P.B.: Call-By-Push-Value: A Functional/Imperative Synthesis. Springer, Dordrecht (2003). https://doi.org/10.1007/978-94-007-0954-6
Levy, P.B., Power, J., Thielecke, H.: Modelling environments in call-by-value programming languages. Inf. Comput. 185(2), 182–210 (2003)
Lindley, S., McBride, C., McLaughlin, C.: Do be do be do. In: Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages, pp. 500–514. POPL 2017, ACM, New York (2017). https://doi.org/10.1145/3009837.3009897
Materzok, M.: Axiomatizing subtyped delimited continuations. In: Computer Science Logic 2013. CSL 2013, Schloss Dagstuhl-Leibniz-Zentrum fuer Informatik (2013)
Materzok, M., Biernacki, D.: Subtyping delimited continuations. In: Proceedings of the 16th ACM SIGPLAN International Conference on Functional Programming, pp. 81–93. ICFP 2011, ACM, New York (2011). https://doi.org/10.1145/2034773.2034786
Materzok, M., Biernacki, D.: A dynamic interpretation of the CPS hierarchy. In: Jhala, R., Igarashi, A. (eds.) APLAS 2012. LNCS, vol. 7705, pp. 296–311. Springer, Heidelberg (2012). https://doi.org/10.1007/978-3-642-35182-2_21
Nielsen, L.R.: A selective CPS transformation. Electron. Notes Theor. Comput. Sci. 45, 311–331 (2001)
Nielson, F., Nielson, H.R., Hankin, C.: Type and effect systems. In: Principles of Program Analysis, pp. 283–363. Springer, Heidelberg (1999). https://doi.org/10.1007/978-3-662-03811-6_5
Norell, U.: Towards a practical programming language based on dependent type theory. Ph.D. thesis, Chalmers University of Technology (2007)
Piróg, M., Polesiuk, P., Sieczkowski, F.: Typed equivalence of effect handlers and delimited control. In: 4th International Conference on Formal Structures for Computation and Deduction (FSCD 2019). Schloss Dagstuhl-Leibniz-Zentrum fuer Informatik (2019)
Plotkin, G.: Call-by-name, call-by-value and the \(\lambda \)-calculus. Theoret. Comput. Sci. 1(2), 125–159 (1975)
Plotkin, G., Power, J.: Algebraic operations and generic effects. Appl. Categ. Struct. 11(1), 69–94 (2003). https://doi.org/10.1023/A:1023064908962
Plotkin, G., Pretnar, M.: Handlers of algebraic effects. In: Castagna, G. (ed.) ESOP 2009. LNCS, vol. 5502, pp. 80–94. Springer, Heidelberg (2009). https://doi.org/10.1007/978-3-642-00590-9_7
Rémy, D.: Type Inference for Records in Natural Extension of ML, pp. 67–95. MIT Press, Cambridge (1994)
Rompf, T., Maier, I., Odersky, M.: Implementing first-class polymorphic delimited continuations by a type-directed selective CPS-transform. In: Proceedings of the 14th ACM SIGPLAN International Conference on Functional Programming, pp. 317–328. ICFP 2009, ACM, New York (2009). https://doi.org/10.1145/1596550.1596596
Schuster, P., Brachthäuser, J.I., Ostermann, K.: Compiling effect handlers in capability-passing style. Proc. ACM Program. Lang. 4(ICFP), 1–28 (2020). https://doi.org/10.1145/3408975
Shan, C.C.: A static simulation of dynamic delimited control. Higher-Order Symb. Comput. 20(4), 371–401 (2007)
Xie, N., Brachthäuser, J.I., Hillerström, D., Schuster, P., Leijen, D.: Effect handlers, evidently. Proc. ACM Program. Lang. 4(ICFP), 1–29 (2020). https://doi.org/10.1145/3408981
Zhang, Y., Myers, A.C.: Abstraction-safe effect handlers via tunneling. Proc. ACM Program. Lang. 3(POPL), 1–29 (2019). https://doi.org/10.1145/3290318
Zhang, Y., Salvaneschi, G., Myers, A.C.: Handling bidirectional control flow. Proc. ACM Program. Lang. 4(OOPSLA), 1–30 (2020). https://doi.org/10.1145/3428207
Acknowledgments
We gratefully thank the anonymous reviewers for their constructive feedback on the presentation and technical development. This work was supported by JSPS KAKENHI under Grant No. JP18H03218, No. JP19K24339, and No. JP22H03563.
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2022 Springer Nature Switzerland AG
About this paper
Cite this paper
Cong, Y., Asai, K. (2022). Understanding Algebraic Effect Handlers via Delimited Control Operators. In: Swierstra, W., Wu, N. (eds) Trends in Functional Programming. TFP 2022. Lecture Notes in Computer Science, vol 13401. Springer, Cham. https://doi.org/10.1007/978-3-031-21314-4_4
Download citation
DOI: https://doi.org/10.1007/978-3-031-21314-4_4
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-031-21313-7
Online ISBN: 978-3-031-21314-4
eBook Packages: Computer ScienceComputer Science (R0)