caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Accessing constructors (tags) from C
@ 2008-11-23 18:22 Raj Bandyopadhyay
  2008-11-23 18:38 ` [Caml-list] " Eric Cooper
  2008-11-23 18:42 ` David Allsopp
  0 siblings, 2 replies; 4+ messages in thread
From: Raj Bandyopadhyay @ 2008-11-23 18:22 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Accessing constructors (tags) from C
  2008-11-23 18:22 Accessing constructors (tags) from C Raj Bandyopadhyay
@ 2008-11-23 18:38 ` Eric Cooper
  2008-11-23 18:42 ` David Allsopp
  1 sibling, 0 replies; 4+ messages in thread
From: Eric Cooper @ 2008-11-23 18:38 UTC (permalink / raw)
  To: caml-list

On Sun, Nov 23, 2008 at 01:22:46PM -0500, Raj Bandyopadhyay wrote:
> I was wondering how to access constructors from C.

See http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html,
in particular, section 18.3.

> 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?

This falls under the section "Concrete types", where it says:

    The constant constructors and the non-constant constructors for a
    given concrete type are numbered separately, starting from 0, in
    the order in which they appear in the concrete type declaration.

> I would like to do this for a large OCaml program with lots of
> different types etc.

That will get tedious and error-prone.  I'd look into using an
interface description language and stub compiler, like camlidl or
ocamlrpcgen.

-- 
Eric Cooper             e c c @ c m u . e d u


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [Caml-list] Accessing constructors (tags) from C
  2008-11-23 18:22 Accessing constructors (tags) from C Raj Bandyopadhyay
  2008-11-23 18:38 ` [Caml-list] " Eric Cooper
@ 2008-11-23 18:42 ` David Allsopp
  2008-11-23 21:49   ` Mathias Kende
  1 sibling, 1 reply; 4+ messages in thread
From: David Allsopp @ 2008-11-23 18:42 UTC (permalink / raw)
  To: 'Raj Bandyopadhyay', caml-list

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [Caml-list] Accessing constructors (tags) from C
  2008-11-23 18:42 ` David Allsopp
@ 2008-11-23 21:49   ` Mathias Kende
  0 siblings, 0 replies; 4+ messages in thread
From: Mathias Kende @ 2008-11-23 21:49 UTC (permalink / raw)
  To: caml-list

Le dimanche 23 novembre 2008 à 18:42 +0000, David Allsopp a écrit :
> 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)).

(Int x) is t = caml_alloc(1,0); Store_field(t,0,Val_int(x)). Because the
numbering of the fields starts from zero separately for the constant and
non-constant constructors (definitely, this is error prone).

Mathias


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-11-23 21:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-23 18:22 Accessing constructors (tags) from C Raj Bandyopadhyay
2008-11-23 18:38 ` [Caml-list] " Eric Cooper
2008-11-23 18:42 ` David Allsopp
2008-11-23 21:49   ` Mathias Kende

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).