Has anyone written a formal paper describing a method to (automatically) convert functions to be tail recursive? I am looking for a university-level formal treatment including the limitations (types of functions that can be converted), procedures for conversion, and, if possible, proofs of correctness? Examples in Haskell would be a bonus.
Has anyone written a formal paper describing a method to (automatically) convert functions to
Share
So there are two parts to this:
Transforming recursion into tail recursion
It appears relatively straight forward to recognize tail recursive definitions syntactically. After all, ‘tail recursive’ just means that the call appears syntactically in the tail of the expression.
E.g. the Scheme folks describe:
Transforming functions into tail calls
The tricky part of your question is optimizations for identifying and transforming candidate recursive computations into tail recursive ones.
One reference is in GHC, which uses inlining and a wide array of simplification rules to collapse away recursive calls, until their underlying tail recursive structure remains.
Tail Call Elimination
Once you have your functions in a tail-recursive form, you’d like to have that implemented effiicently. If you can generate a loop, that’s a good start. If your target machine doesn’t, then the tail call elimination” will need a few tricks. To quote Odersky and Schinz, cited below,
Tail call elimination on the Java Virtual Machine, Michel Schinz Martin Odersky, 2001
Henry G. Baker, Jr. CONS should not CONS its arguments, part II: Cheney
on the M. T. A. Draft Version, January 1994.