Compiler Friendly Tail Recursion + Tail Recursion Checking in ATS

爱⌒轻易说出口 提交于 2019-12-23 18:11:01

问题


What is the best way to check that a function has been tail call optimized in ATS? (So far I have been running "top" to see if memory usage is constant)

As a follow up: Say you have a complex tail-recursive function that the compiler has failed to TCO, is there a way to re-write it in a more compiler friendly way? Or in such a way to force the compiler to attempt TCO?


回答1:


There is a peculiar way to do it in ATS2.

Say you have

fnx foo(...) = bar(...)
and bar(...) = ...bar...

If the body of bar contains a non-tail-recursive call to bar, then the C compiler is to complain with an error message.

Things become a lot more challenging when (linear) streams are involved. A seemingly non-tail-recursive function can run without concern of stack overflow because it essentially saves its stack on heap (and then frees it): This is a place where ATS really shines!



来源:https://stackoverflow.com/questions/49222337/compiler-friendly-tail-recursion-tail-recursion-checking-in-ats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!