[mercury-users] C FFI for discriminated union types.

Guillaume Yziquel guillaume.yziquel at citycable.ch
Sun Jun 19 23:54:00 AEST 2011


Le Sunday 19 Jun 2011 à 23:26:59 (+1000), Julien Fischer a écrit :
> >
> >However the tradeoff is:
> >
> >-1- Do the conversion in Mercury code, and take extra care for OCaml's
> >GC in every C stub that calls Mercury since it may trigger OCaml's GC.
> >
> >-2- Do the conversion in C code, no OCaml GC worries (or at least not
> >too much) in exchange for tedious conversion function in C level.
> 
> There is a third possibility, do the conversion in C code that makes
> calls back to Mercury, via pragma foreign_export, in order to manipulate
> the d.u. type.  For the type above, since presumably all you need to do
> is deconstruct it, is:
> 
>    :- pragma foreign_export("C", is_term_def(in, out), "C_is_term_def").
>    :- pred is_term_def(term_def::in, term::out) is semidet.
>    is_term_def(term_def(Term), Term).
> 
>    :- pragma foreign_export("C", is_term_def_str(in, out),
>      "C_is_term_def").
>    :- pred is_term_def_string(term_def::in, str::out) is semidet.
>    is_term_def_str(term_def_string(Str), Str).
> 
> The C code code that converts the Mercury values to OCaml values would
> look something like this:
> 
>     MR_Word TermDef;
>     <Term>  term;
>     <Str>   str;
> 
>     if (C_is_term_def(TermDef, &term)) {
> 	/* Convert term_def/1 with argument term to OCaml */
>     } else if (C_is_term_def_string(TermDef, &str)) {
> 	/* Convert term_def_string/1 with argument str to OCaml */
>     } else {
> 	/* error */
>     }
> 
> (<Term> and <Str> are whatever the C types of term/0 and str/0 are.)

Yes, that is exactly how I am handling it now, but I find this to be
quite brittle, and has caused a few segmentation faults while
refactoring things somewhat brutally. But, yes, with the given
documentation, it seems the designated road to follow.

> >Both are workable, but I'd appreciate having more background, if only
> >informally, about the design of data representation for discriminated
> >union datatypes in C grades.
> 
> See section 3.2 of the following paper:
> 
>     The execution algorithm of Mercury: an efficient purely declarative
>     logic programming language
>     Zoltan Somogyi, Fergus Henderson and Thomas Conway. JLP, 1996.
> 
> which is available from the papers section of the Mercury website, at
> <http://www.mercury.csse.unimelb.edu.au/information/papers.html#jlp>.
> 
> Note that some of the implementation details have changed since that
> paper was written but the basic ideas are pretty much the same.

Sure. Thank you so much for that reference.

> Julien.

Best regards,

-- 
     Guillaume Yziquel

--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list