Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-08 Thread Alexander Burger
Hi Thorsten,

> Question on topic 2:
> given the C data type described below for an R "node", would it be somehow
> possible to extract just the SEXPTYPE from a SEXP return value, i.e.
> the first field of the sexpinfo_struct, that is the first struct in the
> SEXPREC_HEADER, that is the first field in the SEXPREC struct to which the
> SEXP return value points?

Given a pointer to the struct in 'P' (and if that integer is the first field in
the struct), you can get it with:

   (struct P 'I)

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe



Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-08 Thread Thorsten Jolitz
Picolisp has "native" calls as an FFI to call C shared libraries from
Picolisp, and build Picolisp wrapper functions on top of C functions
defined in header files.

A lot of programming languages allow that, there are e.g. many Python
wrappers for all kinds of data science libraries. But with Picolisp you
need only very little code to do it. It's still not easy when the C
functions are non-trivial (and you have only limited C knowledge).

There are quite a few Picolisp wrapper libs from AW on GitHub with good
explanations

Cesar Rabak  schrieb am Do., 8. Apr. 2021, 21:54:

> So, if I understand correctly we could use the introspective power of
> picolisp and write a {meta?}program to produce the "library" for Pil, and
> then use this to call R from picolisp?
>
> --
> Cesar Rabak
>
> On Thu, Apr 8, 2021 at 4:14 PM Thorsten Jolitz  wrote:
>
>> Hi Alex,
>> after digging deeper in R Internals I found out that there are actually C
>> access functions for the various subtypes of SEXP that can be wrapped with
>> native too:
>>
>> rinc: (setupRinC)
>> -> NIL
>> rinc: (evalQuietlyInR "V <- c(1.5, 3.4, 4.2)")
>> -> NIL
>> rinc: (evalInR "V")
>> -> 65007640
>> rinc: (REAL (evalInR "V"))
>> -> (2 3 4)
>> rinc: (teardownRinC)
>> -> NIL
>>
>> I think this is already quite nice.
>> Setup an R session, do quiet evaluations in R, do evaluations with return
>> value, convert that return value (SEXP subtype) to its appropriate Pil data
>> type, tear down R session.
>>
>> So there are only a few challenges left:
>> 1. write wrappers for all type conversion functions (for all SEXP
>> subtypes)
>> 2. figure out the SEXP subtype from the pointer returned
>> 3. dynamically get the length for all SEXP subtypes with a length, to be
>> able to dynamically define return types like '(1.0 . 3) in native or struct
>> (I think there are C functions for this in R internals too)
>>
>> Question on topic 2:
>> given the C data type described below for an R "node", would it be
>> somehow possible to extract just the SEXPTYPE from a SEXP return value, i.e.
>> the first field of the sexpinfo_struct, that is the first struct in the
>> SEXPREC_HEADER, that is the first field in the SEXPREC struct to which the
>> SEXP return value points?
>> Without specifying that complicated C structure in native or struct,
>> because we have the C access functions for that. Ignoring all the rest of
>> the pointer content, just extract that one field SEXPTYPE?
>>
>> Knowing which SEXPTYPE the return value represents, one could then read
>> the returned pointer with the appropriate data type into Pil (like REAL in
>> the example above).
>> if the return value is atomic, one is done. If its a vector, list, or so
>> one would need the length of it too, not only the type (or is it a valid
>> strategy to just use some very big value for the specification like '(1.0 .
>> ) or so)?
>>
>> Cheers
>> Thorsten
>>
>>
>> *Composition of an R node: *
>> the SEXP subtypes are defined like this:
>> typedef unsigned int SEXPTYPE;
>>   52   #define NILSXP›0›   /* nil = NULL */
>>   53   #define SYMSXP›1›   /* symbols */
>>   54   #define LISTSXP›   2›   /* lists of dotted pairs */
>>   55   #define CLOSXP›3›   /* closures */
>>   56   #define ENVSXP›4›   /* environments */
>>   57   #define PROMSXP›   5›   /* promises: [un]evaluated closure
>> arguments */
>>   58   #define LANGSXP›   6›   /* language constructs (special lists)
>> */
>>   59   #define SPECIALSXP   7› /* special forms */
>>   60   #define BUILTINSXP   8› /* builtin non-special forms */
>>   61   #define CHARSXP›   9›   /* "scalar" string type (internal
>> only)*/
>>   64  #define INTSXP›   13›   /* integer vectors */
>>   65   #define REALSXP›  14›   /* real variables */
>>   66   #define CPLXSXP›  15›   /* complex variables */
>>   67   #define STRSXP›   16›   /* string vectors */
>>   68   #define DOTSXP›   17›   /* dot-dot-dot object */
>>   69   #define ANYSXP›   18›   /* make "any" args work.
>>   72   #define VECSXP›   19›   /* generic vectors */
>>   73   #define EXPRSXP›  20›   /* expressions vectors */
>>   74   #define BCODESXP21/* byte code */
>>   62   etc
>>
>> this type is part of the sxpinfo structure:
>> struct sxpinfo_struct {
>>  142   SEXPTYPE type  :  TYPE_BITS;
>&g

Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-08 Thread Cesar Rabak
So, if I understand correctly we could use the introspective power of
picolisp and write a {meta?}program to produce the "library" for Pil, and
then use this to call R from picolisp?

--
Cesar Rabak

On Thu, Apr 8, 2021 at 4:14 PM Thorsten Jolitz  wrote:

> Hi Alex,
> after digging deeper in R Internals I found out that there are actually C
> access functions for the various subtypes of SEXP that can be wrapped with
> native too:
>
> rinc: (setupRinC)
> -> NIL
> rinc: (evalQuietlyInR "V <- c(1.5, 3.4, 4.2)")
> -> NIL
> rinc: (evalInR "V")
> -> 65007640
> rinc: (REAL (evalInR "V"))
> -> (2 3 4)
> rinc: (teardownRinC)
> -> NIL
>
> I think this is already quite nice.
> Setup an R session, do quiet evaluations in R, do evaluations with return
> value, convert that return value (SEXP subtype) to its appropriate Pil data
> type, tear down R session.
>
> So there are only a few challenges left:
> 1. write wrappers for all type conversion functions (for all SEXP subtypes)
> 2. figure out the SEXP subtype from the pointer returned
> 3. dynamically get the length for all SEXP subtypes with a length, to be
> able to dynamically define return types like '(1.0 . 3) in native or struct
> (I think there are C functions for this in R internals too)
>
> Question on topic 2:
> given the C data type described below for an R "node", would it be somehow
> possible to extract just the SEXPTYPE from a SEXP return value, i.e.
> the first field of the sexpinfo_struct, that is the first struct in the
> SEXPREC_HEADER, that is the first field in the SEXPREC struct to which the
> SEXP return value points?
> Without specifying that complicated C structure in native or struct,
> because we have the C access functions for that. Ignoring all the rest of
> the pointer content, just extract that one field SEXPTYPE?
>
> Knowing which SEXPTYPE the return value represents, one could then read
> the returned pointer with the appropriate data type into Pil (like REAL in
> the example above).
> if the return value is atomic, one is done. If its a vector, list, or so
> one would need the length of it too, not only the type (or is it a valid
> strategy to just use some very big value for the specification like '(1.0 .
> ) or so)?
>
> Cheers
> Thorsten
>
>
> *Composition of an R node: *
> the SEXP subtypes are defined like this:
> typedef unsigned int SEXPTYPE;
>   52   #define NILSXP›0›   /* nil = NULL */
>   53   #define SYMSXP›1›   /* symbols */
>   54   #define LISTSXP›   2›   /* lists of dotted pairs */
>   55   #define CLOSXP›3›   /* closures */
>   56   #define ENVSXP›4›   /* environments */
>   57   #define PROMSXP›   5›   /* promises: [un]evaluated closure
> arguments */
>   58   #define LANGSXP›   6›   /* language constructs (special lists)
> */
>   59   #define SPECIALSXP   7› /* special forms */
>   60   #define BUILTINSXP   8› /* builtin non-special forms */
>   61   #define CHARSXP›   9›   /* "scalar" string type (internal
> only)*/
>   64  #define INTSXP›   13›   /* integer vectors */
>   65   #define REALSXP›  14›   /* real variables */
>   66   #define CPLXSXP›  15›   /* complex variables */
>   67   #define STRSXP›   16›   /* string vectors */
>   68   #define DOTSXP›   17›   /* dot-dot-dot object */
>   69   #define ANYSXP›   18›   /* make "any" args work.
>   72   #define VECSXP›   19›   /* generic vectors */
>   73   #define EXPRSXP›  20›   /* expressions vectors */
>   74   #define BCODESXP21/* byte code */
>   62   etc
>
> this type is part of the sxpinfo structure:
> struct sxpinfo_struct {
>  142   SEXPTYPE type  :  TYPE_BITS;
>  143   /* ==> (FUNSXP == 99) %% 2^5 == 3 ==
> CLOSXP
>  144   › ›   ›,* -> warning: `type' is narrower than values
>  145   › ›   ›,*  of its type
>  146   › ›   ›,* when SEXPTYPE was an enum */
>  147   unsigned int scalar:  1;
>  148   unsigned int obj   :  1;
>  149etc
>
> which then becomes part of an header:
> #define SEXPREC_HEADER \
>  209   struct sxpinfo_struct sxpinfo; \
>  210   struct SEXPREC *attrib; \
>  211   struct SEXPREC *gengc_next_node, *gengc_prev_node
>  212
>
> which finally becomes part of the SEXPREC structure. A SEXP is a pointer
> to a SEXPREC structure.
>  213   /* The standard node structure consists of a header followed by the
>  214  node data. */
>  215   typedef struct SEXPREC {
>  216   SEXPREC_HEADER;
>  217   union {
>  218   › struct primsx

Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-08 Thread Thorsten Jolitz
Hi Alex,
after digging deeper in R Internals I found out that there are actually C
access functions for the various subtypes of SEXP that can be wrapped with
native too:

rinc: (setupRinC)
-> NIL
rinc: (evalQuietlyInR "V <- c(1.5, 3.4, 4.2)")
-> NIL
rinc: (evalInR "V")
-> 65007640
rinc: (REAL (evalInR "V"))
-> (2 3 4)
rinc: (teardownRinC)
-> NIL

I think this is already quite nice.
Setup an R session, do quiet evaluations in R, do evaluations with return
value, convert that return value (SEXP subtype) to its appropriate Pil data
type, tear down R session.

So there are only a few challenges left:
1. write wrappers for all type conversion functions (for all SEXP subtypes)
2. figure out the SEXP subtype from the pointer returned
3. dynamically get the length for all SEXP subtypes with a length, to be
able to dynamically define return types like '(1.0 . 3) in native or struct
(I think there are C functions for this in R internals too)

Question on topic 2:
given the C data type described below for an R "node", would it be somehow
possible to extract just the SEXPTYPE from a SEXP return value, i.e.
the first field of the sexpinfo_struct, that is the first struct in the
SEXPREC_HEADER, that is the first field in the SEXPREC struct to which the
SEXP return value points?
Without specifying that complicated C structure in native or struct,
because we have the C access functions for that. Ignoring all the rest of
the pointer content, just extract that one field SEXPTYPE?

Knowing which SEXPTYPE the return value represents, one could then read the
returned pointer with the appropriate data type into Pil (like REAL in the
example above).
if the return value is atomic, one is done. If its a vector, list, or so
one would need the length of it too, not only the type (or is it a valid
strategy to just use some very big value for the specification like '(1.0 .
) or so)?

Cheers
Thorsten


*Composition of an R node: *
the SEXP subtypes are defined like this:
typedef unsigned int SEXPTYPE;
  52   #define NILSXP›0›   /* nil = NULL */
  53   #define SYMSXP›1›   /* symbols */
  54   #define LISTSXP›   2›   /* lists of dotted pairs */
  55   #define CLOSXP›3›   /* closures */
  56   #define ENVSXP›4›   /* environments */
  57   #define PROMSXP›   5›   /* promises: [un]evaluated closure
arguments */
  58   #define LANGSXP›   6›   /* language constructs (special lists) */
  59   #define SPECIALSXP   7› /* special forms */
  60   #define BUILTINSXP   8› /* builtin non-special forms */
  61   #define CHARSXP›   9›   /* "scalar" string type (internal only)*/
  64  #define INTSXP›   13›   /* integer vectors */
  65   #define REALSXP›  14›   /* real variables */
  66   #define CPLXSXP›  15›   /* complex variables */
  67   #define STRSXP›   16›   /* string vectors */
  68   #define DOTSXP›   17›   /* dot-dot-dot object */
  69   #define ANYSXP›   18›   /* make "any" args work.
  72   #define VECSXP›   19›   /* generic vectors */
  73   #define EXPRSXP›  20›   /* expressions vectors */
  74   #define BCODESXP21/* byte code */
  62   etc

this type is part of the sxpinfo structure:
struct sxpinfo_struct {
 142   SEXPTYPE type  :  TYPE_BITS;
 143   /* ==> (FUNSXP == 99) %% 2^5 == 3 ==
CLOSXP
 144   › ›   ›,* -> warning: `type' is narrower than values
 145   › ›   ›,*  of its type
 146   › ›   ›,* when SEXPTYPE was an enum */
 147   unsigned int scalar:  1;
 148   unsigned int obj   :  1;
 149etc

which then becomes part of an header:
#define SEXPREC_HEADER \
 209   struct sxpinfo_struct sxpinfo; \
 210   struct SEXPREC *attrib; \
 211   struct SEXPREC *gengc_next_node, *gengc_prev_node
 212

which finally becomes part of the SEXPREC structure. A SEXP is a pointer to
a SEXPREC structure.
 213   /* The standard node structure consists of a header followed by the
 214  node data. */
 215   typedef struct SEXPREC {
 216   SEXPREC_HEADER;
 217   union {
 218   › struct primsxp_struct primsxp;
 219   › struct symsxp_struct symsxp;
 220   › etc




Am Mi., 7. Apr. 2021 um 15:55 Uhr schrieb Alexander Burger <
a...@software-lab.de>:

> On Wed, Apr 07, 2021 at 07:46:20AM +0200, Alexander Burger wrote:
> > OK, so now we have a pointer to a structure filled by evalInR().
> > ...
> >(struct (evalInR "6*4") ...)
>
> In fact this depends on what exactly evalInR() returns.
>
> If it returns a pointer to a dynamically allocated structure, and the
> caller is
> responsible to de-allocate it, you would do
>
>(let P (evalInR "6*4")
>   (prog1
>  (struct P ...)
>  (free P) ) )
>
> If, however, the structure is statically allocated, you can also 

Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-07 Thread Alexander Burger
On Wed, Apr 07, 2021 at 07:46:20AM +0200, Alexander Burger wrote:
> OK, so now we have a pointer to a structure filled by evalInR().
> ...
>(struct (evalInR "6*4") ...)

In fact this depends on what exactly evalInR() returns.

If it returns a pointer to a dynamically allocated structure, and the caller is
responsible to de-allocate it, you would do

   (let P (evalInR "6*4")
  (prog1
 (struct P ...)
 (free P) ) )

If, however, the structure is statically allocated, you can also give the
structure's structure directly as the return value specification:

   (native `*RinC "evalInR" '(...) "Cmd")

For example, as a (somewhat conceived) test case

   static struct {
  char *s;
  int i[4];
  double d;
  char nm[8];
   } Data;

   void *foo(void) {
  Data.s = "Hello";
  Data.i[0] = 1;
  Data.i[1] = 2;
  Data.i[2] = -3;
  Data.i[3] = 4;
  Data.d = 0.99;
  strcpy(Data.nm, "world");
  return 
   }

Then in the REPL

   : (native "./test.so" "foo" '(S I (I . 2) (B . 4) 100 (C . 8)))
   -> ("Hello" 1 (2 -3) (4 0 0 0) 99 ("w" "o" "r" "l" "d"))

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe



Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-06 Thread Alexander Burger
Hi Thorsten,

>  (de evalInR ("Cmd")
>   (native `*RinC "evalInR" 'P "Cmd"))
> 
> ##  SEXP evalInR(char * cmd);
> 
> rinc: (evalInR "print(6*4)")
> [1] 24
> -> 65814160

OK, so now we have a pointer to a structure filled by evalInR().


> rinc: (evalInR "6*4")
> -> 65814160
> 
> how can I extract the result from that Pointer in PicoLisp (lack of C
> skills ... ;-)?

You can use the 'struct' function to access it.

I could not find the exact definition of SEXP. but as it is a pointer to a
structure, you can do

   (struct (evalInR "6*4") ...)

The arguments depend on the exact layout of the data in the C struct. I think
this helps: https://software-lab.de/doc/native.html

☺/ A!ex


-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-06 Thread Thorsten Jolitz
Hi Cesar,
it works both ways, and there is an old R C API  and more modern C++
implementations (with C headers too) for the use case here:
- call R from C(++).
or better:
- call R from PicoLisp via (native) C (calls)
See my last answer to Alex to see that the call to R actually works.
Cheers
Thorsten.


Am Di., 6. Apr. 2021 um 22:20 Uhr schrieb Cesar Rabak :

> Hi Thorsten,
>
> The quote you copied here (coming from the ref. of yours R's C Interface),
> describes IIUC a structure, more accurately the model of, for calling
> "foreing" C functions in R and not the converse.
>
> The way to call R functions in picolisp would be to call the functions
> made available through the API, in case of R a process similar to FFI using
> as reference the include files, wouldn't?
>
> HTH
> --
> Cesar Rabak
>
>
> On Mon, Apr 5, 2021 at 4:30 PM Thorsten Jolitz  wrote:
>
>> Hello List,
>> I wonder how to deal with the R SEXP Data structure in native calls.
>>
>> *"Technically, [a SEXP] is a pointer to a structure with typedef SEXPREC

Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-06 Thread Thorsten Jolitz
Hi Alex,
thanks for the hints, I tried both, T and 'P as result values , the first
gives a segment fault, but the second actually works:

 (de evalInR ("Cmd")
  (native `*RinC "evalInR" 'P "Cmd"))


##  SEXP evalInR(char * cmd);

rinc: (evalInR "print(6*4)")
[1] 24
-> 65814160

If I use "print" in the R command, I actually see the R output.
And with the 'P, I actually get a return value.
But what can I do in PicoLisp with that pointer? If I skip the "print" and
just call R for the return value:

rinc: (evalInR "6*4")
-> 65814160

how can I extract the result from that Pointer in PicoLisp (lack of C
skills ... ;-)?
Cheers
Thorsten

Am Di., 6. Apr. 2021 um 10:23 Uhr schrieb Alexander Burger <
a...@software-lab.de>:

> Hi Thorsten,
>
> > I wonder how to deal with the R SEXP Data structure in native calls.
> >
> > *"Technically, [a SEXP] is a pointer to a structure with typedef SEXPREC.
> > A SEXP is a variant type, with subtypes for all R’s data structures"*
> >
> > E.g.
> >
> >- INTSXP: integer vector
> >- LGLSXP: logical vector
> >- STRSXP: character vector
> >- ... (and a dozen more)
>
> I do not really understand the implications in the context of R, but Pil21
> has a
> new 'T' result specification for raw Lisp data.
>
> It allows to pass a pointer to any Lisp data item to a native function,
> and/or
> to return such data.
>
>
> > When I have an imaginary generic C function like this:
> >
> > SEXP fun(SEXP x, char * cmd)
>
> As an example, we might call the standard 'prog1' function in the Pil21
> executable (which has the internal label "_prog1", try (vi 'prog1)). It
> takes a
> list for the body, and returns the resulting value:
>
>: (%@ "_prog1" T '(T prog1 7 (println 1 2 3)))
>1 2 3
>-> 7
>
> This is equivalent to
>
>: (prog1 7 (println 1 2 3))
>1 2 3
>-> 7
>
>
> > how can I specify Return Value/Arguments in a 'native' call, if I cannot
> > know in advance to which R subtype the  SEXP Points?
>
> If the above is not what you intended, the 'P' result specification can be
> used
> as an unspecific pointer (void*, an unsigned 64 bit number).
>
> ☺/ A!ex
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-06 Thread Cesar Rabak
Hi Thorsten,

The quote you copied here (coming from the ref. of yours R's C Interface),
describes IIUC a structure, more accurately the model of, for calling
"foreing" C functions in R and not the converse.

The way to call R functions in picolisp would be to call the functions made
available through the API, in case of R a process similar to FFI using as
reference the include files, wouldn't?

HTH
--
Cesar Rabak


On Mon, Apr 5, 2021 at 4:30 PM Thorsten Jolitz  wrote:

> Hello List,
> I wonder how to deal with the R SEXP Data structure in native calls.
>
> *"Technically, [a SEXP] is a pointer to a structure with typedef SEXPREC.
> A SEXP is a variant type, with subtypes for all R’s data structures."*
>
> E.g.
>
>- INTSXP: integer vector
>- LGLSXP: logical vector
>- STRSXP: character vector
>- ... (and a dozen more)
>
> When I have an imaginary generic C function like this:
>
> SEXP fun(SEXP x, char * cmd)
>
> how can I specify Return Value/Arguments in a 'native' call, if I cannot
> know in advance to which R subtype the  SEXP Points?
>
> On the one hand, a SEXP is like a C Datatype for a Lisp list, so one could
> think that's ideal for calling C from Pil with list args and list return
> values. On the other hand, how can one give the primitive specifications in
> the specification lists for 'native' args/return vals, without knowing the
> concrete subtype (R data structure), i.e. when CMD can be any R command
> that may take/return any of the subtypes (char, logical, int, vector, list
> .)?
>
> Is there maybe a generic solution on the 'native' side too, without the
> primitive specifications?
> Thanks in advance and Cheers
> Thorsten
>
> PS
> For those interested here two links:
> Rinternals.h source code:
> r-source/Rinternals.h at a1425adea54bcc98eef86081522b5dbb3e149cdc ·
> wch/r-source (github.com)
> <https://github.com/wch/r-source/blob/a1425adea54bcc98eef86081522b5dbb3e149cdc/src/include/Rinternals.h>
>
> R's C Interface (blog post explaining the code/data structures)
> R's C interface · Advanced R. (had.co.nz)
> <http://adv-r.had.co.nz/C-interface.html>.
>


Re: Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-06 Thread Alexander Burger
Hi Thorsten,

> I wonder how to deal with the R SEXP Data structure in native calls.
> 
> *"Technically, [a SEXP] is a pointer to a structure with typedef SEXPREC.
> A SEXP is a variant type, with subtypes for all R’s data structures"*
> 
> E.g.
> 
>- INTSXP: integer vector
>- LGLSXP: logical vector
>- STRSXP: character vector
>- ... (and a dozen more)

I do not really understand the implications in the context of R, but Pil21 has a
new 'T' result specification for raw Lisp data.

It allows to pass a pointer to any Lisp data item to a native function, and/or
to return such data.


> When I have an imaginary generic C function like this:
> 
> SEXP fun(SEXP x, char * cmd)

As an example, we might call the standard 'prog1' function in the Pil21
executable (which has the internal label "_prog1", try (vi 'prog1)). It takes a
list for the body, and returns the resulting value:

   : (%@ "_prog1" T '(T prog1 7 (println 1 2 3)))
   1 2 3
   -> 7

This is equivalent to

   : (prog1 7 (println 1 2 3))
   1 2 3
   -> 7


> how can I specify Return Value/Arguments in a 'native' call, if I cannot
> know in advance to which R subtype the  SEXP Points?

If the above is not what you intended, the 'P' result specification can be used
as an unspecific pointer (void*, an unsigned 64 bit number).

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Native Calls with SEXP (a variant type, with subtypes for all R’s data structures).

2021-04-05 Thread Thorsten Jolitz
Hello List,
I wonder how to deal with the R SEXP Data structure in native calls.

*"Technically, [a SEXP] is a pointer to a structure with typedef SEXPREC.
A SEXP is a variant type, with subtypes for all R’s data structures"*

E.g.

   - INTSXP: integer vector
   - LGLSXP: logical vector
   - STRSXP: character vector
   - ... (and a dozen more)

When I have an imaginary generic C function like this:

SEXP fun(SEXP x, char * cmd)

how can I specify Return Value/Arguments in a 'native' call, if I cannot
know in advance to which R subtype the  SEXP Points?

On the one hand, a SEXP is like a C Datatype for a Lisp list, so one could
think that's ideal for calling C from Pil with list args and list return
values. On the other hand, how can one give the primitive specifications in
the specification lists for 'native' args/return vals, without knowing the
concrete subtype (R data structure), i.e. when CMD can be any R command
that may take/return any of the subtypes (char, logical, int, vector, list
)?

Is there maybe a generic solution on the 'native' side too, without the
primitive specifications?
Thanks in advance and Cheers
Thorsten

PS
For those interested here two links:
Rinternals.h source code:
r-source/Rinternals.h at a1425adea54bcc98eef86081522b5dbb3e149cdc ·
wch/r-source (github.com)
<https://github.com/wch/r-source/blob/a1425adea54bcc98eef86081522b5dbb3e149cdc/src/include/Rinternals.h>

R's C Interface (blog post explaining the code/data structures)
R's C interface · Advanced R. (had.co.nz)
<http://adv-r.had.co.nz/C-interface.html>.


Re: SEXP?

2013-07-01 Thread Thorsten Jolitz
Alexander Burger a...@software-lab.de writes:

Hi Alex,

 I wonder if there is a way in PicoLisp to check if some function
 argument is a SEXP, i.e. if something like a function 'sexp? exists that
 returns T if the argument is a SEXP.

 I'm a bit puzzled by this question. To my understanding, every possible
 expression in PicoLisp is a SEXP. Even auto-quoting expressions like

I'm puzzled too ...

: T
- T

: abc
- abc

: (1 2 3)
- (1 2 3)

 are SEXPs (though it might be argued that the last one is not, since it
 doesn't contain any symbols).

.. and was a bit confused when asking, I probably meant 'list' instead of
SEXP. 

e.g., when I filter function args that are given as unevaluated list,
and should be either names of existing files or Emacs Lisp expressions:

,---
| (filter 'info CmdLineArgs)
`---

,---
| (filter '((X) (not (info X))) CmdLineArgs)
`---

then invalid file names fall into category 'elisp expressions' and cause
errors later on. But its probably better to check the expressions of the
elisp side and this question is kind of obsolete, but thanks anyway. 

 With so many occurences fo SEX in this post, I wonder if it will be
 filtered as SPAM or actually posted in the mailing list 

 Worked :) And NSA will only read read (and not filter) it.

at least I sent it only once and are not classified as spammer now,
hopefully ;)

-- 
cheers,
Thorsten

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: SEXP?

2013-07-01 Thread Alexander Burger
Hi Thorsten,

  I'm a bit puzzled by this question. To my understanding, every possible
  expression in PicoLisp is a SEXP. Even auto-quoting expressions like
 ...
 .. and was a bit confused when asking, I probably meant 'list' instead of
 SEXP. 
 ...
 then invalid file names fall into category 'elisp expressions' and cause
 errors later on. But its probably better to check the expressions of the
 elisp side and this question is kind of obsolete, but thanks anyway. 

In general, there is no way to detect statically whether an s-expression
is executable or not. Something like (a b c d) may not be executable
*now*, because 'a' is not a function, but it may be shortly after.

♪♫ Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: SEXP?

2013-07-01 Thread dexen deVries
On Monday 01 of July 2013 07:39:49 you wrote:
 Hi Thorsten,
 
  I wonder if there is a way in PicoLisp to check if some function
  argument is a SEXP, i.e. if something like a function 'sexp? exists that
  returns T if the argument is a SEXP.


how about (pair 'any) ? basically the opposite of (atom 'any)


-- 
dexen deVries

[[[↓][→]]]

Take care of the luxuries and the necessities will take care of themselves.
-- L. Long

--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: SEXP?

2013-07-01 Thread Thorsten Jolitz
dexen deVries dexen.devr...@gmail.com
writes:

 On Monday 01 of July 2013 07:39:49 you wrote:
 Hi Thorsten,
 
  I wonder if there is a way in PicoLisp to check if some function
  argument is a SEXP, i.e. if something like a function 'sexp? exists that
  returns T if the argument is a SEXP.


 how about (pair 'any) ? basically the opposite of (atom 'any)

thats probably what I was looking for, thanks, besides the fact that
Alex is of course right about the difficulties with doing these kind of
tests in a dynamic environment. 

-- 
cheers,
Thorsten

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: SEXP?

2013-07-01 Thread Joe Bogner
Hi Thorsten,

I've used fun? to determine if an argument had the proper form of something
that was executable or if it was data. It may not work in all cases, but it
was sufficient for my use case.  As Alex mentioned, it won't let you know
whether it's actually executable or not. I think of it as having the
potential to be executable.

Joe


On Mon, Jul 1, 2013 at 4:50 AM, Thorsten Jolitz tjol...@gmail.com wrote:

 dexen deVries dexen.devr...@gmail.com
 writes:

  On Monday 01 of July 2013 07:39:49 you wrote:
  Hi Thorsten,
 
   I wonder if there is a way in PicoLisp to check if some function
   argument is a SEXP, i.e. if something like a function 'sexp? exists
 that
   returns T if the argument is a SEXP.
 
 
  how about (pair 'any) ? basically the opposite of (atom 'any)

 thats probably what I was looking for, thanks, besides the fact that
 Alex is of course right about the difficulties with doing these kind of
 tests in a dynamic environment.

 --
 cheers,
 Thorsten

 --
 UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe



Re: SEXP?

2013-07-01 Thread Thorsten Jolitz
Joe Bogner joebog...@gmail.com writes:

Hi Joe,

 I've used fun? to determine if an argument had the proper form of
 something that was executable or if it was data. It may not work in
 all cases, but it was sufficient for my use case.  As Alex mentioned,
 it won't let you know whether it's actually executable or not. I think
 of it as having the potential to be executable.

'fun? would be another possibility, but 'pair is a little bit more
tolerant (returns non-NIL in more cases), and since I apply it on Emacs
Lisp (and not PicoLisp) expressions, this tolerance seems to be a good
thing.

But thanks for the tip anyway!

 On Mon, Jul 1, 2013 at 4:50 AM, Thorsten Jolitz
 tjol...@gmail.com wrote:

 dexen deVries
 dexen.devr...@gmail.com writes:
 
 
  On Monday 01 of July 2013 07:39:49 you wrote:
  Hi Thorsten,
 
   I wonder if there is a way in PicoLisp to check if some function
   argument is a SEXP, i.e. if something like a function 'sexp? exists
 that
   returns T if the argument is a SEXP.
 
 
  how about (pair 'any) ? basically the opposite of (atom 'any)
 
 
 thats probably what I was looking for, thanks, besides the fact that
 Alex is of course right about the difficulties with doing these kind of
 tests in a dynamic environment.
 
 
 
 --
 cheers,
 Thorsten
 
 --
 UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
 



-- 
cheers,
Thorsten

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


SEXP?

2013-06-30 Thread Thorsten Jolitz

Hi List, 

I wonder if there is a way in PicoLisp to check if some function
argument is a SEXP, i.e. if something like a function 'sexp? exists that
returns T if the argument is a SEXP.

To complicate things a bit, I would actually need to check in PicoLisp
if the argument given is an Emacs Lisp SEXP, but maybe it actually does
not make a difference since in this case Lisp is Lisp, no matter what
dialect.

PS 

With so many occurences fo SEX in this post, I wonder if it will be
filtered as SPAM or actually posted in the mailing list 

-- 
cheers,
Thorsten


-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe