Tail Modulo Cons

02/19/2021
by   Frédéric Bour, et al.
0

OCaml function calls consume space on the system stack. Operating systems set default limits on the stack space which are much lower than the available memory. If a program runs out of stack space, they get the dreaded "Stack Overflow" exception – they crash. As a result, OCaml programmers have to be careful, when they write recursive functions, to remain in the so-called _tail-recursive_ fragment, using _tail_ calls that do not consume stack space. This discipline is a source of difficulties for both beginners and experts. Beginners have to be taught recursion, and then tail-recursion. Experts disagree on the "right" way to write `List.map`. The direct version is beautiful but not tail-recursive, so it crashes on larger inputs. The naive tail-recursive transformation is (slightly) slower than the direct version, and experts may want to avoid that cost. Some libraries propose horrible implementations, unrolling code by hand, to compensate for this performance loss. In general, tail-recursion requires the programmer to manually perform sophisticated program transformations. In this work we propose an implementation of "Tail Modulo Cons" (TMC) for OCaml. TMC is a program transformation for a fragment of non-tail-recursive functions, that rewrites them in _destination-passing style_. The supported fragment is smaller than other approaches such as continuation-passing-style, but the performance of the transformed code is on par with the direct, non-tail-recursive version. Many useful functions that traverse a recursive datastructure and rebuild another recursive structure are in the TMC fragment, in particular `List.map` (and `List.filter`, `List.append`, etc.). Finally those functions can be written in a way that is beautiful, correct on all inputs, and efficient.

READ FULL TEXT

page 1

page 2

page 3

page 4

research
02/20/2023

Tail recursion transformation for invertible functions

Tail recursive functions allow for a wider range of optimisations than g...
research
01/27/2020

Operationally-based Program Equivalence Proofs using LCTRSs

We propose an operationally-based deductive proof method for program equ...
research
02/28/2022

Getting There and Back Again

"There and Back Again" (TABA) is a programming pattern where the recursi...
research
10/03/2022

Exact Recursive Probabilistic Programming

Recursive calls over recursive data are widely useful for generating pro...
research
03/17/2016

An Implementation and Analysis of a Kernel Network Stack in Go with the CSP Style

Modern operating system kernels are written in lower-level languages suc...
research
07/28/2022

Regression verification of unbalanced recursive functions with multiple calls (long version)

Given two programs p_1 and p_2, typically two versions of the same progr...
research
02/22/2019

On Transforming Functions Accessing Global Variables into Logically Constrained Term Rewriting Systems

In this paper, we show a new approach to transformations of an imperativ...

Please sign up or login with your details

Forgot password? Click here to reset