Automatic Tail Recursion Optimisation

Implements meta-programming functions for automatically translating recursive functions into looping functions or trampolines.

Recursive functions are the natural way to express iterations in a functional programming langauge, but in R, they can be significantly slower than loop-versions and for moderately long sequences or moderately deep trees, recursive functions will reach a limit imposted on them by the stack limit.

There are known solutions to these problems, as long as functions are written to be tail-recursive, meaning that the return value of a function is either a base value or another recursive call, but where we do not call recursively to then do something with the result.

The goal of `tailr` is to automatically transform tail-recursive functions into loops or trampolines.

Installation

You can install the released version of `tailr` from CRAN using

You can install tailr from GitHub with:

Examples

Consider a classical recursive function, `factorial`:

(I know R already has a builtin factorial function, but please ignore that). This function will compute the factorial of `n`, but if `n` is too large, it will exceed the stack limit:

A classical way out of this problem is to turn it into a tail-recursive function:

R doesn’t implement the tail-recursion optimisation, though, so it doesn’t help us.

With `tailr` we can, automatically, translate a tail-recursive function into a looping one, essentially implementing the tail-recursion optimisation this way.

I have disabled byte compilation to make running time comparisons fair below; by default it is enabled. For a function as simple as `factorial`, though, byte compiling will not affect the running time in any substantial amount.

This version, because it looks instead of recurse, doesn’t have the stack limit problem:

We get the result `Inf` because the number we compute is too large to represent on the computer, but that is not the point of the example. The point is that the recursion doesn’t get too deep for the stack because we avoid recursion alltogether.

With something as simple as computing the factorial, it is easy to write a looping function by hand, and it will be much faster than both the (tail-)recursive and the transformed function:

The transformed version runs in about the same time as the recursive one, but the looping function is much faster.

However, consider a more complicated example. Using the `pmatch` package, we can create a linked list data structure as this:

A natural way to process linked lists using pattern matching is to write recursive functions that matches different patterns of their input. A function for computing the length of a linked list can look like this:

The function we generate is rather complicated

but, then, it is not one we want to manually inspect in any case.

It is not too hard to implement this function with a loop either, but it is not as simple as the recursive function:

If we compare the running time for these three functions, the transformed function is faster than the recursive but not as fast as the iterative:

FIXME: The main reason for this is that the running time is dominated by the cost of `pmatch`. If I manage to get that faster, the iterative function might end being much faster here as well.

More examples:

The module primarily solves the problem of exceeding the stack space. The transformed functions are not as fast as those we can code by hand using loops. It should be possible to improve on the running time of the transformed functions, however, with some program analysis… This analysis should be included in the time usage analysis, though, which will probably still come out saying that manually programmed looping versions are faster than transformed functions. Recursive functions can be a lot easier to read, though, than their corresponding looping versions, especially with pattern matching.

tailr 0.1.3

• Rewrote transformations to use the foolbox framework.
• Made changes for rlang 0.3.0 compatibility.
• Removed dependency on package pmatch I still use pmatch 0.1.5 in the README examples, but I have no other dependencies on the package.

tailr 0.1.2

• The transformation function do no longer use !!! and this seems to solve problems with CMD CHECK in downstream packages.
• the srcref attribute gets set (to the source of the input function) in loop_transform.

tailr 0.1.1

• Handles functions when they are local variables.

tailr 0.1.0

• Initial release.

Reference manual

install.packages("tailr")

0.1.3 by Thomas Mailund, 10 months ago

https://github.com/mailund/tailr

Report a bug at https://github.com/mailund/tailr/issues

Browse source code at https://github.com/cran/tailr

Authors: Thomas Mailund [aut, cre]

Documentation:   PDF Manual

GPL-3 license

Imports rlang, glue

Depends on foolbox

Suggests covr, testthat, microbenchmark, compiler, pmatch

See at CRAN