4

I've come to Idris from Scala. Scala has tail call optimization (TCO), and I can tell the compiler to balk if it can't optimize a recursive function using TCO. For example, see these posts.

This Scala function successfully counts String lengths

def allLengths(strs: List[String]): List[Int] = strs match {
  case Nil => Nil
  case x :: xs => x.length :: allLengths(xs)
}

but if I annotate it with annotation.tailrec, the compiler errors

error: could not optimize @tailrec annotated method zip: it contains a recursive call not in tail position

because the function doesn't directly return a call to allLengths. If I run it (without annotation) with a really long List, I get "ERROR: too much recursion", as expected.

I can, however, rewrite it as

@tailrec
def allLengths(strs: List[String], acc: List[Int] = Nil): List[Int] = strs match {
  case Nil => acc.reverse
  case x :: xs => allLengths(xs, x.length :: acc)
}

which compiles fine with the annotation. With @tailrec, Scala compiles the code by converting it to an imperative loop which isn't at risk of recursion errors. I believe it may also be faster as an imperative loop.

In Brady's Idris book, he uses the example

allLengths : List String -> List Nat
allLengths [] = []
allLengths (x :: xs) = length x :: allLengths xs

which can be compiled with a total annotation, and I can't seem to cause a recursion error (though allLengths (replicate 5000 "hi") is having difficulty). Having come from Scala, I was surprised that he doesn't write it in a tail recursive way. A few questions:

  • are runtime recursion errors possible in Idris for recursive functions that aren't tail recursive?
  • are tail recursive functions optimized in Idris during compilation? What about non-tail recursive?
  • is there an annotation like in Scala that ensures TCO? @tailrec feels very similar to total, but the former doesn't guarantee totality and the latter doesn't guarantee tail recursion

1 Answer 1

2

TCOs are mostly subjective to the corresponding runtime or the backend system for Idris code generation. They can take Idris IR, identify tail calls and choose to optimize them. As I've been working on the JVM backend for Idris, I can say that JVM backend for Idris does eliminate tail recursion and uses trampolines for non-self tail calls without any explicit annotation required from the user.

Here is how Idris 2 JVM backend handles the following tail recursive functions:

reverse : List a -> List a
reverse xs = go [] xs where
  go : List a -> List a -> List a
  go acc [] = acc
  go acc (x :: xs) = go (x :: acc) xs

allLengths : List Nat -> List String -> List Nat
allLengths acc [] = reverse acc
allLengths acc (x :: xs) = allLengths (length x :: acc) xs

Here both allLengths and go inside reverse are tail recursive and also note that allLengths calls reverse in tail position. Idris 2 JVM backend turns both of these functions into loops at the bytecode level but also trampolines other tail calls. This is how the decompiled bytecode would look like:

    // `go` function decompiled code
    public static Object Main$$nested1190$243$go(Object arg$0, Object arg$1, IdrisObject arg$2, IdrisObject arg$3) {
        while(true) {
            switch(arg$3.getConstructorId()) {
            case 0:
                return arg$2;
            case 1:
                Object e$2 = arg$3.getProperty(0);
                IdrisObject e$3 = (IdrisObject)Runtime.unwrap(arg$3.getProperty(1));
                arg$0 = null;
                arg$2 = (IdrisObject)(new col(1, Runtime.unwrap(e$2), arg$2));
                arg$3 = e$3;
                break;
            default:
                return Runtime.crash("Unreachable code");
            }
        }
    }

    // `reverse` function
    public static Thunk Main$reverse(Object arg$0, IdrisObject arg$1) {
        return () -> {
            return Runtime.createThunk(Main$$nested1190$243$go((Object)null, arg$1, (IdrisObject)(new Nil(0)), arg$1));
        };
    }

    // `allLengths` function
    public static Thunk Main$allLengths(IdrisObject arg$0, IdrisObject arg$1) {
        while(true) {
            switch(arg$1.getConstructorId()) {
            case 0:
                return () -> {
                    return Main$reverse((Object)null, arg$0);
                };
            case 1:
                String e$2 = (String)Runtime.unwrap(arg$1.getProperty(0));
                IdrisObject e$3 = (IdrisObject)Runtime.unwrap(arg$1.getProperty(1));
                arg$0 = (IdrisObject)(new col(1, Runtime.unwrap(Prelude.length(e$2)), arg$0));
                arg$1 = e$3;
                break;
            default:
                return Runtime.createThunk(Runtime.crash("Unreachable code"));
            }
        }
    }

We can see while loops for both go and allLengths and the tail recursive function calls are completely eliminated by just storing values to function arguments for next iteration of while loop. We can also see lambdas for trampoline thunks (reverse call in allLengths function) for other functions called in tail positions. Non-tail recursive functions are not transformed currently by the JVM backend so they can still exhaust the stack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.