* [Caml-list] How OCaml objects of sum types can be passed to a C/C++ functions? @ 2004-07-07 8:56 Claudio Trento 2004-07-07 9:24 ` Richard Jones 2004-07-07 9:29 ` Basile Starynkevitch [local] 0 siblings, 2 replies; 3+ messages in thread From: Claudio Trento @ 2004-07-07 8:56 UTC (permalink / raw) To: caml-list 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 = 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++? How can C/C++ code visit such a structure? Many thanks for your time and patience, Claudio ------------------- 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 ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] How OCaml objects of sum types can be passed to a C/C++ functions? 2004-07-07 8:56 [Caml-list] How OCaml objects of sum types can be passed to a C/C++ functions? Claudio Trento @ 2004-07-07 9:24 ` Richard Jones 2004-07-07 9:29 ` Basile Starynkevitch [local] 1 sibling, 0 replies; 3+ messages in thread From: Richard Jones @ 2004-07-07 9:24 UTC (permalink / raw) To: Claudio Trento; +Cc: caml-list 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 = > 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++? > How can C/C++ code visit such a structure? > Many thanks for your time and patience, This section in the manual: http://caml.inria.fr/ocaml/htmlman/manual032.html#htoc217 contains a rather unclear explanation. Rich. -- Richard Jones. http://www.annexia.org/ http://www.j-london.com/ Merjis Ltd. http://www.merjis.com/ - improving website return on investment 'There is a joke about American engineers and French engineers. The American team brings a prototype to the French team. The French team's response is: "Well, it works fine in practice; but how will it hold up in theory?"' ------------------- 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 ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] How OCaml objects of sum types can be passed to a C/C++ functions? 2004-07-07 8:56 [Caml-list] How OCaml objects of sum types can be passed to a C/C++ functions? Claudio Trento 2004-07-07 9:24 ` Richard Jones @ 2004-07-07 9:29 ` Basile Starynkevitch [local] 1 sibling, 0 replies; 3+ messages in thread From: Basile Starynkevitch [local] @ 2004-07-07 9:29 UTC (permalink / raw) To: caml-list 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 ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2004-07-07 9:30 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2004-07-07 8:56 [Caml-list] How OCaml objects of sum types can be passed to a C/C++ functions? Claudio Trento 2004-07-07 9:24 ` Richard Jones 2004-07-07 9:29 ` Basile Starynkevitch [local]
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox