From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.5 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 3B665BB84 for ; Sun, 23 Nov 2008 19:42:21 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: As8AAAIzKUlRZ90xkWdsb2JhbACNMYYrAQEBAQkLCgcRA7ktgnyDQQ X-IronPort-AV: E=Sophos;i="4.33,654,1220220000"; d="scan'208";a="19468470" Received: from mtaout03-winn.ispmail.ntl.com ([81.103.221.49]) by mail3-smtp-sop.national.inria.fr with ESMTP; 23 Nov 2008 19:42:20 +0100 Received: from aamtaout04-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout03-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20081123184219.MWJL1691.mtaout03-winn.ispmail.ntl.com@aamtaout04-winn.ispmail.ntl.com>; Sun, 23 Nov 2008 18:42:19 +0000 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout04-winn.ispmail.ntl.com (InterMail vG.2.02.00.01 201-2161-120-102-20060912) with ESMTP id <20081123184219.DUWN22934.aamtaout04-winn.ispmail.ntl.com@romulus.metastack.com>; Sun, 23 Nov 2008 18:42:19 +0000 Received: from Countertenor ([149.254.192.219]) (authenticated bits=0) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id mANIgDsr015509 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Sun, 23 Nov 2008 18:42:16 GMT From: "David Allsopp" To: "'Raj Bandyopadhyay'" , References: <49299F76.6090006@rice.edu> In-Reply-To: <49299F76.6090006@rice.edu> Subject: RE: [Caml-list] Accessing constructors (tags) from C Date: Sun, 23 Nov 2008 18:42:04 -0000 Organization: MetaStack Solutions Ltd. Message-ID: <005c01c94d9b$2fbec1b0$8f3c4510$@com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AclNmGymbRJWPg38QyKSXklsEMnFHAAAXxlA Content-Language: en-gb X-Scanned-By: MIMEDefang 2.64 on 81.102.132.74 X-Cloudmark-Analysis: v=1.0 c=1 a=IARX6Qb8WPkA:10 a=O_srGWgP91AA:10 a=ZOzjf2MOAAAA:8 a=CjxXgO3LAAAA:8 a=U4F_L8JwRYlw5sQtHc0A:9 a=vHMwMwrD2aAqvoGeVnIA:7 a=t2k2DU0r7l_6mAgMSIi_Ty6tE3AA:4 a=BDXKcin-EtgA:10 X-Spam: no; 0.00; constructors:01 val:01 alloc:01 val:01 mli:01 constructors:01 bindings:01 camlp:01 bindings:01 variants:01 integers:01 ocaml:01 beginner's:01 ocaml:01 bug:01 See Section 18.3.4 of the manual. In your example below, Null is an integer 0 and Int is a 1-word block with tag 1. In C, Null is Val_int(0) and t = Int(x) is caml_alloc(1, 1); Store_field(t, 0, Val_int(x)). In general, the first constructor is 0 and so on in the order specified in the type declaration in ML. It is therefore *very* important to put a note in your .mli file that the order of the constructors matters because you're using it from C bindings. If you want to do this automatically, you'll need to write a pre-processor (a camlp4 filter can probably help you build a .h file that you can #include in your C files if you want to do it symbolically - I've never had to write C bindings large enough to worry about the fragility of sum types). Note that polymorphic variants have a completely different representation explained in 18.3.6 in the reference manual. David > -----Original Message----- > From: caml-list-bounces@yquem.inria.fr [mailto:caml-list- > bounces@yquem.inria.fr] On Behalf Of Raj Bandyopadhyay > Sent: 23 November 2008 18:23 > To: caml-list@yquem.inria.fr > Subject: [Caml-list] Accessing constructors (tags) from C > > Hi all > > I was wondering how to access constructors from C. For example if I > define a type > > type t = Null | Int of int > > and I want to create values of type t in C using the OCaml-C interface, > how do I find > the integers corresponding to constructors Null or Int? I would like to > do this for a large OCaml program with lots of different types etc. > > Thanks! > Raj > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs