Advertisement

Fusion for Free

Efficient Algebraic Effect Handlers
  • Nicolas Wu
  • Tom SchrijversEmail author
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 9129)

Abstract

Algebraic effect handlers are a recently popular approach for modelling side-effects that separates the syntax and semantics of effectful operations. The shape of syntax is captured by functors, and free monads over these functors denote syntax trees. The semantics is captured by algebras, and effect handlers pass these over the syntax trees to interpret them into a semantic domain.

This approach is inherently modular: different functors can be composed to make trees with richer structure. Such trees are interpreted by applying several handlers in sequence, each removing the syntactic constructs it recognizes. Unfortunately, the construction and traversal of intermediate trees is painfully inefficient and has hindered the adoption of the handler approach.

This paper explains how a sequence of handlers can be fused into one, so that multiple tree traversals can be reduced to a single one and no intermediate trees need to be allocated. At the heart of this optimization is keeping the notion of a free monad abstract, thus enabling a change of representation that opens up the possibility of fusion. We demonstrate how the ensuing code can be inlined at compile time to produce efficient handlers.

Keywords

Syntax Tree Effect Handler Abstract Syntax Tree Intermediate Tree Term Algebra 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.

Notes

Acknowledgments

The authors would like to thank Ralf Hinze for suggesting that they consider the specification that free monads satisfy in terms of adjunctions, and to James McKinna who helped baptize \( con \) for constructing terms. We are also grateful for the feedback from the members of the IFIP Working Group 2.1 on an early iteration of this work. This work has been partially funded by the Flemish Fund for Scientific Research (FWO).

References

  1. 1.
    Atkey, R., Johann, P., Ghani, N., Jacobs, B.: Interleaving data and effects (2012) (Submitted for publication)Google Scholar
  2. 2.
    Burris, S., Sankappanavar, H.P.: A course in universal algebra, graduate texts in mathematics, vol. 78. Springer, New York (1981)CrossRefGoogle Scholar
  3. 3.
    Chin, W.N.: Safe fusion of functional expressions ii: Further improvements. J. Funct. Program. 4, 515–555 (1994)CrossRefGoogle Scholar
  4. 4.
    Gill, A., Launchbury, J., Peyton Jones, S.L.: A short cut to deforestation. In: Proceedings of the Conference on Functional Programming Languages and Computer Architecture, FPCA 1993, pp. 223–232. ACM, New York, NY, USA (1993)Google Scholar
  5. 5.
    Harper, T.: A library writer’s guide to shortcut fusion. In: Proceedings of the 4th ACM Symposium on Haskell, Haskell 2011, pp. 47–58. ACM, New York, NY, USA (2011)Google Scholar
  6. 6.
    Hinze, R., Harper, T., James, D.W.H.: Theory and practice of fusion. In: Hage, J., Morazán, M.T. (eds.) IFL. LNCS, vol. 6647, pp. 19–37. Springer, Heidelberg (2011) CrossRefGoogle Scholar
  7. 7.
    Hinze, R., Wu, N., Gibbons, J.: Conjugate hylomorphisms - the mother of all structured recursion schemes. In: Proceedings of the 42nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2015, pp. 527–538. ACM, New York, NY, USA (2015)Google Scholar
  8. 8.
    Hyland, M., Plotkin, G.D., Power, J.: Combining effects: sum and tensor. Theor. Comput. Sci. 357(1–3), 70–99 (2006)zbMATHMathSciNetCrossRefGoogle Scholar
  9. 9.
    Kammar, O., Lindley, S., Oury, N.: Handlers in action. In: Proceedings of the 18th ACM SIGPLAN International Conference on Functional programming, ICFP 2014, pp. 145–158. ACM (2013)Google Scholar
  10. 10.
    Kiselyov, O., Sabry, A., Swords, C.: Extensible effects: an alternative to monad transformers. In: Proceedings of the 2013 ACM SIGPLAN Symposium on Haskell, Haskell 2013, pp. 59–70. ACM (2013)Google Scholar
  11. 11.
    Liang, S., Hudak, P., Jones, M.: Monad transformers and modular interpreters. In: Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 1995, pp. 333–343. ACM, New York, NY, USA (1995)Google Scholar
  12. 12.
    Ploeg, A.V.D., Kiselyov, O.: Reflection without remorse: revealing a hidden sequence to speed up monadic reflection. In: Proceedings of the 2014 ACM SIGPLAN Symposium on Haskell, Haskell 2014, pp. 133–144. ACM, New York, NY, USA (2014)Google Scholar
  13. 13.
    Plotkin, G.D., Matija, P.: Handling algebraic effects. Log. Meth. Comput. Sci. 9(4), 1–36 (2013)CrossRefGoogle Scholar
  14. 14.
    Plotkin, G., Power, J.: Notions of computation determine monads. In: Nielsen, M., Engberg, U. (eds.) FOSSACS 2002. LNCS, vol. 2303, pp. 342–356. Springer, Heidelberg (2002) CrossRefGoogle Scholar
  15. 15.
    Plotkin, G.D., Power, J.: Algebraic operations and generic effects. Appl. Categorical Struct. 11(1), 69–94 (2003)zbMATHMathSciNetCrossRefGoogle Scholar
  16. 16.
    Rivas, E., Jaskelioff, M.: Notions of computation as monoids. CoRR abs/1406.4823. (2014). http://arxiv.org/abs/1406.4823
  17. 17.
    Schrijvers, T., Wu, N., Desouter, B., Demoen, B.: Heuristics entwined with handlers combined. In: Proceedings of the 16th International Symposium on Principles and Practice of Declarative Programming, PPDP 2014, pp. 259–270. ACM (2014)Google Scholar
  18. 18.
    Swierstra, W.: Data types à la carte. J. Funct. Program. 18(4), 423–436 (2008)zbMATHMathSciNetCrossRefGoogle Scholar
  19. 19.
    Takano, A., Meijer, E.: Shortcut deforestation in calculational form. In: Proceedings of the Seventh International Conference on Functional Programming Languages and Computer Architecture, FPCA 1995, pp. 306–313. ACM, New York, NY, USA (1995)Google Scholar
  20. 20.
    Voigtländer, J.: Proving correctness via free theorems: the case of the destroy/build-rule. In: Proceedings of the 2008 ACM SIGPLAN Symposium on Partial Evaluation and Semantics-based Program Manipulation, PEPM 2008, pp. 13–20. ACM, New York, NY, USA (2008)Google Scholar
  21. 21.
    Voigtländer, J.: Free theorems involving type constructor classes: functional pearl. In: Proceedings of the 14th ACM SIGPLAN International Conference on Functional Programming, ICFP 2009, pp. 173–184. ACM, New York, NY, USA (2009)Google Scholar
  22. 22.
    Wadler, P.: Theorems for free!. In: Proceedings of the Fourth International Conference on Functional Programming Languages and Computer Architecture, FPCA 1989, pp. 347–359. ACM, New York, NY, USA (1989)Google Scholar
  23. 23.
    Wadler, P.: Deforestation: transforming programs to eliminate trees. Theor. Comput. Sci. 73(2), 231–248 (1990)zbMATHMathSciNetCrossRefGoogle Scholar
  24. 24.
    Wu, N., Schrijvers, T., Hinze, R.: Effect handlers in scope. In: Proceedings of the 2014 ACM SIGPLAN Symposium on Haskell, Haskell 2014, pp. 1–12. ACM, New York, NY, USA (2014)Google Scholar

Copyright information

© Springer International Publishing Switzerland 2015

Authors and Affiliations

  1. 1.Department of Computer ScienceUniversity of BristolBristolUK
  2. 2.Department of Computer ScienceKU LeuvenLeuvenBelgium

Personalised recommendations