当前位置: X-MOL 学术arXiv.cs.PL › 论文详情
Our official English website, www.x-mol.net, welcomes your feedback! (Note: you will need to create a separate account there.)
Tail Modulo Cons
arXiv - CS - Programming Languages Pub Date : 2021-02-19 , DOI: arxiv-2102.09823
Frédéric Bour, Basile Clément, Gabriel Scherer

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.

中文翻译:

尾模缺点

OCaml函数调用会占用系统堆栈上的空间。操作系统对堆栈空间设置默认限制,该限制比可用内存低得多。如果程序的堆栈空间不足,则会得到可怕的“堆栈溢出”异常-它们会崩溃。结果,OCaml程序员在编写递归函数时必须小心,以使用不占用堆栈空间的_tail_调用保留在所谓的_tail-recursive_片段中。对于初学者和专家而言,这门学科都是困难的根源。必须先教初学者递归,然后再进行尾递归。专家对“ List.map”的“正确”编写方式持不同意见。直接版本很漂亮,但不尾递归,因此在较大的输入上会崩溃。天真的尾递归转换(比)慢于直接版本,并且专家可能希望避免这种费用。一些库提出了可怕的实现,请手动展开代码以补偿这种性能损失。通常,尾递归需要程序员手动执行复杂的程序转换。在这项工作中,我们提出了OCaml的“尾部模态约束”(TMC)的实现。TMC是针对非尾递归函数片段的程序转换,它以_destination-passing style_重写它们。受支持的片段比其他方法(例如,连续传递样式)小,但转换后的代码的性能与直接的非尾递归版本相当。TMC片段中有许多有用的遍历递归数据结构并重建另一个递归结构的函数,尤其是List.map(和List.filter,List)。附加”等)。最后,可以以美观,在所有输入上正确且有效的方式编写这些功能。
更新日期:2021-02-22
down
wechat
bug