Hostname: page-component-89b8bd64d-z2ts4 Total loading time: 0 Render date: 2026-05-09T13:02:05.984Z Has data issue: false hasContentIssue false

Tail recursion modulo context: An equational approach (extended version)

Part of: POPL 23

Published online by Cambridge University Press:  24 October 2025

DAAN LEIJEN
Affiliation:
Microsoft Research, Redmond, WA 98052, USA (e-mail: daan@microsoft.com)
ANTON FELIX LORENZEN
Affiliation:
University of Edinburgh, Edinburgh EH8 9YL, UK (e-mail: anton.lorenzen@ed.ac.uk)
Rights & Permissions [Opens in a new window]

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 context (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 instantiations not so commonly associated with TRMC, such as defunctionalized evaluation contexts, monoids, semirings, exponents, and fields. 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 nonlinear control (e.g., 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 nonlinear 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.

Information

Type
Research Article
Creative Commons
Creative Common License - CCCreative Common License - BY
This is an Open Access article, distributed under the terms of the Creative Commons Attribution licence (https://creativecommons.org/licenses/by/4.0/), which permits unrestricted re-use, distribution and reproduction, provided the original article is properly cited.
Copyright
© The Author(s), 2025. Published by Cambridge University Press
Figure 0

Fig. 1. Syntax and operational semantics.

Figure 1

Fig. 2. Calculated algorithm for general selective tail recursion modulo context transformation. It is parameterized by the ($\star$) condition, the composition ($\bullet$), and application (app) operations.

Figure 2

Fig. 3. Implementing constructor composition and application in the runtime system (for languages without precise reference counts).

Figure 3

Fig. 4. Benchmarks on Ubuntu 20.04 (AMD 5950x), Koka v2.4.1-dev. The benchmarks are map over a list (map), map over a tree (tmap), balanced red-black tree insertion (rbtree), and the knapsack program that uses nonlinear control flow. Each workload is scaled to process the same number of total elements (usually 100 000 000). The tested variants are TRMC (trmc), the standard non-tail-recursive style (std), accumulator style (acc), accumulator style without Perceus reuse (acc (no reuse)), and finally CPS style (cps).

Figure 4

Fig. A1. Benchmarks on Ubuntu 20.04 (AMD 5950x), Koka v2.4.1-dev, OCaml 4.14.0. The benchmark repeatedly maps the increment function over a list of a given size and sums the result list. Each workload is scaled to process the same number of total elements (100 000 000). The tested variants of $\texttt{map}$ are TRMC (trmc), accumulator style (acc), the standard non-tail-recursive style (std), and finally CPS style (cps).

Figure 5

Fig. B1. Minamide’s type system adapted to our language

Submit a response

Discussions

No Discussions have been published for this article.