Skip to content

E`<`<rrors in recursive default argument references

8 messages · Stavros Macrakis, Wacek Kusnierczyk, Peter Dalgaard +1 more

#
Tested in: R version 2.8.1 (2008-12-22) / Windows

Recursive default argument references normally give nice clear errors.
 In the first set of examples, you get the error:

  Error in ... :
  promise already under evaluation: recursive default argument
reference or earlier problems?

  (function(a = a) a      ) ()
  (function(a = a) c(a)   ) ()
  (function(a = a) a[1]   ) ()
  (function(a = a) a[[1]] ) ()
  (function(a = a) a$x    ) ()
  (function(a = a) mean(a) )   ()
  (function(a = a) sort(a) ) ()
  (function(a = a) as.list(a) ) ()

But in the following examples, R seems not to detect the 'promise
already under evaluation' condition and instead gets a stack overflow,
with the error message:

  Error: C stack usage is too close to the limit

  (function(a = a)  (a)    ) ()
  (function(a = a)  -a     ) ()
  (function(a = a) var(a) ) ()
  (function(a = a) sum(a) ) ()
  (function(a = a) is.vector(a) ) ()
  (function(a = a) as.numeric(a) ) ()

I don't understand why the two sets of examples behave differently.

            -s
#
Stavros Macrakis wrote:
when i run these examples, the execution seems to get into an endless
loop with no error messages whatsoever.  how much time does it take
before you get the error?  (using r 2.8.0 and also the latest r-devel).

vQ
btw. ?'-' talks about '-' as a *binary* operator, but the only example
given there which uses '-' uses it as a *unary* operator.  since '-'()
complains that '-' takes 1 or 2 arguments, it might be a good idea to
acknowledge it in the man page.
a bug in excel?

vQ
#
On Mon, Mar 9, 2009 at 5:40 PM, Wacek Kusnierczyk
<Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
In 2.8.1/Windows (32 bit), they return immediately, though the
stack-overflow case is about 13x slower.
user  system elapsed
   0.67    0.00    0.67
user  system elapsed
   0.05    0.00    0.05

Are you running under 64 bits?  How long does a vanilla infinite
recursion take to fail on your machine?  I get:
user  system elapsed
   0.27    0.00    0.26

         -s
#
Stavros Macrakis wrote:
this won't stop within a minute, running at 100% cpu, have to kill the
process.
user  system elapsed
  0.064   0.000   0.067
32 bits ubuntu 8.04
user  system elapsed
  0.204   0.008   0.212


vQ

PS i'm looking into the sources; in src/main/eval.c:317 there is a check
for whether the promise has been seen:

    if(PRSEEN(e))

it appears that when i evaluate

    (function(a=a) -a)()

the check is not passed (the condition is false -- promise not seen). 
seems like the evaluation goes into a loop before the promise is marked
as seen.  (or i misunderstand the code, likely.)

vQ
#
Stavros Macrakis wrote:
Ouch!!!

This shouldn't happen, I'm pretty sure. In particular not the apparently 
unstoppable loop under Linux. Thanks for pointing it out.
1 day later
#
Looks like an infinite recursion in R_isMissing, which I think may be
turned into an infinite loop if the C compiler is doing tail call
optimization.  I need to understand why this is written the way it is
and also why another case that I would expect to also have this
problem does not before identifying the appropriate fix.

luke
On Tue, 10 Mar 2009, Peter Dalgaard wrote:

            

  
    
#
Thanks to Stavros for the report.  This should now be fixed in R-devel.

luke
On Wed, 11 Mar 2009, luke at stat.uiowa.edu wrote:

            

  
    
#
luke at stat.uiowa.edu wrote:
indeed, though i find some of the error messages strange:

    (function(a=a) -a)()
    # Error in (function(a = a) -a)() :
    #  element 1 is empty;
    #   the part of the args list of '-' being evaluated was:
    #   (a)

    (function(a=a) c(a))()
    # Error in (function(a = a) c(a))() :
    #   promise already under evaluation: recursive default argument
reference or earlier problems?

why are they different?

vQ