Why can't tail calls be optimized in JVM-based Lisps?

后端 未结 3 2006
情书的邮戳
情书的邮戳 2020-12-08 08:15

Main question: I view the most significant application of tail call optimization (TCO) as a translation of a recursive call into a loop (in cases in which the recursive call

相关标签:
3条回答
  • 2020-12-08 08:41

    In the final presentation of ClojureConj 2014, Brian Goetz pointed out there is a security feature in the JVM that prevents stack frame collapsing (as that would be an attack vector for people looking to make a function go somewhere else on return).

    https://www.youtube.com/watch?v=2y5Pv4yN0b0&index=21&list=PLZdCLR02grLoc322bYirANEso3mmzvCiI

    0 讨论(0)
  • 2020-12-08 08:43

    Real TCO works for arbitrary calls in tail position, not just self calls, so that code like the following does not cause a stack overflow:

    (letfn [(e? [x] (or (zero? x) (o? (dec x))))
            (o? [x] (e? (dec x)))]
      (e? 10))
    

    Clearly you'd need JVM support for this, since programs running on the JVM cannot manipulate the call stack. (Unless you were willing to establish your own calling convention and impose the associated overhead on function calls; Clojure aims to use regular JVM method calls.)

    As for eliminating self calls in tail position, that's a simpler problem which can be solved as long as the entire function body gets compiled to a single JVM method. That is a limiting promise to make, however. Besides, recur is fairly well liked for its explicitness.

    0 讨论(0)
  • 2020-12-08 08:54

    There is a reason why the JVM does not support TCO: Why does the JVM still not support tail-call optimization?

    However there is a way around this by abusing the heap memory and some trickery explained in the A First-Order One-Pass CPS Transformation paper; it is implemented in Clojure by Chris Frisz and Daniel P. Friedman (see clojure-tco).

    Now Rich Hickey could have chosen to do such an optimization by default, Scala does this at some points. Instead he chose to rely on the end user to specify the cases where they can be optimized by Clojure with the trampoline or loop-recur constructs. The decision has been explained here: https://groups.google.com/d/msg/clojure/4bSdsbperNE/tXdcmbiv4g0J

    0 讨论(0)
提交回复
热议问题