Skip to content

:Re: PROTECT and OCaml GC.

5 messages · Laurent Gautier, Guillaume Yziquel

#
It does not have to be a functional language.
To see it in use within a some-language-to-R bridge, you can check the
source in JRI, rpy2.
I can mostly speak for rpy2, and the way it is done there relies on both 
R and Python's GC. Creating a anonymous R object presented to the Python 
world is first "R_preserved" (from garbage collection from R) then using 
Python's reference counting mechanism, calling an "R_Release" whenever 
the Python wrapper is available for garbage collection (in fact there is 
a twist, but this is roughly the way it is working). In your case, 
you'll use the OCaml GC system (and "R_release" the R object when its 
OCaml representation is going for garbage collection).
Rpy2 can do a lot of that, and probably so can JRI.
Symbols are used in various places in R, checking the outcome of 
deparse(substitute()) on an anonymous variable of large size could tell 
you that some things will not work out of the box nicely.


HTH,


L.
#
Laurent Gautier a ?crit :
For now, garbage collection is a secondary issue.

I would have been interested in a binding to a functional language to 
see exactly where you get a closure to closure mapping. That's why I was 
asking since that's what I'm foremost interested in.
An anonymous closure to anonymous closure mapping? Could you point out 
where this is done exactly?
#
Guillaume Yziquel wrote:
Anonymous R objects, that is without an associated symbol in R, can be 
passed to functions (and in that way makes a binding "take hold of R 
objects without using symbols").
For example, building R code made of anonymous objects can be achieved 
by making a LANGSXP object and tweaking it.

Example in R itself:
 > x <- call("round", 2.3)
 > eval(x)
[1] 2
 > x[[1]] <- function(x) x^2
 > eval(x)
[1] 5.29

Getting more complex constructs may need a little more trickery.

...or do you mean something else ?


L.
#
Laurent Gautier a ?crit :
You're saying "anonymous R objects can be passed to functions". My 
question is:

can you construct a LANGSXP in C/Python/rpy from R objects and an 
anonymous R closure, the closure being available from C as a SEXP 
closure. Can you write a function

	SEXP my_langsxp (SEXP closure, SEXP arglist);

with only the API?
#
Guillaume Yziquel wrote:
If this can be done from R itself, it can be done from the C level.

I have not looked into making sure that this can precisely and 
effortlessly be done with rpy2, as ironing other parts of the bridge 
caught my attention and seemed more urgently needed, but I remember 
contemplating the idea of having it working at some point...
(I went as far as getting LANGSXP objects exposed as Python list-like 
objects, I think).