Tail Modulo Cons
Fr\'ed\'eric Bour, Basile Cl\'ement, Gabriel Scherer

TL;DR
This paper introduces Tail Modulo Cons (TMC), a program transformation for OCaml that enables writing recursive functions like `List.map` in a clear, correct, and efficient manner without manual tail-recursion optimization.
Contribution
The paper presents TMC, a novel transformation that rewrites certain non-tail-recursive functions into destination-passing style, improving both correctness and performance.
Findings
TMC transforms a class of recursive functions into efficient, tail-recursive-like code.
TMC enables writing recursive functions that are both elegant and performant.
The implementation supports common list functions like `List.map`, `List.filter`, and `List.append`.
Abstract
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…
Peer Reviews
No public reviews on file for this paper yet. If you reviewed it on a platform where reviews are public (OpenReview, ICLR, NeurIPS, ICML), you can paste yours below so the community can read it here.
Videos
No videos yet. Explain this paper in a talk, walkthrough, or lecture? Add one.
Taxonomy
TopicsLogic, programming, and type systems · Scientific Computing and Data Management · Parallel Computing and Optimization Techniques
