Search code examples
tail-call-optimizationats

Compiler Friendly Tail Recursion + Tail Recursion Checking in ATS


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?


Solution

  • 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!