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 程序员必须小心, 当他们写出循环函数时, 要留在所谓的 _ latter- recurive_ 碎片中, 使用不消耗堆叠空间的 _tail_ 调用。 这个纪律是计算堆堆叠空间的一个困难来源。 开始者必须学习循环, 然后尾盘再翻。 专家对写“ List. mapp ” 的“ 正确” 方式有异议。 直接版本必须漂亮但不是尾盘, 所以它会崩溃的转换过程会( L) 比直接版本要慢一些, 专家可能想要避免所有成本。 有些图书馆会提出可怕的执行方法, 但是会滚动的代码, 来弥补这种损失。 在一般情况下, 尾盘翻转程序运行的功能需要手动程序执行。

0
下载
关闭预览

相关内容

TMC:IEEE Transactions on Mobile Computing。 Explanation:移动计算中的IEEE事务。 Publisher:IEEE。 SIT: http://dblp.uni-trier.de/db/journals/tmc/
【AAAI2021】面向交通需求预测的耦合层图卷积
专知会员服务
43+阅读 · 2021年1月31日
斯坦福2020硬课《分布式算法与优化》
专知会员服务
117+阅读 · 2020年5月6日
开源书:PyTorch深度学习起步
专知会员服务
49+阅读 · 2019年10月11日
[综述]深度学习下的场景文本检测与识别
专知会员服务
77+阅读 · 2019年10月10日
机器学习入门的经验与建议
专知会员服务
90+阅读 · 2019年10月10日
知识图谱本体结构构建论文合集
专知会员服务
102+阅读 · 2019年10月9日
意识是一种数学模式
CreateAMind
3+阅读 · 2019年6月24日
已删除
将门创投
3+阅读 · 2019年1月15日
A Technical Overview of AI & ML in 2018 & Trends for 2019
待字闺中
16+阅读 · 2018年12月24日
disentangled-representation-papers
CreateAMind
26+阅读 · 2018年9月12日
VIP会员
相关VIP内容
【AAAI2021】面向交通需求预测的耦合层图卷积
专知会员服务
43+阅读 · 2021年1月31日
斯坦福2020硬课《分布式算法与优化》
专知会员服务
117+阅读 · 2020年5月6日
开源书:PyTorch深度学习起步
专知会员服务
49+阅读 · 2019年10月11日
[综述]深度学习下的场景文本检测与识别
专知会员服务
77+阅读 · 2019年10月10日
机器学习入门的经验与建议
专知会员服务
90+阅读 · 2019年10月10日
知识图谱本体结构构建论文合集
专知会员服务
102+阅读 · 2019年10月9日
相关资讯
意识是一种数学模式
CreateAMind
3+阅读 · 2019年6月24日
已删除
将门创投
3+阅读 · 2019年1月15日
A Technical Overview of AI & ML in 2018 & Trends for 2019
待字闺中
16+阅读 · 2018年12月24日
disentangled-representation-papers
CreateAMind
26+阅读 · 2018年9月12日
Top
微信扫码咨询专知VIP会员