Puregen tutorial

41 views
Skip to first unread message

u89012

unread,
Apr 2, 2019, 12:18:11 AM4/2/19
to pure-lang
Hello Group,

I've written before asking for help with libraries but haven't got any response. I've decided to jump in and take care of it myself (with gentle push from Albert, thank you Albert!). I could get simple C functions exposed to Pure but I'm facing some challenges trying to understand how structs are interfaced and the wrapper's role in allocating and deallocating structs or memory in general. Could someone, please please, share some ideas (and working code if possible) to demonstrate a simple example? I'm particularly looking to understand how to handle in/out parameters that are structs.

Oh one more thing, I see that pure-gen manual demonstrates wrapping gsl library, but I haven't had any luck trying to get it to work on macOS (as rightly mentioned on that page, I'm getting a lot of `system headers missing` errors). Can someone tell me if this does indeed generate a complete wrapper or will there be additional work required to get the full gsl? I'm assuming additional work will be required as otherwise the gsl provided by pure as add-on would be that full gsl. 

Thanks!

Kurt Pagani

unread,
Apr 2, 2019, 11:08:32 AM4/2/19
to pure...@googlegroups.com
Have a look into the sources of pure-sockets, pure-sql3, etc. @
https://github.com/agraef/pure-lang. There you'll certainly see how it works.
AFAIK structs are handled by pointers (void*) only, that is you sometimes have
to write some additional helper functions in C yourself (mostly for convenience).

For example, when you look at the structure at the top of the attached defs.h,
and a C function e.g.

int ispoly(U *p, U *x);

it reads in Pure simply as

extern int ispoly(void *p, void *x);

That is, you don't have to care about "U" itself as long as you are not
interested in particular slots. However, if you want access to the internals of
U, then it will be useful to write some "accessors" in C which can be imported
using simple return types.

Sometimes it will be necessary to "malloc", but this works fine, usually:

let env = NULL ;
assert fact::string = EnvAssertString env fact ;
showpp fact::pointer = string buf
when buf = malloc 256; EnvGetFactPPForm env buf 256 fact end ;

Hope this helps a bit.
Kurt
> --
> You received this message because you are subscribed to the Google Groups
> "pure-lang" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to pure-lang+...@googlegroups.com
> <mailto:pure-lang+...@googlegroups.com>.
> To post to this group, send email to pure...@googlegroups.com
> <mailto:pure...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/pure-lang.
> For more options, visit https://groups.google.com/d/optout.
defs.h

Kurt Pagani

unread,
Apr 2, 2019, 12:37:29 PM4/2/19
to pure...@googlegroups.com
> Oh one more thing, I see that pure-gen manual demonstrates wrapping gsl library,
> but I haven't had any luck trying to get it to work on macOS (as rightly
> mentioned on that page, I'm getting a lot of `system headers missing` errors).
> Can someone tell me if this does indeed generate a complete wrapper or will
> there be additional work required to get the full gsl? I'm assuming additional
> work will be required as otherwise the gsl provided by pure as add-on would be
> that full gsl. 

Well, I can't say much regarding macOS, but it works for me on Ubuntu/Debian,
but there are indeed some issues on cygwin64 and mingw32. Perhaps one has to
'sync' with the newer sources of GSL.

Did you use the sources on github or the tar package below?
https://bitbucket.org/purelang/pure-lang/downloads/pure-gsl-0.12.tar.gz

My experience is that "pure-gen" is good for starting, i.e. wrapping big
libraries like ecl, clips, etc., however, one has to do some needlework usually.
For small libraries (some dozens of functions) it's hardly worthwile, IMO ;)



Kurt Pagani

unread,
Apr 2, 2019, 4:01:47 PM4/2/19
to pure-lang
>>  share some ideas (and working code if possible) to demonstrate a simple example? I'm particularly looking to understand how to handle in/out parameters that are structs.
 
Here a simple example (note: inline %< %>)

// Example (copy into struct.pure)
// Note that by using inline code the external decls are not mandatory ...

%<

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} ; 


void printBook( struct Books *book );


struct Books *load( ) {

   struct Books *p = malloc(sizeof(struct Books));
 
   strcpy( p->title, "Pure Programming");
   strcpy( p->author, "A.G.");
   strcpy( p->subject, "Pure Programming Tutorial");
   p->book_id = 1234567;

   //printBook(p);

   return(p);
}

void printBook( struct Books *book ) {

   printf( "Book title   : %s\n", book->title);
   printf( "Book author  : %s\n", book->author);
   printf( "Book subject : %s\n", book->subject);
   printf( "Book book_id : %d\n", book->book_id);
}

int getId( struct Books *book ) {return book->book_id;};


%>


let p = load;
printBook p;
getId p;

// run

$ pure

 __ \  |   |  __| _ \    Pure 0.68 (i686-w64-mingw32)
 |   | |   | |    __/    Copyright (c) 2008-2018 by Albert Graef
 .__/ \__,_|_|  \___|    (Type 'help' for help, 'help copying'
_|                       for license information.)

Loaded prelude from C:/msys64/mingw32/lib/pure/prelude.pure.

> using struct;
Book title   : Pure Programming
Book author  : A.G.
Book subject : Pure Programming Tutorial
Book book_id : 1234567
> p;
#<pointer 03554880>
> getId p;
1234567
>




u89012

unread,
Apr 3, 2019, 12:07:44 AM4/3/19
to pure-lang
Dear Kurt, Thank you so much for taking the time to reply and thank your for your insights and the code! I didn't know you could inline C code like that! That's very good to know! I also realized that additional work was probably required to get the full gsl (I was looking for quad functions but looks like it'll have to wait for now). I'll digest this and let you know how it goes. Appreciate all your help!

Albert Graef

unread,
Apr 3, 2019, 5:20:05 AM4/3/19
to pure...@googlegroups.com
Hi Sunder,

did you install Pure from MacPorts? https://github.com/agraef/pure-lang/wiki/PureOnMacOSX

The pure-gsl module is available in MacPorts, so you can simply install it from there. But note that the module is still rather incomplete. IIRC, I've started work on it quite some time ago, and Eddie Rucker added quite a bit of operations he was interested in, but consider it WIP.

Albert


--
You received this message because you are subscribed to the Google Groups "pure-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pure-lang+...@googlegroups.com.
To post to this group, send email to pure...@googlegroups.com.


--
Dr. Albert Gr"af
Computer Music Research Group, JGU Mainz, Germany
Email:  agg...@gmail.com
WWW:    https://plus.google.com/+AlbertGraef

u89012

unread,
Apr 3, 2019, 7:31:43 AM4/3/19
to pure-lang
Hello Albert, Yes I did and that works great, thank you! But I was looking to get the full gsl going by trying to compile it locally as mentioned in pure-gen manual page and that just wouldn't work on my laptop (running macOS). I intend to try again on a Linux box shortly. Hope your conference went well! 

Regards,
Sunder
To unsubscribe from this group and stop receiving emails from it, send an email to pure...@googlegroups.com.

To post to this group, send email to pure...@googlegroups.com.
Visit this group at https://groups.google.com/group/pure-lang.
For more options, visit https://groups.google.com/d/optout.

Albert Graef

unread,
Aug 21, 2019, 2:12:17 PM8/21/19
to pure...@googlegroups.com
On Wed, Apr 3, 2019 at 1:31 PM u89012 <u89...@gmail.com> wrote:
Hello Albert, Yes I did and that works great, thank you! But I was looking to get the full gsl going by trying to compile it locally as mentioned in pure-gen manual page and that just wouldn't work on my laptop (running macOS). I intend to try again on a Linux box shortly. Hope your conference went well! 

Yeah, that example did work at one point in time, honest. ;-) But now I'm getting a bunch of compilation errors as well. There's syntax that pure-gen's C/C++ parser doesn't understand (some missing symbol declarations), and apparently pure-gen doesn't know how to deal with long double pointers. Probably nothing serious that can't be fixed, but I can't work on it right now, so I created a ticket for it: https://github.com/agraef/pure-lang/issues/30

I'm pretty sure that I can fix those long double pointers in pure-gen, by looking at what dump-ast outputs in that case, but if anyone fluent in Haskell has an idea why the parser always crashes in dump-ast.hs:27 with that Haskell exception, please do let me know! :)

Cheers,
Albert

--
Dr. Albert Gr"af
Computer Music Research Group, JGU Mainz, Germany
Reply all
Reply to author
Forward
0 new messages