Skip to content
Prev 363272 / 398503 Next

Continuation-parsing / trampoline / infinite recursion problem

?
Ok, I think maybe I am beginning to see what is going wrong...

Explicitly remembering the thunk parameters in a list works fine, as far as I can see.

make_thunk <- function(f, ...) {
? remembered <- list(...)
? function(...) do.call(f, as.list(remembered))
}

thunk_factorial <- function(n, continuation = identity) {
? if (n == 1) {
? ? continuation(1)
? } else {
? ? new_continuation <- function(result) {
? ? ? make_thunk(continuation, n * result)
? ? }
? ? make_thunk(thunk_factorial, n - 1, new_continuation)
? }
}

trampoline <- function(thunk) {
? while (is.function(thunk)) thunk <- thunk()
? thunk
}

trampoline(thunk_factorial(100))


But if I delay the evaluation of the parameters to thunk I get an error

make_thunk <- function(f, ...) {
? remembered <- eval(substitute(alist(...))) # not evaluating parameters yet
? function(...) do.call(f, as.list(remembered))
}

thunk_factorial <- function(n, continuation = identity) {
? if (n == 1) {
? ? continuation(1)
? } else {
? ? new_continuation <- function(result) {
? ? ? make_thunk(continuation, n * result)
? ? }
? ? make_thunk(thunk_factorial, n - 1, new_continuation)
? }
}

trampoline(thunk_factorial(100))

Running this version I am told, when applying the function, that it doesn?t see variable `n`.


As far as I can see, the thunk remembers the parameters just fine. At least this gives me the parameters I made it remember

x <- 1
f <- make_thunk(list, a = 1 * x, b = 2 * x)
g <- make_thunk(list, c = 3 * x)
f()
g()

Here I just get the parameters back in a list because the wrapped function is `list`. (The reason I have `x` as a global variable and use it in the arguments is so I get call objects that needs to be evaluated lazily instead of just values).

These values contain the expressions I gave the `make_thunk` function, of course, and they are not evaluated. So in the factorial function the missing `n` is because I give it the expression `n - 1` that it of course cannot evaluate in the thunk.

So I cannot really delay evaluation.

Does this sound roughly correct?

Now why I can still get it to work when I call `cat` remains a mystery?

Cheers
	Thomas
On 10 August 2016 at 19:12:41, Thomas Mailund (mailund at birc.au.dk(mailto:mailund at birc.au.dk)) wrote:

            

Thread (18 messages)

Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 9 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 10 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 10 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 10 Duncan Murdoch Continuation-parsing / trampoline / infinite recursion problem Aug 10 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 10 Bert Gunter Continuation-parsing / trampoline / infinite recursion problem Aug 10 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 10 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 10 Duncan Murdoch Continuation-parsing / trampoline / infinite recursion problem Aug 10 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 10 William Dunlap Continuation-parsing / trampoline / infinite recursion problem Aug 10 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 10 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 10 Duncan Murdoch Continuation-parsing / trampoline / infinite recursion problem Aug 10 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 10 Duncan Murdoch Continuation-parsing / trampoline / infinite recursion problem Aug 11 Thomas Mailund Continuation-parsing / trampoline / infinite recursion problem Aug 11