Questions tagged [trampolines]

A technique to emulate (or augment) jumps / function calls by a custom dispatch. A single trampoline is sufficient to express all control transfers of a program.

A technique to emulate (or augment) jumps / function calls by a custom dispatch to converted functions which return control back to the dispatch (a.k.a. the trampoline) instead of making the function calls themselves, using data to control the control flow.

A single trampoline is sufficient to express all control transfers of a program; a program so expressed is trampolined or in "trampolined style"; converting a program to trampolined style is trampolining. Trampolined functions can be used to implement tail recursive function calls in stack-oriented languages.

See Wikipedia.

71 questions
6
votes
2 answers

Why does return/redo evaluate result functions in the calling context, but block results are not evaluated?

Last night I learned about the /redo option for when you return from a function. It lets you return another function, which is then invoked at the calling site and reinvokes the evaluator from the same position >> foo: func [a] [(print a)…
4
votes
1 answer

Stack safety of trampoline when the interpreter loop itself is recursive

I'm implementing a Trampoline in Python, in order to write recursive functions with stack safety (since CPython does not feature TCO). It looks like this: from typing import Generic, TypeVar from abc import ABC, abstractmethod A = TypeVar('A',…
3
votes
0 answers

Basic Hook vs Trampoline Hook

I've been learning about hooking functions of processes which has been fun and I have managed to hook a process by doing the following: Create jump to my own code in the function I've hooked Jump to my own code Jump back to the instruction after my…
Kryton
  • 95
  • 1
  • 5
3
votes
1 answer

Tail recursion elimination on conditional types doesn't work

In TS 4.5 tail call optimization was added for recursive generics. The following snippet computes Fibonacci numbers (in unary) up to F12, but for F13 it fails with the usual "Type instantiation is excessively deep and possibly infinite" exception.…
3
votes
1 answer

Groovy's trampoline() makes recursive execution much slower - why?

I'm experimenting with recursion: def fac //fac = { int curr, res = 1G -> 1 >= curr ? res : fac( curr - 1, res * curr ) } fac = { int curr, res = 1G -> 1 >= curr ? res : fac.trampoline( curr - 1, res * curr ) } fac = fac.trampoline() def rnd = new…
injecteer
  • 20,038
  • 4
  • 45
  • 89
3
votes
1 answer

Stack-safe mutual recursion without leaking implementation details on the call side

I generalized clojure's loop/recur trampoline so that it works with indirect recursion: const trampoline = f => (...args) => { let acc = f(...args); while (acc && acc.type === recur) { let [f, ...args_] = acc.args; acc =…
user5536315
3
votes
1 answer

Continuation passing style vs aggressively trimmed call stack?

I'm considering something like CPS for use in an interpreter for an actor based language. The function arguments are passed in an array of variants, and the continuation returned in the same array, so an simple function def add (x,y) => x + y so a…
Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
3
votes
1 answer

Why nested FlatMaps could blow up the stack in Scala?

I am learning the Trampoline trick in Scala by reading this paper Stackless Scala with Free Monad, by Rúnar Bjarnason. But I got stuck in section 4.3 "An easy thing to get wrong". One thing makes me confused is how a f(x) could directly trigger…
3
votes
0 answers

Emacs: magit status failed on ssh remote server repo

I'm using ssh to connect to a remote server. On the server there is a git repo called MRFLSSVM. However, when I execute magit-status on: /ssh:qmServer:/home/Chang/qmCodeLab/MRFLSSVM/ Magit asks me to Create repository in…
spacegoing
  • 5,056
  • 5
  • 25
  • 42
3
votes
0 answers

Trampolining in JavaScript - how to handle try/catch in recursive call

I have a recursive function written in Javascript, which is giving RangeError because of excessive deep recursion. Hence, I have used trampolining to make it tail-optimized. This has wrapped the function call in a while loop and I have gotten rid of…
kajarigd
  • 1,299
  • 3
  • 28
  • 46
3
votes
1 answer

Scala: expanded syntax of trampolining function breaks tail recursion

This is an exercise in "Functional Programming in Scala," chapter 13, to implement a trampoline for interpreting tail-recursive functions. runTrampoline2 is not tail-recursive, and overflows the stack with my test inputs. Furthermore, the tailrec…
Peter Becich
  • 989
  • 3
  • 14
  • 30
3
votes
2 answers

Difference between "trampoline" methods

I've started learning JS recently, and this is my first dive into functional language territory. Was doing the "trampoline" exercise in the functional-javascript-workshop npm module, and came across an interesting difference between my own solution…
Joel Gallant
  • 315
  • 4
  • 21
3
votes
3 answers

How do I jump out of a function in Lisp?

Is it possible in (Common) Lisp to jump to another function instead of call another? I mean, that the current function is broken and another is called, without jumping back through thousands of functions, as if I'd decide myself if tail call…
2
votes
2 answers

MonoTouch SIGABRT "Ran out of trampolines of type 2" error

I get a SIGABRT / ran out of trampolines error when running my MonoTouch app on a native device (iPad). This happens pretty early in my app - I'm constructing a section using MonoTouch.Dialog. ItemTypeRadio = new RootElement ("Type", new…
Omri Gazitt
  • 3,428
  • 2
  • 21
  • 27
2
votes
1 answer

How to write analyzer/evaluator functions like `eval-if` in CPS form?

I'm trying to write a toy python Scheme interpreter based on the meta-circular evaluator in SICP. Since python only supports a call stack of limited depth, I have to eliminate the tail calls. I read about trampolines and implemented the parser with…
pjhades
  • 1,948
  • 2
  • 19
  • 34