caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* OCaml/C variant representation
@ 2010-02-24 15:26 Jean Yang
  2010-02-24 16:42 ` Sylvain Le Gall
  2010-02-24 23:07 ` [Caml-list] " Richard Jones
  0 siblings, 2 replies; 5+ messages in thread
From: Jean Yang @ 2010-02-24 15:26 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 881 bytes --]

Hello,

  I'm having some trouble with variant representation in the OCaml/C
interface.

  According to the reference I found (
http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html), it seems like if
we have value v = variant VConstr(val), where val is some integer value,
should have Field(val, 0) == hash_variant("VConstr") and Int_val(Field(v, 0)
== val.  I don't seem to be understanding this correctly, since I am getting
Int_val(Field(v, 0)) == val.

  I have the following datatype declaration in my OCaml:
type newty = VConstr of int | ...
In the C implementation of OCaml function foo : newty -> int, I have
something like this;
  CAMLprim value foo (value v) {  ... }
In the body of this function I am looking at the fields of val.  Am I doing
something wrong here?

Thanks,
Jean

-- 
Jean Yang
http://people.csail.mit.edu/jeanyang
Save us!  Think before you print.

[-- Attachment #2: Type: text/html, Size: 1101 bytes --]

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

* Re: OCaml/C variant representation
  2010-02-24 15:26 OCaml/C variant representation Jean Yang
@ 2010-02-24 16:42 ` Sylvain Le Gall
  2010-02-28 15:16   ` [Caml-list] " Jianzhou Zhao
  2010-02-24 23:07 ` [Caml-list] " Richard Jones
  1 sibling, 1 reply; 5+ messages in thread
From: Sylvain Le Gall @ 2010-02-24 16:42 UTC (permalink / raw)
  To: caml-list

Hello,

On 24-02-2010, Jean Yang <jeanyang@csail.mit.edu> wrote:
> Hello,
>
>   I'm having some trouble with variant representation in the OCaml/C
> interface.
>
>   According to the reference I found (
> http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html), it seems like if
> we have value v = variant VConstr(val), where val is some integer value,
> should have Field(val, 0) == hash_variant("VConstr") and Int_val(Field(v, 0)
>== val.  I don't seem to be understanding this correctly, since I am getting
> Int_val(Field(v, 0)) == val.
>
>   I have the following datatype declaration in my OCaml:
> type newty = VConstr of int | ...
> In the C implementation of OCaml function foo : newty -> int, I have
> something like this;
>   CAMLprim value foo (value v) {  ... }
> In the body of this function I am looking at the fields of val.  Am I doing
> something wrong here?
>

Please have a look at:
http://caml.inria.fr/mantis/view.php?id=4803

This section is misleading. You should use hash_variant for `VConstr.

In your case VConstr of int will be Block with size = 1 and tag = 0;
first non-constant constructor and first field contains Value_int(...)

Regards
Sylvain Le Gall



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

* Re: [Caml-list] OCaml/C variant representation
  2010-02-24 15:26 OCaml/C variant representation Jean Yang
  2010-02-24 16:42 ` Sylvain Le Gall
@ 2010-02-24 23:07 ` Richard Jones
  1 sibling, 0 replies; 5+ messages in thread
From: Richard Jones @ 2010-02-24 23:07 UTC (permalink / raw)
  To: Jean Yang; +Cc: caml-list

On Wed, Feb 24, 2010 at 10:26:22AM -0500, Jean Yang wrote:
> In the body of this function I am looking at the fields of val.  Am I doing
> something wrong here?

As a general tip, use the extlib function Std.dump (or look at the
source of Std.dump) to see how values are represented at runtime.

For example:

$ rlwrap ocaml
        Objective Caml version 3.11.2

# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads
- : unit = ()

# #require "extlib";;
/usr/lib64/ocaml/extlib: added to search path
/usr/lib64/ocaml/extlib/extLib.cma: loaded

# type t = A | B | C of int | D of int ;;
type t = A | B | C of int | D of int
# Std.dump A ;;
- : string = "0"                    # Stored as the OCaml int 0
# Std.dump B ;;
- : string = "1"                    # Stored as the OCaml int 1
# Std.dump (C 42) ;;
- : string = "(42)"                 # One-element block, tag 0
# Std.dump (D 42) ;;
- : string = "Tag1 (42)"            # One-element block, tag 1

# Std.dump ;;
- : 'a -> string = <fun>
# Std.dump `Foo ;;
- : string = "3505894"              # OCaml int, the hash of string "Foo"
# Std.dump (`Foo 1) ;;
- : string = "(3505894, 1)"         # Two-element block, tag 0
# Std.dump (`Foo (1, 2)) ;;
- : string = "(3505894, (1, 2))"    # Two-elem block, second field is
                                    # another block.

# Std.dump None ;;
- : string = "0"
# Std.dump (Some "foo") ;;
- : string = "(\"foo\")"

Rich.

-- 
Richard Jones
Red Hat


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

* Re: [Caml-list] Re: OCaml/C variant representation
  2010-02-24 16:42 ` Sylvain Le Gall
@ 2010-02-28 15:16   ` Jianzhou Zhao
  2010-02-28 16:22     ` Sylvain Le Gall
  0 siblings, 1 reply; 5+ messages in thread
From: Jianzhou Zhao @ 2010-02-28 15:16 UTC (permalink / raw)
  To: Sylvain Le Gall; +Cc: caml-list

On Wed, Feb 24, 2010 at 11:42 AM, Sylvain Le Gall <sylvain@le-gall.net> wrote:
> Hello,
>
> On 24-02-2010, Jean Yang <jeanyang@csail.mit.edu> wrote:
>> Hello,
>>
>>   I'm having some trouble with variant representation in the OCaml/C
>> interface.
>>
>>   According to the reference I found (
>> http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html), it seems like if
>> we have value v = variant VConstr(val), where val is some integer value,
>> should have Field(val, 0) == hash_variant("VConstr") and Int_val(Field(v, 0)
>>== val.  I don't seem to be understanding this correctly, since I am getting
>> Int_val(Field(v, 0)) == val.
>>
>>   I have the following datatype declaration in my OCaml:
>> type newty = VConstr of int | ...
>> In the C implementation of OCaml function foo : newty -> int, I have
>> something like this;
>>   CAMLprim value foo (value v) {  ... }
>> In the body of this function I am looking at the fields of val.  Am I doing
>> something wrong here?
>>
>
> Please have a look at:
> http://caml.inria.fr/mantis/view.php?id=4803
>
> This section is misleading. You should use hash_variant for `VConstr.
>
> In your case VConstr of int will be Block with size = 1 and tag = 0;
> first non-constant constructor and first field contains Value_int(...)

If I am creating an OCaml variant (string option) from C, say using 'alloc',
does it matter which tag I am using for 'some'? Can it be only tag 0?
I was looking at LLVM OCamling bindings, sometimes the 'some' is
also of 'tag 1', which confused me.

If my variant is A | B | C of int | D of int, does C have to start from tag 0?
and D must be 1? Similarly, does A have to be assigned into Val_int(0),
and B is from 1?

Thanks.

Zjz

>
> Regards
> Sylvain Le Gall
>
>
> _______________________________________________
> 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] 5+ messages in thread

* Re: OCaml/C variant representation
  2010-02-28 15:16   ` [Caml-list] " Jianzhou Zhao
@ 2010-02-28 16:22     ` Sylvain Le Gall
  0 siblings, 0 replies; 5+ messages in thread
From: Sylvain Le Gall @ 2010-02-28 16:22 UTC (permalink / raw)
  To: caml-list

On 28-02-2010, Jianzhou Zhao <jianzhou@seas.upenn.edu> wrote:
> On Wed, Feb 24, 2010 at 11:42 AM, Sylvain Le Gall <sylvain@le-gall.net> wrote:
>> On 24-02-2010, Jean Yang <jeanyang@csail.mit.edu> wrote:
>> Please have a look at:
>> http://caml.inria.fr/mantis/view.php?id=4803
>>
>> This section is misleading. You should use hash_variant for `VConstr.
>>
>> In your case VConstr of int will be Block with size = 1 and tag = 0;
>> first non-constant constructor and first field contains Value_int(...)
>
> If I am creating an OCaml variant (string option) from C, say using 'alloc',
> does it matter which tag I am using for 'some'? Can it be only tag 0?
> I was looking at LLVM OCamling bindings, sometimes the 'some' is
> also of 'tag 1', which confused me.

I think it is an error, but since there is only one possibility in the
case of Some, maybe it is not important (i.e. you can distingish between
the two variant None | Some just looking at the fact there are block or
not).

> If my variant is A | B | C of int | D of int, does C have to start from tag 0?
> and D must be 1? Similarly, does A have to be assigned into Val_int(0),
> and B is from 1?

Yes. At least this is what I understand from section "18.3.4 Concrete
types" of the OCaml manual. As a matter of fact, I never had problems
following this convention before. 

Regards,
Sylvain Le Gall


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

end of thread, other threads:[~2010-02-28 16:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-24 15:26 OCaml/C variant representation Jean Yang
2010-02-24 16:42 ` Sylvain Le Gall
2010-02-28 15:16   ` [Caml-list] " Jianzhou Zhao
2010-02-28 16:22     ` Sylvain Le Gall
2010-02-24 23:07 ` [Caml-list] " Richard Jones

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