Tail recursive functions allow for a wider range of optimisations than general recursive functions. For this reason, much research has gone into the transformation and optimisation of this family of functions, in particular those written in continuation passing style (CPS). Though the CPS transformation, capable of transforming any recursive function to an equivalent tail recursive one, is deeply problematic in the context of reversible programming (as it relies on troublesome features such as higher-order functions), we argue that relaxing (local) reversibility to (global) invertibility drastically improves the situation. On this basis, we present an algorithm for tail recursion conversion specifically for invertible functions. The key insight is that functions introduced by program transformations that preserve invertibility, need only be invertible in the context in which the functions subject of transformation calls them. We show how a bespoke data type, corresponding to such a context, can be used to transform invertible recursive functions into a pair of tail recursive function acting on this context, in a way where calls are highlighted, and from which a tail recursive inverse can be straightforwardly extracted.
翻译:尾递归函数相比一般递归函数能够支持更广泛的优化。为此,大量研究致力于此类函数的变换与优化,特别是采用续延传递风格(CPS)编写的函数。尽管能够将任意递归函数转化为等价尾递归函数的CPS变换在可逆编程语境中存在深层问题(因其依赖于高阶函数等棘手特性),但我们认为将(局部)可逆性放宽为(全局)可逆性可大幅改善这一状况。基于此,我们提出了一种专门针对可逆函数的尾递归转换算法。其核心洞见在于:通过程序变换引入的函数(该变换保持可逆性)仅需在变换目标函数的调用上下文中保持可逆。我们展示了一种定制数据类型(对应此类上下文)的用法,可将可逆递归函数转化为作用于该上下文的一对尾递归函数——在这种转化中,函数调用得以凸显,并可以此直接提取出尾递归逆函数。