Hostname: page-component-cd9895bd7-gbm5v Total loading time: 0 Render date: 2024-12-26T06:01:21.030Z Has data issue: false hasContentIssue false

Effect handlers via generalised continuations

Published online by Cambridge University Press:  16 March 2020

DANIEL HILLERSTRÖM
Affiliation:
Laboratory for Foundations of Computer Science, The University of Edinburgh, EdinburghEH8 9YL, UK, (e-mail: [email protected])
SAM LINDLEY
Affiliation:
Laboratory for Foundations of Computer Science, The University of Edinburgh, EdinburghEH8 9YL, UK Department of Computing, Imperial College London, LondonSW7 2BU, UK, (e-mail: [email protected])
ROBERT ATKEY
Affiliation:
Mathematically Structured Programming Group, University of Strathclyde, GlasgowG1 1XQ, UK, (e-mail: [email protected])
Rights & Permissions [Opens in a new window]

Abstract

Core share and HTML view are not available for this content. However, as you have access to this content, a full PDF is available via the ‘Save PDF’ action button.

Plotkin and Pretnar’s effect handlers offer a versatile abstraction for modular programming with user-defined effects. This paper focuses on foundations for implementing effect handlers, for the three different kinds of effect handlers that have been proposed in the literature: deep, shallow, and parameterised. Traditional deep handlers are defined by folds over computation trees and are the original construct proposed by Plotkin and Pretnar. Shallow handlers are defined by case splits (rather than folds) over computation trees. Parameterised handlers are deep handlers extended with a state value that is threaded through the folds over computation trees. We formulate the extensions both directly and via encodings in terms of deep handlers and illustrate how the direct implementations avoid the generation of unnecessary closures. We give two distinct foundational implementations of all the kinds of handlers we consider: a continuation-passing style (CPS) transformation and a CEK-style abstract machine. In both cases, the key ingredient is a generalisation of the notion of continuation to accommodate stacks of effect handlers. We obtain our CPS translation through a series of refinements as follows. We begin with a first-order CPS translation into untyped lambda calculus which manages a stack of continuations and handlers as a curried sequence of arguments. We then refine the initial CPS translation by uncurrying it to yield a properly tail-recursive translation and then moving towards more and more intensional representations of continuations in order to support different kinds of effect handlers. Finally, we make the translation higher order in order to contract administrative redexes at translation time. Our abstract machine design then uses the same generalised continuation representation as the CPS translation. We have implemented both the abstract machine and the CPS transformation (plus extensions) as backends for the Links web programming language.

Type
Research Article
Copyright
© The Author(s) 2020. Published by Cambridge University Press

References

Appel, A. W. (1992) Compiling with Continuations. Cambridge University.Google Scholar
Bauer, A. & Pretnar, M. (2015) Programming with algebraic effects and handlers. J. Log. Alg. Meth. Program. 84(1), 108123.Google Scholar
Berdine, J., O’Hearn, P. W., Reddy, U. S. & Thielecke, H. (2002) Linear continuation-passing. Higher-Order Symb. Comput. 15(2–3), 181208.10.1023/A:1020891112409CrossRefGoogle Scholar
Biernacki, D., Piróg, M., Polesiuk, P. & Sieczkowski, F. (2018) Handle with care: Relational interpretation of algebraic effects and handlers. PACMPL 2(POPL), 8:18:30.Google Scholar
Biernacki, D., Piróg, M., Polesiuk, P. & Sieczkowski, F. (2019) Abstracting algebraic effects. PACMPL 3(POPL), 6:16:28.Google Scholar
Bingham, E., Chen, J. P., Jankowiak, M., Obermeyer, F., Pradhan, N., Karaletsos, T., Singh, R., Szerlip, P., Horsfall, P. & Goodman, N. D. (2018) Pyro: Deep universal probabilistic programming. J. Mach. Learn. Res, 28:1–28:6.Google Scholar
Bouton, C. L. (1901) Nim, a game with a complete mathematical theory. Ann. Math. 3(1/4), 3539.10.2307/1967631CrossRefGoogle Scholar
Brachthäuser, J. I. & Schuster, P. (2017) Effekt: Extensible algebraic effects in scala (short paper). In SCALA@SPLASH. ACM, pp. 6772.10.1145/3136000.3136007CrossRefGoogle Scholar
Brachthäuser, J. I., Schuster, P. & Ostermann, K. (2018) Effect handlers for the masses. PACMPL 2(OOPSLA), 111:1111:27.Google Scholar
Brachthäuser, J. I., Schuster, P. & Ostermann, K. (to appear) Effekt: Capability-passing style for type- and effect-safe, extensible effect handlers in Scala. J. Funct. Program. 30.Google Scholar
Bruggeman, C., Waddell, O. & Dybvig, R. K. (1996) Representing control in the presence of one-shot continuations. In Proceedings of the ACM SIGPLAN’96 Conference on Programming Language Design and Implementation (PLDI), Philadephia, Pennsylvania, May 21–24, 1996, Fischer, C. N. (ed). ACM, pp. 99107.10.1145/231379.231395CrossRefGoogle Scholar
Convent, L., Lindley, S., McBride, C. & McLaughlin, C. (to appear) Doo bee doo bee doo. J. Funct. Program. 30.Google Scholar
Cooper, E., Lindley, S., Wadler, P. & Yallop, J. (2006) Links: Web programming without tiers. In FMCO. LNCS, vol. 4709. Springer, pp. 266296.Google Scholar
Danvy, O. & Filinski, A. (1990) Abstracting control. In LISP and Functional Programming, pp. 151160.10.1145/91556.91622CrossRefGoogle Scholar
Danvy, O. & Filinski, A. (1992) Representing control: A study of the CPS transformation. Math. Struct. Comput. Sci. 2(4), 361391.10.1017/S0960129500001535CrossRefGoogle Scholar
Danvy, O. & Nielsen, L. R. (2003) A first-order one-pass CPS transformation. Theor. Comput. Sci. 308(1–3), 239257.10.1016/S0304-3975(02)00733-8CrossRefGoogle Scholar
Dolan, S., Eliopoulos, S., Hillerström, D., Madhavapeddy, A., Sivaramakrishnan, K. C. & White, L. (2017) Concurrent system programming with effect handlers. In TFP. Lecture Notes in Computer Science, vol. 10788. Springer, pp. 98117.Google Scholar
Dolan, S., White, L. & Madhavapeddy, A. (2014) Multicore OCaml. In OCaml Workshop.Google Scholar
Dolan, S., White, L., Sivaramakrishnan, K. C., Yallop, J. & Madhavapeddy, A. (2015) Effective concurrency through algebraic effects. In OCaml Workshop.Google Scholar
Dybvig, R. K., Peyton Jones, S. L. & Sabry, A. (2007) A monadic framework for delimited continuations. J. Funct. Program. 17(6), 687730.Google Scholar
Felleisen, M. & Friedman, D. P. (1987) Control operators, the SECD-machine, and the λ-calculus. In Formal Description of Programming Concepts III, Wirsing, Martin (ed). Elsevier, pp. 193217.Google Scholar
Flanagan, C., Sabry, A., Duba, B. F. & Felleisen, M. (1993) The essence of compiling with continuations. In PLDI. ACM, pp. 237247.10.1145/155090.155113CrossRefGoogle Scholar
Fokkinga, M. M. (1990) Tupling and mutumorphisms. Squiggolist 1(4), 8182.Google Scholar
Forster, Y., Kammar, O., Lindley, S. & Pretnar, M. (2017) On the expressive power of user-defined effects: Effect handlers, monadic reflection, delimited control. PACMPL 1(ICFP), e15.Google Scholar
Forster, Y., Kammar, O., Lindley, S. & Pretnar, M. (2019) On the expressive power of user-defined effects: Effect handlers, monadic reflection, delimited control. J. Funct. Program. 29, e15.10.1017/S0956796819000121CrossRefGoogle Scholar
Hillerström, D. (2015) Handlers for Algebraic Effects in Links. MSc thesis, The University of Edinburgh, Scotland.Google Scholar
Hillerström, D. (2016) Compilation of Effect Handlers and their Applications in Concurrency. MSc(R) thesis, The University of Edinburgh, Scotland.Google Scholar
Hillerström, D. & Lindley, S. (2016) Liberating effects with rows and handlers. In TyDe@ICFP. ACM, pp. 1527.10.1145/2976022.2976033CrossRefGoogle Scholar
Hillerström, D. & Lindley, S. (2018) Shallow effect handlers. In APLAS, vol. 11275. Springer International Publishing, pp. 415433.Google Scholar
Hillerström, D., Lindley, S., Atkey, R. & Sivaramakrishnan, K. C. (2017) Continuation passing style for effect handlers. In FSCD. LIPIcs, vol. 84. Schloss Dagstuhl - Leibniz-Zentrum fuer Informatik, pp. 18:1–18:19.Google Scholar
Hillerström, D., Lindley, S. & Sivaramakrishnan, K. C. (2016) Compiling Links effect handlers to the OCaml backend. In ML Workshop.Google Scholar
Huet, G. P. (1997) The zipper. J. Funct. Program. 7(5), 549554.10.1017/S0956796897002864CrossRefGoogle Scholar
James, R. P. & Sabry, A. (2011) Yield: Mainstream delimited continuations. In TPDC.Google Scholar
Kammar, O., Lindley, S. & Oury, N. (2013) Handlers in action. In ICFP. ACM, pp. 145158.10.1145/2500365.2500590CrossRefGoogle Scholar
Kennedy, A. (2007) Compiling with continuations, continued. In ICFP. ACM, pp. 177190.10.1145/1291151.1291179CrossRefGoogle Scholar
Kiselyov, O. (2012) Delimited control in OCaml, abstractly and concretely. Theor. Comput. Sci. 435, 5676.10.1016/j.tcs.2012.02.025CrossRefGoogle Scholar
Kiselyov, O. & Sivaramakrishnan, K. C. (2016) Eff directly in OCaml. In ML Workshop.Google Scholar
Kranz, D., Kelsey, R., Rees, J., Hudak, P., Philbin, J. & Adams, N. (1986) ORBIT: An optimizing compiler for Scheme. 21(7), 219233. Proceedings of the SIGPLAN’86 Symposium on Compiler Construction.10.1145/13310.13333CrossRefGoogle Scholar
Leijen, D. (2014) Koka: Programming with row polymorphic effect types. In MSFP. EPTCS, vol. 153, pp. 100126.10.4204/EPTCS.153.8CrossRefGoogle Scholar
Leijen, D. (2017a) Implementing algebraic effects in C - “monads for free in C”. In APLAS. Lecture Notes in Computer Science, vol. 10695. Springer, pp. 339363.Google Scholar
Leijen, D. (2017b) Structured asynchrony with algebraic effects. In TyDe@ICFP. ACM, pp. 1629.10.1145/3122975.3122977CrossRefGoogle Scholar
Leijen, D. (2017c) Type directed compilation of row-typed algebraic effects. In POPL. ACM, pp. 486499.10.1145/3093333.3009872CrossRefGoogle Scholar
Levy, P. B., Power, J. & Thielecke, H. (2003) Modelling environments in call-by-value programming languages. Inf. Comput. 185(2), 182210.10.1016/S0890-5401(03)00088-9CrossRefGoogle Scholar
Lindley, S., McBride, C. & McLaughlin, C. (2017) Do be do be do. In POPL. ACM, pp. 500514.10.1145/3009837.3009897CrossRefGoogle Scholar
Materzok, M. & Biernacki, D. (2012) A dynamic interpretation of the CPS hierarchy. In APLAS. LNCS, vol. 7705. Springer, pp. 296311.Google Scholar
Meijer, E., Fokkinga, M. M. & Paterson, R. (1991) Functional programming with bananas, lenses, envelopes and barbed wire. In FPCA. Lecture Notes in Computer Science, vol. 523. Springer, pp. 124144.Google Scholar
Piróg, M., Polesiuk, P. & Sieczkowski, F. (2019) Typed equivalence of effect handlers and delimited control. In FSCD. LIPIcs, vol. 131. Schloss Dagstuhl - Leibniz-Zentrum fuer Informatik, pp. 30:1–30:16.Google Scholar
Plotkin, G. D. (1975) Call-by-name, call-by-value and the lambda-calculus. Theor. Comput. Sci. 1(2), 125159.10.1016/0304-3975(75)90017-1CrossRefGoogle Scholar
Plotkin, G. D. & Power, J. (2001) Adequacy for algebraic effects. In FoSSaCS. LNCS, vol. 2030. Springer, pp. 124.Google Scholar
Plotkin, G. D. & Pretnar, M. (2013) Handling algebraic effects. Log. Methods Comput. Sci. 9(4).10.2168/LMCS-9(4:23)2013CrossRefGoogle Scholar
Pretnar, M. (2015) An introduction to algebraic effects and handlers. Electr. Notes Theor. Comput. Sci. 319, 1935. Invited tutorial paper.10.1016/j.entcs.2015.12.003CrossRefGoogle Scholar
Remy, D. (1993). Syntactic Theories and the Algebra of Record Terms. Technical report RR-1869. INRIA.Google Scholar
Shan, C.-c. (2007) A static simulation of dynamic delimited control. Higher-Order Symb. Comput 20(4), 371401.10.1007/s10990-007-9010-4CrossRefGoogle Scholar
Steele, G. (1978) RABBIT: A Compiler for SCHEME. Technical report. AITR-474. INRIA.Google Scholar
Wadler, P. (1995) Monads for functional programming. In Advanced Functional Programming. Lecture Notes in Computer Science, vol. 925. Springer, pp. 2452.Google Scholar
Wu, N. & Schrijvers, T. (2015) Fusion for free - efficient algebraic effect handlers. In MPC. Lecture Notes in Computer Science, vol. 9129. Springer, pp. 302322.Google Scholar
Wu, N., Schrijvers, T. & Hinze, R. (2014) Effect handlers in scope. In Proceedings of the 2014 ACM SIGPLAN Symposium on Haskell, Gothenburg, Sweden, September 4–5, 2014, Swierstra, W. (ed). ACM, pp. 112.10.1145/2633357.2633358CrossRefGoogle Scholar
Yallop, J. (2017) Staged generic programming. PACMPL 1(ICFP), 29:129:29.Google Scholar
Submit a response

Discussions

No Discussions have been published for this article.