From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id LAA26203; Wed, 7 Jul 2004 11:30:01 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA26559 for ; Wed, 7 Jul 2004 11:30:00 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i679TxEV002052 for ; Wed, 7 Jul 2004 11:29:59 +0200 Received: from bourg.inria.fr (bourg.inria.fr [128.93.11.100]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA26337 for ; Wed, 7 Jul 2004 11:29:59 +0200 (MET DST) Received: from basile by bourg.inria.fr with local (Exim 4.34) id 1Bi8k5-0006vm-2F for caml-list@inria.fr; Wed, 07 Jul 2004 11:29:29 +0200 Date: Wed, 7 Jul 2004 11:29:29 +0200 To: caml-list@inria.fr Subject: Re: [Caml-list] How OCaml objects of sum types can be passed to a C/C++ functions? Message-ID: <20040707092928.GA26575@bourg.inria.fr> References: Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.6+20040523i From: "Basile Starynkevitch [local]" X-Miltered: at nez-perce with ID 40EBC297.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 basile:01 basile:01 2004:99 trento:99 interfacing:01 expr:01 expr:01 uminus:01 foo:01 foo:01 camlparam:01 val:01 val:01 camlreturn:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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