From berwin at maths.uwa.edu.au  Sat Mar 14 12:24:34 2009
From: berwin at maths.uwa.edu.au (Berwin A Turlach)
Date: Sat, 14 Mar 2009 19:24:34 +0800
Subject: [Rd] surprising behaviour of names<-
In-Reply-To: <49BB4D2A.40806@idi.ntnu.no>
References: <49B67CB1.8060005@idi.ntnu.no> <49B682F9.8050307@biostat.ku.dk>
	<49B6E6D9.7010809@idi.ntnu.no>
	<96A4BB14-D6EA-44B1-B524-6DB139F3A7F8@r-project.org>
	<8CCAAAD3-919F-462F-A616-F4FDDB46D975@r-project.org>
	<49B81186.4070107@idi.ntnu.no>
	<20090312154008.56706f38@berwin-nus1>
	<49B8D060.8090206@idi.ntnu.no>
	<20090312173231.7a2929df@berwin-nus1>
	<49B8DB8F.5030704@idi.ntnu.no> <20090312213508.719afa73@berwin5>
	<49B91A7E.50107@idi.ntnu.no> <20090312231240.6a061f0b@berwin5>
	<49B96FE7.2060705@idi.ntnu.no>
	<20090313120309.7acb13e5@berwin-nus1>
	<49BA38EB.3000004@idi.ntnu.no> <20090313234755.6f6e08c9@berwin5>
	<49BAA8E6.3080407@idi.ntnu.no> <20090314122034.696f6c33@absentia>
	<49BB4D2A.40806@idi.ntnu.no>
Message-ID: <20090314192434.6f21600b@absentia>

On Sat, 14 Mar 2009 07:22:34 +0100
Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:

[...]
> > Well, I don't see any new object created in my workspace after
> > 	x <- 4
> > 	names(x) <- "foo"
> > Do you?
> >   
> 
> of course not.  that's why i'd say the two above are *not*
> equivalent. 
> 
> i haven't noticed the 'in the c code';  do you mean the r interpreter
> actually generates, in the c code, such r expressions for itself to
> evaluate?

As I said before, I have little knowledge about how the parser works and
what goes on under the hood; and I have also little time and
inclination to learn about it.  

But if you are interested in these details, then by all means invest
the time to investigate.

Alternatively, you would hope that Simon eventually finishes the book
that he is writing on programming in R; as I understand it, that book
would explain part of these issues in details.  Hopefully, along with
the book he makes the tools that he has for introspection available.

> >> i guess you have looked under the hood;  point me to the relevant
> >> code.     
> >
> > No I did not, because I am not interested in knowing such intimate
> > details of R, but it seems you were interested.
> >   
> 
> yes, but then your claim about what happens under the hood, in the c
> code, is a pure stipulation.  

I made no claim about what is going on under the hood because I have no
knowledge about these matters.  But, yes, I was speculating of what
might go on.

> and you got the example from the r language definition sec. 10.2,
> which says the forms are equivalent, with no 'under the hood, in the
> c code' comment.

Trying to figure out what a writer/painter actually means/says beyond
the explicitly stated/painted, something that is summed up in Australia
(and other places) under the term "critical thinking", was not high in
the curriculum of your school, was it? :-)

> you're just showing that your statements cannot be taken seriously.

Usually, my statement can be taken seriously, unless followed by some
indication that I said them tongue-in-cheek.  Of course, statements
that I allegedly made but were in fact put into my mouth cannot, and
should not, be taken seriously.

> >> yes, *if* you are able to predict the refcount of the object
> >> passed to 'names<-' *then* you can predict what 'names<-' will do,
> >> [...] 
> >
> > I think Simon pointed already out that you seem to have a wrong
> > picture of what is going on.  [...]
>
> so what you quote effectively talks about a specific refcount
> mechanism.  it's not refcount that would be used by the garbage
> collector, but it's a refcount, or maybe refflag.

Fair enough, if you call this a refcount then there is no problem.
Whenever I came across the term refcount in my readings, it was
referring to different mechanisms, typically mechanisms that kept exact
track on how often an object was referred too.  So I would not call the
value of the named field a refcount.  And we can agree to call it from
now on a refcount as long as we realise what mechanism is really used.
 
> yes, that's my opinion:  the effects of implementation tricks should
> not be observable by the user, because they can lead to hard to
> explain and debug behaviour in the user's program.  you surely don't
> suggest that all users consult the source code before writing
> programs in r.

Indeed, I am not suggesting this.  Only users who use/rely on
features that are not sufficiently documented would have to study the
source code to find out what the exact behaviour is.  But, of course,
this could be fraught with danger since the behaviour could change
without warning.

> i have indeed learned what prefix 'names<-' does and now i know that
> the surprising behaviour is due to the observability of the internal
> optimization.
> 
> thanks to simon, peter, and you for your answers which allowed me to
> learn this ugly detail.

You are welcome.

Cheers,

	Berwin



[Rd] surprising behaviour of names<-
Berwin A Turlach