Thomas Mailund
2016-Aug-10 16:53 UTC
[R] Continuation-parsing / trampoline / infinite recursion problem
> On 10 Aug 2016, at 13:56, Thomas Mailund <mailund at birc.au.dk> wrote: > > make_thunk <- function(f, ...) f(...)Doh! It is of course this one: make_thunk <- function(f, ...) function() f(?) It just binds a function call into a thunk so I can delay its evaluation. Sorry Thomas
Thomas Mailund
2016-Aug-10 16:59 UTC
[R] Continuation-parsing / trampoline / infinite recursion problem
An alternative implementation, closer to what I need when I have more than one recursion in each step, but still using factorial as the example, is this one: thunk_factorial <- function(n, continuation = identity) { force(continuation) # if I remove this line I get an error cat("call: ", n, "\n") # same for this line if (n == 1) { continuation(1) } else { new_continuation <- function(result) { cat("thunk: ", result, "\n?) # remove this line and it fails, keep it and it works make_thunk(continuation, n * result) } make_thunk(thunk_factorial, n - 1, new_continuation) } } trampoline(thunk_factorial(10000)) Here I am making a continuation instead of passing along an accumulator, which I need to do for more complex cases, and with that continuation I can also get it to complete without errors if I output the text inside it. Removing the `cat` line and I get the recursion error? Cheers Thomas> On 10 Aug 2016, at 18:53, Thomas Mailund <mailund at birc.au.dk> wrote: > > >> On 10 Aug 2016, at 13:56, Thomas Mailund <mailund at birc.au.dk> wrote: >> >> make_thunk <- function(f, ...) f(...) > > Doh! It is of course this one: > > make_thunk <- function(f, ...) function() f(?) > > It just binds a function call into a thunk so I can delay its evaluation. > > Sorry > Thomas > > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Duncan Murdoch
2016-Aug-10 17:04 UTC
[R] Continuation-parsing / trampoline / infinite recursion problem
On 10/08/2016 12:53 PM, Thomas Mailund wrote:> > On 10 Aug 2016, at 13:56, Thomas Mailund <mailund at birc.au.dk> wrote: > > > > make_thunk <- function(f, ...) f(...) > > Doh! It is of course this one: > > make_thunk <- function(f, ...) function() f(?) > > It just binds a function call into a thunk so I can delay its evaluation.I haven't looked closely at the full set of functions, but this comment: force(continuation) # if I remove this line I get an error makes it sound as though you're being caught by lazy evaluation. The "make_thunk" doesn't appear to evaluate ..., so its value can change between the time you make the thunk and the time you evaluate it. I think you could force the evaluation within make_thunk by changing it to make_thunk <- function(f, ...) { list(...); function() f(?) } and then would be able to skip the force() in your thunk_factorial function. Duncan Murdoch
Thomas Mailund
2016-Aug-10 17:10 UTC
[R] Continuation-parsing / trampoline / infinite recursion problem
That did the trick! I was so focused on not evaluating the continuation that I completely forgot that the thunk could hold an unevaluated value? now it seems to be working for all the various implementations I have been playing around with. I think I still need to wrap my head around *why* the forced evaluation is necessary there, but I will figure that out when my tired brain has had a little rest. Thanks a lot! Thomas> On 10 Aug 2016, at 19:04, Duncan Murdoch <murdoch.duncan at gmail.com> wrote: > > On 10/08/2016 12:53 PM, Thomas Mailund wrote: >> > On 10 Aug 2016, at 13:56, Thomas Mailund <mailund at birc.au.dk> wrote: >> > >> > make_thunk <- function(f, ...) f(...) >> >> Doh! It is of course this one: >> >> make_thunk <- function(f, ...) function() f(?) >> >> It just binds a function call into a thunk so I can delay its evaluation. > > I haven't looked closely at the full set of functions, but this comment: > > force(continuation) # if I remove this line I get an error > > makes it sound as though you're being caught by lazy evaluation. The "make_thunk" doesn't appear to evaluate ..., so its value can change between the time you make the thunk and the time you evaluate it. I think you could force the evaluation within make_thunk by changing it to > > make_thunk <- function(f, ...) { list(...); function() f(?) } > > and then would be able to skip the force() in your thunk_factorial function. > > Duncan Murdoch > >
Bert Gunter
2016-Aug-10 17:15 UTC
[R] Continuation-parsing / trampoline / infinite recursion problem
make_thunk is probably unnecessary and apparently problematic. I think you could use do.call() instead, as do.call(f,list(...)) . -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Aug 10, 2016 at 9:53 AM, Thomas Mailund <mailund at birc.au.dk> wrote:> >> On 10 Aug 2016, at 13:56, Thomas Mailund <mailund at birc.au.dk> wrote: >> >> make_thunk <- function(f, ...) f(...) > > Doh! It is of course this one: > > make_thunk <- function(f, ...) function() f(?) > > It just binds a function call into a thunk so I can delay its evaluation. > > Sorry > Thomas > > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Thomas Mailund
2016-Aug-10 17:20 UTC
[R] Continuation-parsing / trampoline / infinite recursion problem
On 10 Aug 2016, at 19:15, Bert Gunter <bgunter.4567 at gmail.com<mailto:bgunter.4567 at gmail.com>> wrote: make_thunk is probably unnecessary and apparently problematic. I think you could use do.call() instead, as do.call(f,list(...)) . Yes, make_thunk <- function(f, ...) function() do.call(f, list(...)) also works as far as I can see, yes. I do need to turn it into a thunk, though, as far as I can see. Thanks Thomas [[alternative HTML version deleted]]
William Dunlap
2016-Aug-10 17:48 UTC
[R] Continuation-parsing / trampoline / infinite recursion problem
You may gain some understanding of what is going on by adding the output of sys.nframe() or length(sys.calls()) to the cat() statement. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Aug 10, 2016 at 9:59 AM, Thomas Mailund <mailund at birc.au.dk> wrote:> An alternative implementation, closer to what I need when I have more than > one recursion in each step, but still using factorial as the example, is > this one: > > thunk_factorial <- function(n, continuation = identity) { > force(continuation) # if I remove this line I get an error > cat("call: ", n, "\n") # same for this line > if (n == 1) { > continuation(1) > } else { > new_continuation <- function(result) { > cat("thunk: ", result, "\n?) # remove this line and it fails, keep > it and it works > make_thunk(continuation, n * result) > } > make_thunk(thunk_factorial, n - 1, new_continuation) > } > } > trampoline(thunk_factorial(10000)) > > Here I am making a continuation instead of passing along an accumulator, > which I need to do for more complex cases, and with that continuation I can > also get it to complete without errors if I output the text inside it. > Removing the `cat` line and I get the recursion error? > > Cheers > Thomas > > > > > On 10 Aug 2016, at 18:53, Thomas Mailund <mailund at birc.au.dk> wrote: > > > > > >> On 10 Aug 2016, at 13:56, Thomas Mailund <mailund at birc.au.dk> wrote: > >> > >> make_thunk <- function(f, ...) f(...) > > > > Doh! It is of course this one: > > > > make_thunk <- function(f, ...) function() f(?) > > > > It just binds a function call into a thunk so I can delay its evaluation. > > > > Sorry > > Thomas > > > > > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]