skip to main content
research-article
Open Access

Tail Recursion Modulo Context: An Equational Approach

Published:11 January 2023Publication History
Skip Abstract Section

Abstract

The tail-recursion modulo cons transformation can rewrite functions that are not quite tail-recursive into a tail-recursive form that can be executed efficiently. In this article we generalize tail recursion modulo cons (TRMc) to modulo contexts (TRMC), and calculate a general TRMC algorithm from its specification. We can instantiate our general algorithm by providing an implementation of application and composition on abstract contexts, and showing that our context laws_ hold. We provide some known instantiations of TRMC, namely modulo evaluation contexts (CPS), and associative operations, and further instantiantions not so commonly associated with TRMC, such as defunctionalized evaluation contexts, monoids, semirings, exponents, and cons products. We study the modulo cons instantiation in particular and prove that an instantiation using Minamide’s hole calculus is sound. We also calculate a second instantiation in terms of the Perceus heap semantics to precisely reason about the soundness of in-place update. While all previous approaches to TRMc fail in the presence of non-linear control (for example induced by call/cc, shift/reset or algebraic effect handlers), we can elegantly extend the heap semantics to a hybrid approach which dynamically adapts to non-linear control flow. We have a full implementation of hybrid TRMc in the Koka language and our benchmark shows the TRMc transformed functions are always as fast or faster than using manual alternatives.

References

  1. Andrew W. Appel. 1991. Compiling with Continuations. Cambridge University Press. https://doi.org/10.1017/CBO9780511609619 Google ScholarGoogle ScholarCross RefCross Ref
  2. Jeffrey M. Bell, Françoise Bellegarde, and James Hook. 1997. Type-Driven Defunctionalization. 25–37. isbn:0897919181 https://doi.org/10.1145/258948.258953 Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Richard S. Bird. 1984. The Promotion and Accumulation Strategies in Transformational Programming. ACM Transactions on Programming Languages and Systems, 6, 4 (1984), Oct., 487–504. https://doi.org/10.1145/1780.1781 Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Joshua Bloch. 2008. Effective Java (2nd Edition) (The Java Series) (2 ed.). Prentice Hall PTR, USA. isbn:0321356683 https://doi.org/10.5555/1377533 Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Frédéric Bour, Basile Clément, and Gabriel Scherer. 2021. Tail Modulo Cons. Journeées Francophones des Langages Applicatifs (JFLA), April, https://hal.inria.fr/hal-03146495/document hal-03146495. Google ScholarGoogle Scholar
  6. Keith L Clark and Sten-Åke Tärnlund. 1977. A First Order Theory of Data and Programs.. In IFIP Congress. 939–944. Google ScholarGoogle Scholar
  7. Olivier Danvy and Andrzej Filinski. 1990. Abstracting Control. In Proceedings of the 1990 ACM Conference on LISP and Functional Programming (LFP ’90). 151–160. isbn:0-89791-368-X https://doi.org/10.1145/91556.91622 Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Olivier Danvy and Mayer Goldberg. 2002. There and Back Again. In Proceedings of the Seventh ACM SIGPLAN International Conference on Functional Programming (ICFP ’02). 230–234. https://doi.org/10.1145/581478.581500 Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. Olivier Danvy and Lasse R. Nielsen. 2001. Defunctionalization at Work. In Proceedings of the 3rd ACM SIGPLAN International Conference on Principles and Practice of Declarative Programming (PPDP ’01). 162–174. isbn:158113388X https://doi.org/10.1145/773184.773202 Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Klaus Didrich, Andreas Fett, Carola Gerke, Wolfgang Grieskamp, and Peter Pepper. 1994. OPAL: Design and implementation of an algebraic programming language. In Programming Languages and System Architectures. Springer, 228–244. isbn:978-3-540-48356-4 https://doi.org/10.1007/3-540-57840-4_34 Google ScholarGoogle ScholarCross RefCross Ref
  11. Stephen Dolan, Leo White, KC Sivaramakrishnan, Jeremy Yallop, and Anil Madhavapeddy. 2015. Effective concurrency through algebraic effects. In OCaml Workshop. Google ScholarGoogle Scholar
  12. Damien Doligez and Xavier Leroy. 1993. A Concurrent, Generational Garbage Collector for a Multithreaded Implementation of ML. In Proceedings of the 20th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’93). Association for Computing Machinery, New York, NY, USA. 113–123. isbn:0897915607 https://doi.org/10.1145/158511.158611 Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Zdeněk Dvořák. 2004. Declarative world inspiration. In GCC Developers’ Summit. 25. See also https://github.com/gcc-mirror/gcc/blob/master/gcc/tree-tailcall.cc Google ScholarGoogle Scholar
  14. Jeroen Engels. 2022. Tail recursion, but modulo cons. https://jfmengels.net/modulo-cons/ Accessed: 2022-06-06. Google ScholarGoogle Scholar
  15. Daniel P. Friedman and David S. Wise. 1975. Unwinding Stylized Recursion into Iterations. Bloomingdale, Indiana. https://legacy.cs.indiana.edu/ftp/techreports/TR19.pdf Google ScholarGoogle Scholar
  16. Jeremy Gibbons. 2022. Continuation-Passing Style, Defunctionalization, Accumulations, and Associativity. The Art, Science, and Engineering of Programming, 6 (2022), 11, Article 7. Google ScholarGoogle Scholar
  17. Robert Harper. 2012. 15-150 Equational Reasoning Guide. https://www.cs.cmu.edu/~15150/previous-semesters/2012-spring/resources/handouts/equational.pdf Notes for the 15-150 CMU Functional Programming course. Google ScholarGoogle Scholar
  18. J. Hindley and Jonathan Seldin. 1986. Introduction to Combinators and Lambda-Calculus.. https://doi.org/10.1017/CBO9780511809835 Google ScholarGoogle ScholarCross RefCross Ref
  19. Gérard P. Huet. 1997. The Zipper. Journal of Functional Programming, 7, 5 (1997), 549–554. https://doi.org/10.1017/S0956796897002864 Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. R John Muir Hughes. 1986. A novel representation of lists and its application to the function “reverse”. Information processing letters, 22, 3 (1986), 141–144. https://doi.org/10.1016/0020-0190(86)90059-1 Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. Graham Hutton. 2021. It’s as easy as 1, 2, 3. Jan., https://www.cs.nott.ac.uk/~pszgmh/123.pdf Unpublished draft. Google ScholarGoogle Scholar
  22. Daan Leijen. 2014. Koka: Programming with Row Polymorphic Effect Types. In MSFP’14, 5th workshop on Mathematically Structured Functional Programming. https://doi.org/10.4204/EPTCS.153.8 Google ScholarGoogle ScholarCross RefCross Ref
  23. Daan Leijen. 2017. Type Directed Compilation of Row-typed Algebraic Effects. In Proc. of the 44th ACM SIGPLAN Symp. on Principles of Programming Languages (POPL’17). 486–499. isbn:978-1-4503-4660-3 https://doi.org/10.1145/3009837.3009872 Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Daan Leijen. 2021. The Koka Language. https://koka-lang.github.io Google ScholarGoogle Scholar
  25. Daan Leijen and Anton Lorenzen. 2022. Tail Recursion Modulo Context – An Equational Approach. Microsoft Research. Google ScholarGoogle Scholar
  26. Anton Lorenzen and Daan Leijen. 2022. Reference Counting with Frame Limited Reuse. Proc. ACM Program. Lang., 6, ICFP, Article 103, 24 pages. https://doi.org/10.1145/3547634 Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. Lambert Meertens. 1986. Algorithmics: Towards Programming as a Mathematical Activity. In Mathematics and Computer Science. 289–334. Google ScholarGoogle Scholar
  28. Yasuhiko Minamide. 1998. A Functional Representation of Data Structures with a Hole. In Proceedings of the 25th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’98). 75–84. https://doi.org/10.1145/268946.268953 Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. Yasuhiko Minamide, Greg Morrisett, and Robert Harper. 1996. Typed Closure Conversion. In Proceedings of the 23rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’96). Association for Computing Machinery, New York, NY, USA. 271–283. isbn:0897917693 https://doi.org/10.1145/237721.237791 Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. Alexandre Moine, Arthur Charguéraud, and François Pottier. 2023. A High-Level Separation Logic for Heap Space under Garbage Collection (Extended Version). In Proceedings of the 50th ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages (POPL ’23). 1–27. Google ScholarGoogle Scholar
  31. Chris Okasaki. 1999. Purely Functional Data Structures. Colombia University. isbn:9780521663502 Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. Gordon D. Plotkin and John Power. 2003. Algebraic Operations and Generic Effects. Applied Categorical Structures, 11, 1 (2003), 69–94. https://doi.org/10.1023/A:1023064908962 Google ScholarGoogle ScholarCross RefCross Ref
  33. Gordon D. Plotkin and Matija Pretnar. 2009. Handlers of Algebraic Effects. In 18th European Symposium on Programming Languages and Systems (ESOP’09). 80–94. https://doi.org/10.1007/978-3-642-00590-9_7 Google ScholarGoogle ScholarDigital LibraryDigital Library
  34. François Pottier and Nadji Gauthier. 2004. Polymorphic Typed Defunctionalization. 89–98. isbn:158113729X https://doi.org/10.1145/964001.964009 Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. François Pottier and Jonathan Protzenko. 2013. Programming with Permissions in Mezzo. In Proceedings of the 18th ACM SIGPLAN International Conference on Functional Programming (ICFP ’13). ACM, 173–184. https://doi.org/10.1145/2500365.2500598 Google ScholarGoogle ScholarDigital LibraryDigital Library
  36. Reinking ḩar 44 Xie, de Moura, and Leijen. 2021. Perceus: Garbage Free Reference Counting with Reuse. In Proceedings of the 42nd ACM SIGPLAN International Conference on Programming Language Design and Implementation (PLDI 2021). ACM, New York, NY, USA. 96–111. https://doi.org/10.1145/3453483.3454032 Google ScholarGoogle ScholarDigital LibraryDigital Library
  37. John C. Reynolds. 1972. Definitional Interpreters for Higher-Order Programming Languages. In Proceedings of the ACM Annual Conference - Volume 2. ACM, 717–740. https://doi.org/10.1145/800194.805852 Google ScholarGoogle ScholarDigital LibraryDigital Library
  38. John C. Reynolds. 2002. Separation Logic: A Logic for Shared Mutable Data Structures. In Proceedings of the 17th Annual IEEE Symposium on Logic in Computer Science (LICS ’02). IEEE Computer Society, USA. 55–74. isbn:0769514839 Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. Tore Risch. 1973. REMREC - A program for Automatic Recursion Removal. Inst. för Informationsbehandling, Uppsala Universitet. https://user.it.uu.se/~torer/publ/remrec.pdf Google ScholarGoogle Scholar
  40. Wolfram Schulte and Wolfgang Grieskamp. 1992. Generating efficient portable code for a strict applicative language. In Declarative Programming, Sasbachwalden 1991. Springer, 239–252. https://doi.org/10.1007/978-1-4471-3794-8_16 Google ScholarGoogle ScholarCross RefCross Ref
  41. Chung-chieh Shan. 2007. A static simulation of dynamic delimited control. Higher-Order and Symbolic Computation, 20, 4 (2007), 371–401. issn:1573-0557 https://doi.org/10.1007/s10990-007-9010-4 Google ScholarGoogle ScholarDigital LibraryDigital Library
  42. Dorai Sitaram and Matthias Felleisen. 1990. Control delimiters and their hierarchies. LISP and Symbolic Computation, 3, 1 (1990), 67–99. https://doi.org/10.1007/BF01806126 Google ScholarGoogle ScholarDigital LibraryDigital Library
  43. Jonathan Sobel and Daniel P. Friedman. 1998. Recycling Continuations. In Proc. of the Third ACM SIGPLAN Int. Conf. on Functional Programming (ICFP ’98). 251–260. https://doi.org/10.1145/289423.289452 Google ScholarGoogle ScholarDigital LibraryDigital Library
  44. Andrew Tolmach and Dino P Oliva. 1998. From ML to Ada: Strongly-typed language interoperability via source translation. Journal of Functional Programming, 8, 4 (1998), 367–412. https://doi.org/10.1017/S0956796898003086 Google ScholarGoogle ScholarDigital LibraryDigital Library
  45. Sebastian Ullrich and Leonardo de Moura. 2019. Counting Immutable Beans – Reference counting optimized for purely functional programming. In Proceedings of the 31st symposium on Implementation and Application of Functional Languages (IFL’19). Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. Philip Wadler. 1984. Listlessness is Better than Laziness: Lazy Evaluation and Garbage Collection at Compile-Time. In Proceedings of the 1984 ACM Symposium on LISP and Functional Programming (LFP ’84). Association for Computing Machinery, New York, NY, USA. 45–52. isbn:0897911423 https://doi.org/10.1145/800055.802020 Google ScholarGoogle ScholarDigital LibraryDigital Library
  47. Mitchell Wand. 1980. Continuation-Based Program Transformation Strategies. J. ACM, 27, 1 (1980), Jan., 164–180. https://doi.org/10.1145/322169.322183 Google ScholarGoogle ScholarDigital LibraryDigital Library
  48. Mitchell Wand and Daniel P. Friedman. 1978. Compiling Lambda-Expressions Using Continuations and Factorizations. Comput. Lang., 3, 4 (1978), jan, 241–263. issn:0096-0551 https://doi.org/10.1016/0096-0551(78)90042-5 Google ScholarGoogle ScholarDigital LibraryDigital Library
  49. Andrew K. Wright and Matthias Felleisen. 1994. A syntactic approach to type soundness. Inf. Comput., 115, 1 (1994), Nov., 38–94. https://doi.org/10.1006/inco.1994.1093 Google ScholarGoogle ScholarDigital LibraryDigital Library
  50. Nicolas Wu, Tom Schrijvers, and Ralf Hinze. 2014. Effect Handlers in Scope. In Proceedings of the 2014 ACM SIGPLAN Symposium on Haskell (Haskell ’14). 1–12. isbn:978-1-4503-3041-1 https://doi.org/10.1145/2633357.2633358 Google ScholarGoogle ScholarDigital LibraryDigital Library
  51. Ningning Xie and Daan Leijen. 2020. Effect Handlers in Haskell, Evidently. In Proceedings of the 13th ACM SIGPLAN International Symposium on Haskell (Haskell 2020). Association for Computing Machinery, New York, NY, USA. 95–108. isbn:9781450380508 https://doi.org/10.1145/3406088.3409022 Google ScholarGoogle ScholarDigital LibraryDigital Library
  52. Ningning Xie and Daan Leijen. 2021. Generalized Evidence Passing for Effect Handlers: Efficient Compilation of Effect Handlers to C. Proc. ACM Program. Lang., 5, ICFP, Article 71, 30 pages. https://doi.org/10.1145/3473576 Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Tail Recursion Modulo Context: An Equational Approach

        Recommendations

        Comments

        Login options

        Check if you have access through your login credentials or your institution to get full access on this article.

        Sign in

        Full Access

        • Article Metrics

          • Downloads (Last 12 months)270
          • Downloads (Last 6 weeks)36

          Other Metrics

        PDF Format

        View or Download as a PDF file.

        PDF

        eReader

        View online with eReader.

        eReader
        About Cookies On This Site

        We use cookies to ensure that we give you the best experience on our website.

        Learn more

        Got it!