Skip to main content

Structured Handling of Scoped Effects

  • 1027 Accesses

Part of the Lecture Notes in Computer Science book series (LNCS,volume 13240)

Abstract

Algebraic effects offer a versatile framework that covers a wide variety of effects. However, the family of operations that delimit scopes are not algebraic and are usually modelled as handlers, thus preventing them from being used freely in conjunction with algebraic operations. Although proposals for scoped operations exist, they are either ad-hoc and unprincipled, or too inconvenient for practical programming. This paper provides the best of both worlds: a theoretically-founded model of scoped effects that is convenient for implementation and reasoning. Our new model is based on an adjunction between a locally finitely presentable category and a category of functorial algebras. Using comparison functors between adjunctions, we show that our new model, an existing indexed model, and a third approach that simulates scoped operations in terms of algebraic ones have equal expressivity for handling scoped operations. We consider our new model to be the sweet spot between ease of implementation and structuredness. Additionally, our approach automatically induces fusion laws of handlers of scoped effects, which are useful for reasoning and optimisation.

Keywords

  • Computational effects
  • Category theory
  • Haskell
  • Algebraic theories
  • Scoped effects
  • Handlers
  • Abstract syntax

References

  1. Adámek, J., Rosicky, J.: Locally Presentable and Accessible Categories. London Mathematical Society Lecture Note Series, Cambridge University Press (1994). https://doi.org/10.1017/CBO9780511600579

  2. Adámek, J.: Free algebras and automata realizations in the language of categories. Commentationes Mathematicae Universitatis Carolinae 015(4), 589–602 (1974), http://eudml.org/doc/16649

  3. Ahman, D., Pretnar, M.: Asynchronous effects. Proc. ACM Program. Lang. 5(POPL) (Jan 2021). https://doi.org/10.1145/3434305

  4. Backhouse, R., Bijsterveld, M., van Geldrop, R., van der Woude, J.: Categorical fixed point calculus. In: Pitt, D., Rydeheard, D.E., Johnstone, P. (eds.) Category Theory and Computer Science. pp. 159–179. Springer Berlin Heidelberg, Berlin, Heidelberg (1995). https://doi.org/10.1007/3-540-60164-3_25

  5. Barr, M.: Coequalizers and free triples. Mathematische Zeitschrift (1970)

    Google Scholar 

  6. Barr, M., Wells, C.: Category theory for computing science, vol. 1. Prentice Hall New York (1990)

    Google Scholar 

  7. Bauer, A., Pretnar, M.: An effect system for algebraic effects and handlers. Logical Methods in Computer Science 10(4) (Dec 2014). https://doi.org/10.2168/lmcs-10(4:9)2014

  8. Bauer, A., Pretnar, M.: Programming with algebraic effects and handlers. J. Log. Algebraic Methods Program. 84(1), 108–123 (2015). https://doi.org/10.1016/j.jlamp.2014.02.001

  9. van den Berg, B., Schrijvers, T., Bach-Poulsen, C., Wu, N.: Latent effects for reusable language components: Extended version (2021), https://arxiv.org/abs/2108.11155

  10. Bird, R.S., Paterson, R.: Generalised folds for nested datatypes. Formal Aspects Comput. (1999). https://doi.org/10.1007/s001650050047

  11. Claessen, K.: A poor man’s concurrency monad. Journal of Functional Programming 9(3), 313–323 (1999). https://doi.org/10.1017/S0956796899003342

  12. Coutts, D., Leshchinskiy, R., Stewart, D.: Stream fusion: From lists to streams to nothing at all. SIGPLAN Not. 42(9), 315–326 (Oct 2007). https://doi.org/10.1145/1291220.1291199

  13. Dijkstra, E.W.: Letters to the editor: Go to statement considered harmful. Commun. ACM 11(3), 147–148 (Mar 1968). https://doi.org/10.1145/362929.362947

  14. Fiore, M., Hur, C.K.: On the construction of free algebras for equational systems. Theoretical Computer Science 410(18), 1704–1729 (2009). https://doi.org/10.1016/j.tcs.2008.12.052, automata, Languages and Programming (ICALP 2007)

  15. Fiore, M., Mahmoud, O.: Second-order algebraic theories. In: Hliněný, P., Kučera, A. (eds.) Mathematical Foundations of Computer Science 2010. pp. 368–380. Springer Berlin Heidelberg, Berlin, Heidelberg (2010). https://doi.org/10.1007/978-3-642-15155-2_33

  16. Fiore, M.P., Plotkin, G.D., Turi, D.: Abstract syntax and variable binding. In: 14th Annual IEEE Symposium on Logic in Computer Science, Trento, Italy, July 2-5, 1999 (1999). https://doi.org/10.1109/LICS.1999.782615

  17. Fiore, M.P., Turi, D.: Semantics of name and value passing. In: 16th Annual IEEE Symposium on Logic in Computer Science, Boston, Massachusetts, USA, June 16-19, 2001, Proceedings (2001). https://doi.org/10.1109/LICS.2001.932486

  18. Ghani, N., Uustalu, T.: Explicit substitutions and higher-order syntax. In: Proceedings of the 2003 ACM SIGPLAN Workshop on Mechanized Reasoning about Languages with Variable Binding. p. 1–7. MERLIN ’03, Association for Computing Machinery, New York, NY, USA (2003). https://doi.org/10.1145/976571.976580

  19. Hillerström, D., Lindley, S.: Shallow Effect Handlers. Lecture Notes in Computer Science 11275 LNCS, 415–435 (2018). https://doi.org/10.1007/978-3-030-02768-1_22

  20. Hinze, R.: Prological features in a functional setting — axioms and implementations. In: Sato, M., Toyama, Y. (eds.) Proceedings of the Third Fuji International Symposium on Functional and Logic Programming (FLOPS ’98). pp. 98–122. World Scientific, Singapore, New Jersey, London, Hong Kong (apr 1998)

    Google Scholar 

  21. Hinze, R., Harper, T., James, D.W.H.: Theory and practice of fusion. In: Hage, J., Morazán, M.T. (eds.) Implementation and Application of Functional Languages. pp. 19–37. Springer Berlin Heidelberg, Berlin, Heidelberg (2011). https://doi.org/10.1007/978-3-642-24276-2_2

  22. Hyland, M., Plotkin, G., Power, J.: Combining effects: Sum and tensor. Theor. Comput. Sci. 357(1), 70–99 (Jul 2006). https://doi.org/10.1016/j.tcs.2006.03.013

  23. Hyland, M., Power, J.: The category theoretic understanding of universal algebra: Lawvere theories and monads. Electronic Notes in Theoretical Computer Science 172, 437–458 (2007). https://doi.org/10.1016/j.entcs.2007.02.019, computation, Meaning, and Logic: Articles dedicated to Gordon Plotkin

  24. Johann, P., Ghani, N.: Initial algebra semantics is enough! In: Typed Lambda Calculi and Applications, TLCA. Lecture Notes in Computer Science, Springer (2007). https://doi.org/10.1007/978-3-540-73228-0_16

  25. Johann, P., Ghani, N.: Foundations for structured programming with gadts. In: Necula, G.C., Wadler, P. (eds.) Proceedings of the 35th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2008, San Francisco, California, USA, January 7-12, 2008. pp. 297–308. ACM (2008). https://doi.org/10.1145/1328438.1328475

  26. Jones, M.P., Duponcheel, L.: Composing monads. Research Report YALEU/DCS/RR-1004, Yale University, New Haven, Connecticut, USA (December 1993), http://web.cecs.pdx.edu/~mpj/pubs/RR-1004.pdf

  27. Kammar, O., Lindley, S., Oury, N.: Handlers in action. SIGPLAN Not. 48(9), 145–158 (Sep 2013). https://doi.org/10.1145/2544174.2500590

  28. Kammar, O., Plotkin, G.D.: Algebraic foundations for effect-dependent optimisations. In: Proceedings of the 39th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. p. 349–360. POPL ’12, Association for Computing Machinery, New York, NY, USA (2012). https://doi.org/10.1145/2103656.2103698

  29. Kelly, G.M.: Structures defined by finite limits in the enriched context, i. Cahiers de Topologie et Géométrie Différentielle Catégoriques 23(1), 3–42 (1982), http://www.numdam.org/item/CTGDC_1982__23_1_3_0/

  30. Kelly, G., Power, A.: Adjunctions whose counits are coequalizers, and presentations of finitary enriched monads. Journal of Pure and Applied Algebra 89(1), 163–179 (1993). https://doi.org/10.1016/0022-4049(93)90092-8

  31. Kidney, D.O., Wu, N.: Algebras for weighted search. Proc. ACM Program. Lang. 5(ICFP) (Aug 2021). https://doi.org/10.1145/3473577

  32. King, A.: eff – screaming fast extensible effects for less (2019), https://github.com/hasura/eff

  33. Kiselyov, O., Ishii, H.: Freer monads, more extensible effects. In: Proceedings of the 2015 ACM SIGPLAN Symposium on Haskell. p. 94–105. Haskell ’15, Association for Computing Machinery, New York, NY, USA (2015). https://doi.org/10.1145/2804302.2804319

  34. Kiselyov, O., Mu, S.C., Sabry, A.: Not by equations alone: Reasoning with extensible effects. Journal of Functional Programming 31,  e2 (2021). https://doi.org/10.1017/S0956796820000271

  35. Lambek, J., Scott, P.J.: Introduction to Higher Order Categorical Logic. Cambridge University Press, USA (1986)

    Google Scholar 

  36. Leijen, D.: Type directed compilation of row-typed algebraic effects. In: Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages. p. 486–499. POPL 2017, Association for Computing Machinery, New York, NY, USA (2017). https://doi.org/10.1145/3009837.3009872

  37. Levy, P.B.: Adjunction models for call-by-push-value with stacks. Electronic Notes in Theoretical Computer Science 69, 248–271 (2003). https://doi.org/10.1016/S1571-0661(04)80568-1, cTCS’02, Category Theory and Computer Science

  38. Levy, P.B.: Call-by-push-value: A Functional/Imperative Synthesis, vol. 2. Springer Netherlands (2003). https://doi.org/10.1007/978-94-007-0954-6

  39. Liang, S., Hudak, P., Jones, M.: Monad transformers and modular interpreters. In: ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages. pp. 333–343. POPL ’95, ACM (1995). https://doi.org/10.1145/199448.199528

  40. Lindley, S., McBride, C., McLaughlin, C.: Do be do be do. In: Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages. p. 500–514. POPL 2017, Association for Computing Machinery, New York, NY, USA (2017). https://doi.org/10.1145/3009837.3009897

  41. Mac Lane, S.: Categories for the Working Mathematician, 2nd edn. Graduate Texts in Mathematics, Springer, Berlin (1998)

    Google Scholar 

  42. Maguire, S.: polysemy: Higher-order, low-boilerplate free monads (2019), https://hackage.haskell.org/package/polysemy

  43. Matthes, R., Uustalu, T.: Substitution in non-wellfounded syntax with variable binding. Theoretical Computer Science 327(1), 155–174 (2004). https://doi.org/10.1016/j.tcs.2004.07.025, selected Papers of CMCS ’03

  44. Moggi, E.: An abstract view of programming languages. Tech. Rep. ECS-LFCS-90-113, Edinburgh University, Department of Computer Science (June 1989)

    Google Scholar 

  45. Moggi, E.: Notions of computation and monads. Information and Computation 93(1), 55 – 92 (1991). https://doi.org/10.1016/0890-5401(91)90052-4, selections from 1989 IEEE Symposium on Logic in Computer Science

  46. Piróg, M., Schrijvers, T., Wu, N., Jaskelioff, M.: Syntax and semantics for operations with scopes. In: Proceedings of the 33rd Annual ACM/IEEE Symposium on Logic in Computer Science. p. 809–818. LICS ’18, Association for Computing Machinery, New York, NY, USA (2018). https://doi.org/10.1145/3209108.3209166

  47. Piróg, M., Gibbons, J.: Tracing monadic computations and representing effects. Electronic Proceedings in Theoretical Computer Science 76, 90–111 (Feb 2012). https://doi.org/10.4204/eptcs.76.8

  48. Plotkin, G., Power, J.: Notions of computation determine monads. In: Nielsen, M., Engberg, U. (eds.) Foundations of Software Science and Computation Structures, 5th International Conference. pp. 342–356. FOSSACS 2002, Springer (2002). https://doi.org/10.1007/3-540-45931-6_24

  49. Plotkin, G., Power, J.: Algebraic operations and generic effects. Applied Categorical Structures 11(1), 69–94 (2003). https://doi.org/10.1023/A:1023064908962

  50. Plotkin, G., Pretnar, M.: A logic for algebraic effects. In: 2008 23rd Annual IEEE Symposium on Logic in Computer Science. pp. 118–129 (2008). https://doi.org/10.1109/LICS.2008.45

  51. Plotkin, G., Pretnar, M.: Handlers of algebraic effects. In: Castagna, G. (ed.) Programming Languages and Systems. pp. 80–94. Springer Berlin Heidelberg, Berlin, Heidelberg (2009). https://doi.org/10.1007/978-3-642-00590-9_7

  52. Plotkin, G., Pretnar, M.: Handling algebraic effects. Logical Methods in Computer Science 9(4) (Dec 2013). https://doi.org/10.2168/lmcs-9(4:23)2013

  53. Poulsen, C.B., van der Rest, C., Schrijvers, T.: Staged effects and handlers for modular languages with abstraction. In: Workshop on Partial Evaluation and Program Manipulation (PEPM) (2021), https://casvdrest.github.io/staged-effects.agda/pepm21.pdf

  54. Power, A.J.: Enriched Lawvere theories. Theory and Applications of Categories 6(7), 83–93 (1999)

    Google Scholar 

  55. Riehl, E.: Category Theory in Context. Aurora: Dover Modern Math Originals, Dover Publications (2017)

    Google Scholar 

  56. Rix, R., Thomson, P., Wu, N., Schrijvers, T.: fused-effects: A fast, flexible, fused effect system (2018), https://hackage.haskell.org/package/fused-effects

  57. Robinson, E.: Variations on algebra: Monadicity and generalisations of equational theories. Form. Asp. Comput. 13(3–5), 308–326 (Jul 2002). https://doi.org/10.1007/s001650200014

  58. Schrijvers, T., Piróg, M., Wu, N., Jaskelioff, M.: Monad transformers and modular algebraic effects: what binds them together. In: Proceedings of the 12th ACM SIGPLAN International Symposium on Haskell, Haskell@ICFP 2019, Berlin, Germany, August 18-23, 2019. pp. 98–113 (2019). https://doi.org/10.1145/3331545.3342595

  59. Spivey, J.M.: Algebras for combinatorial search. Journal of Functional Programming 19(3-4), 469–487 (2009). https://doi.org/10.1017/S0956796809007321

  60. Spivey, M.: A functional theory of exceptions. Science of Computer Programming 14(1), 25–42 (1990). https://doi.org/10.1016/0167-6423(90)90056-J

  61. Stark, I.: Free-algebra models for the \(\pi \)-calculus. Theoretical Computer Science 390(2), 248–270 (2008). https://doi.org/10.1016/j.tcs.2007.09.024

  62. Staton, S.: Instances of computational effects: An algebraic perspective. In: 2013 28th Annual ACM/IEEE Symposium on Logic in Computer Science. pp. 519–519 (2013). https://doi.org/10.1109/LICS.2013.58

  63. Steele, Jr., G.L.: Building interpreters by composing monads. In: Boehm, H., Lang, B., Yellin, D.M. (eds.) Proceedings of the 21st ACM SIGPLAN-SIGACT symposium on Principles of programming languages. pp. 472–492. POPL ’94, ACM (1994). https://doi.org/10.1145/174675.178068

  64. Swierstra, W.: Data types à la carte. J. Funct. Program. 18(4), 423–436 (2008). https://doi.org/10.1017/S0956796808006758

  65. Takano, A., Meijer, E.: Shortcut deforestation in calculational form. In: Proceedings of the Seventh International Conference on Functional Programming Languages and Computer Architecture. Association for Computing Machinery, New York, NY, USA (1995). https://doi.org/10.1145/224164.224221

  66. Wadler, P.: Deforestation: Transforming programs to eliminate trees. Theor. Comput. Sci. 73(2), 231–248 (Jan 1988). https://doi.org/10.1016/0304-3975(90)90147-A

  67. Wadler, P.: Comprehending monads. In: Proceedings of the 1990 ACM Conference on LISP and Functional Programming. pp. 61–78. LFP ’90, ACM (1990). https://doi.org/10.1145/91556.91592

  68. Wadler, P.: Monads for functional programming. In: Advanced Functional Programming, First International Spring School on Advanced Functional Programming Techniques-Tutorial Text. p. 24–52. Springer-Verlag, Berlin, Heidelberg (1995). https://doi.org/10.5555/647698.734146

  69. Wu, N., Schrijvers, T.: Fusion for free. In: Hinze, R., Voigtländer, J. (eds.) Mathematics of Program Construction. pp. 302–322. Springer International Publishing, Cham (2015). https://doi.org/978-3-319-19797-5_15

  70. Wu, N., Schrijvers, T., Hinze, R.: Effect handlers in scope. In: Proceedings of the 2014 ACM SIGPLAN Symposium on Haskell. p. 1–12. Haskell ’14, Association for Computing Machinery, New York, NY, USA (2014). https://doi.org/10.1145/2633357.2633358

  71. Yang, Z., Paviotti, M., Wu, N., van den Berg, B., Schrijvers, T.: Structured handling of scoped effects: Artefacts (Jan 2022). https://doi.org/10.5281/zenodo.5914133

  72. Yang, Z., Paviotti, M., Wu, N., van den Berg, B., Schrijvers, T.: Structured handling of scoped effects: Extended version (2022), https://arxiv.org/abs/2201.10287

  73. Yang, Z., Wu, N.: Reasoning about effect interaction by fusion. Proc. ACM Program. Lang. 5(ICFP) (Aug 2021). https://doi.org/10.1145/3473578

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Zhixuan Yang .

Editor information

Editors and Affiliations

Rights and permissions

Open Access This chapter is licensed under the terms of the Creative Commons Attribution 4.0 International License (http://creativecommons.org/licenses/by/4.0/), which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons license and indicate if changes were made.

The images or other third party material in this chapter are included in the chapter's Creative Commons license, unless indicated otherwise in a credit line to the material. If material is not included in the chapter's Creative Commons license and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder.

Reprints and Permissions

Copyright information

© 2022 The Author(s)

About this paper

Verify currency and authenticity via CrossMark

Cite this paper

Yang, Z., Paviotti, M., Wu, N., van den Berg, B., Schrijvers, T. (2022). Structured Handling of Scoped Effects. In: Sergey, I. (eds) Programming Languages and Systems. ESOP 2022. Lecture Notes in Computer Science, vol 13240. Springer, Cham. https://doi.org/10.1007/978-3-030-99336-8_17

Download citation

  • DOI: https://doi.org/10.1007/978-3-030-99336-8_17

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-030-99335-1

  • Online ISBN: 978-3-030-99336-8

  • eBook Packages: Computer ScienceComputer Science (R0)