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.
- Andrew W. Appel. 1991. Compiling with Continuations. Cambridge University Press. https://doi.org/10.1017/CBO9780511609619
Google Scholar
Cross Ref
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- Keith L Clark and Sten-Åke Tärnlund. 1977. A First Order Theory of Data and Programs.. In IFIP Congress. 939–944.
Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Cross Ref
- Stephen Dolan, Leo White, KC Sivaramakrishnan, Jeremy Yallop, and Anil Madhavapeddy. 2015. Effective concurrency through algebraic effects. In OCaml Workshop.
Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
- Jeroen Engels. 2022. Tail recursion, but modulo cons. https://jfmengels.net/modulo-cons/ Accessed: 2022-06-06.
Google Scholar
- 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 Scholar
- Jeremy Gibbons. 2022. Continuation-Passing Style, Defunctionalization, Accumulations, and Associativity. The Art, Science, and Engineering of Programming, 6 (2022), 11, Article 7.
Google Scholar
- 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 Scholar
- J. Hindley and Jonathan Seldin. 1986. Introduction to Combinators and Lambda-Calculus.. https://doi.org/10.1017/CBO9780511809835
Google Scholar
Cross Ref
- Gérard P. Huet. 1997. The Zipper. Journal of Functional Programming, 7, 5 (1997), 549–554. https://doi.org/10.1017/S0956796897002864
Google Scholar
Digital Library
- 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 Scholar
Digital Library
- Graham Hutton. 2021. It’s as easy as 1, 2, 3. Jan., https://www.cs.nott.ac.uk/~pszgmh/123.pdf Unpublished draft.
Google Scholar
- 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 Scholar
Cross Ref
- 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 Scholar
Digital Library
- Daan Leijen. 2021. The Koka Language. https://koka-lang.github.io
Google Scholar
- Daan Leijen and Anton Lorenzen. 2022. Tail Recursion Modulo Context – An Equational Approach. Microsoft Research.
Google Scholar
- 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 Scholar
Digital Library
- Lambert Meertens. 1986. Algorithmics: Towards Programming as a Mathematical Activity. In Mathematics and Computer Science. 289–334.
Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- Chris Okasaki. 1999. Purely Functional Data Structures. Colombia University. isbn:9780521663502
Google Scholar
Digital Library
- 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 Scholar
Cross Ref
- 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 Scholar
Digital Library
- François Pottier and Nadji Gauthier. 2004. Polymorphic Typed Defunctionalization. 89–98. isbn:158113729X https://doi.org/10.1145/964001.964009
Google Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- 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 Scholar
Cross Ref
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- Mitchell Wand. 1980. Continuation-Based Program Transformation Strategies. J. ACM, 27, 1 (1980), Jan., 164–180. https://doi.org/10.1145/322169.322183
Google Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
Index Terms
Tail Recursion Modulo Context: An Equational Approach
Recommendations
Termination modulo combinations of equational theories
FroCoS'09: Proceedings of the 7th international conference on Frontiers of combining systemsRewriting with rules R modulo axioms E is a widely used technique in both rule-based programming languages and in automated deduction. Termination methods for rewriting systems modulo specific axioms E (e.g., associativity-commutativity) are known. ...
Theorem proving modulo based on Boolean equational procedures
RelMiCS'08/AKA'08: Proceedings of the 10th international conference on Relational and kleene algebra methods in computer science, and 5th international conference on Applications of kleene algebraDeduction with inference rules modulo computation rules plays an important role in automated deduction as an effective method for scaling up. We present four equational theories that are isomorphic to the traditional Boolean theory and show that each of ...
Constrained narrowing for conditional equational theories modulo axioms
For an unconditional equational theory ( , E ) whose oriented equations E are confluent and terminating, narrowing provides an E-unification algorithm. This has been generalized by various authors in two directions: (i) by considering unconditional ...






Comments