8

I've wanted to avoid blowing the stack while creating a system similar to function advice or method combination. This involves tree traversal (in my implementation), conditional recursion, etc. One of the very few methods available for converting recursion into loops is trampolining. I've tried this and then found out that I need to implement eg. short-circuit boolean expression evaluation. In short, I've implemented a combination of a trampoline with continuations, and now I'm trying to find out whether this construction exists and what its name may be - as I've been unable to find any such already existing construct.

My implementation - bounce evaluation with manual stack handling:

function immediate($bounce, $args)
{
    $stack = array($bounce->run($args));

    while ($stack[0] instanceof Bounce) {
        $current = array_pop($stack);
        if ($current instanceof Bounce) {
            $stack[] = $current;
            $stack[] = $current->current();
        } else {
            $next = array_pop($stack);
            $stack[] = $next->next($current);
        }
    }

    return $stack[0];
}

The Bounce class:

class Bounce
{
    protected $current;
    protected $next;

    public function __construct($current, $next)
    {
        $this->current = $current;
        $this->next = $next;
    }

    public function current()
    {
        $fn = $this->current;
        return $fn();
    }

    public function next($arg)
    {
        $fn = $this->next;
        return $fn($arg);
    }
}

And, as an example, short-circuit AND implementation (ie. in JavaScript first(args) && second(args)). $first and $second are functions that may return Bounces as well.

return new Bounce(
    function () use ($first, $args) {
        return $first($args);
    },
    function ($return) use ($second, $args) {
        if (!$return) {
            return $return;
        }
        return new Bounce(
            function () use ($second, $args) {
                return $second($args);
            },
            null
        );
    }
);

This allows for general recursion, with overhead of approximately 3 function calls per normal function call (though in the general case for variable iteration count, it is quite cumbersome to write and requires 'lazy recursion').

Has anyone seen such a structure before?

snawster
  • 152
  • 3
  • 9
  • Trampolines work well for linear data structures, but for trees or other recrusive processes you need a way to sequence or flatten them, declaring the particular order in which things are processed. [Continuation-Passing Style](https://en.wikipedia.org/wiki/Continuation-passing_style) offers a technique to do just that. – Mulan Nov 20 '21 at 17:28
  • In this question, [_How to adapt trampolines to continuation-passing style?_](https://stackoverflow.com/q/57733363/633183), I offer a detailed 3-part answer that shows you how to make any recursive program stack-safe in any language that supports first-class functions, such as JavaScript, Python, PHP, or many others. If interested, see [Part 1](https://stackoverflow.com/a/57739457/633183), [Part 2](https://stackoverflow.com/a/57743504/633183), [Part 3](https://stackoverflow.com/a/57813405/633183). – Mulan Nov 20 '21 at 17:29
  • To answer your question, I can't say I've seen a name for the particular structure, but you essentially implemented you own [evaluation strategy](https://en.wikipedia.org/wiki/Evaluation_strategy) for your program. Utilities like `Bounce` and `immediate` are changing the order and process of the emerging program. This is basically the foundation of specifying and implementing behaviour of your own language. – Mulan Nov 20 '21 at 17:32

0 Answers0