caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Basile Starynkevitch [local]" <basile.starynkevitch@inria.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] How OCaml objects of sum types can be passed to a C/C++ functions?
Date: Wed, 7 Jul 2004 11:29:29 +0200	[thread overview]
Message-ID: <20040707092928.GA26575@bourg.inria.fr> (raw)
In-Reply-To: <Pine.LNX.4.44.0407071047060.15731-100000@strudel.di.unipi.it>

On Wed, Jul 07, 2004 at 10:56:04AM +0200, Claudio Trento wrote:
> 
> on the subject of interfacing OCaml with C/C++, it is not clear
> to me whether and how OCaml objects of sum types can be passed
> to a C/C++ function.  For example, suppose I have the declaration
> 
> type expr =

(* I Basile added the 2 cases below for explanations *)
       Nonsense
     | Bizarre

>    | Variable of int
>    | Coefficient of int
>    | UPlus of expr
>    | UMinus of expr
>    | Sum of (expr * expr)
>    | Difference of (expr * expr)
>    | Product of (expr * expr)
> 
> What is the best way to pass an expr object to C/C++?

It is documented in the reference manual:
http://caml.inria.fr/ocaml/htmlman/manual032.html sections 18.2.2 and
18.3.4

The two cases Nonsense and Bizarre I added (for the example) have no
argument (i.e. no "of" keyword - for representation issues, Foo of unit
is not like just Foo). So they are represented by small non-negative
integer, so Nonsense is 0 and Bizarre is 1.

The other cases have an "of" keyword, so are represented as tagged
blocks. The first case Variable has tag 0, the next Coefficient has
tag 1, etc

> How can C/C++ code visit such a structure?

Test if a value is a tagged integer, and otherwise it is a block, test
its tag:

in Ocaml: 
   external c_fun : expr -> unit = "c_fun_ml"

in C // untested code

value c_fun_ml(value ex) {
   CAMLparam1(ex);
   switch (ex) {
   case Val_int(0): /* Nonsense */
     break;
   case Val_int(1): /* Bizarre */
     break;
   default: {
     int tag = Tag_val(ex);
     switch (tag) {
       case 0: /* Variable varnum */
         { int varnum = Int_val(Field(ex,0); 
           break; }
       case 1: /* Coefficient */ 
       default: 
           break;
      } /* end switch tag */
     break;
   };
   } /* end switch ex */
   CAMLreturn(Val_unit);
}


I hope you got the idea. I advise you to avoid coding in C, and to
call C functions only inside your Ocaml wrappers (which do appropriate
checks, raise exceptions, etc...)

Regards.

-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - phone +33 1 3963 5197 - mobile 6 8501 2359
http://cristal.inria.fr/~starynke --- all opinions are only mine 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      parent reply	other threads:[~2004-07-07  9:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-07  8:56 Claudio Trento
2004-07-07  9:24 ` Richard Jones
2004-07-07  9:29 ` Basile Starynkevitch [local] [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040707092928.GA26575@bourg.inria.fr \
    --to=basile.starynkevitch@inria.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).