caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: "'Raj Bandyopadhyay'" <rajb@rice.edu>, <caml-list@yquem.inria.fr>
Subject: RE: [Caml-list] Accessing constructors (tags) from C
Date: Sun, 23 Nov 2008 18:42:04 -0000	[thread overview]
Message-ID: <005c01c94d9b$2fbec1b0$8f3c4510$@com> (raw)
In-Reply-To: <49299F76.6090006@rice.edu>

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


  parent reply	other threads:[~2008-11-23 18:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-23 18:22 Raj Bandyopadhyay
2008-11-23 18:38 ` [Caml-list] " Eric Cooper
2008-11-23 18:42 ` David Allsopp [this message]
2008-11-23 21:49   ` Mathias Kende

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='005c01c94d9b$2fbec1b0$8f3c4510$@com' \
    --to=dra-news@metastack.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=rajb@rice.edu \
    /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).