From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id DAA11198; Tue, 3 Jul 2001 03:02:37 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id DAA11185 for ; Tue, 3 Jul 2001 03:02:35 +0200 (MET DST) Received: from www.invert.com (www.invert.com [209.164.21.15]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f6312Xj19449 for ; Tue, 3 Jul 2001 03:02:34 +0200 (MET DST) Received: (from miles@localhost) by www.invert.com (8.10.1/8.10.1AA) id f6312Wc51897 for caml-list@inria.fr; Mon, 2 Jul 2001 18:02:32 -0700 (PDT) (envelope-from miles) Date: Mon, 2 Jul 2001 18:02:32 -0700 From: Miles Egan To: caml-list@inria.fr Subject: [Caml-list] hash_variant anomalies Message-ID: <20010702180232.A51837@caddr.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk The manual states: Like constructed terms, values of variant types are represented either as integers (for variants without arguments), or as blocks (for variants with an argument). Unlike constructed terms, variant constructors are not numbered starting from 0, but identified by a hash value (a Caml integer), as computed by the C function hash_variant (declared in ): the hash value for a variant constructor named, say, VConstr is hash_value("VConstr"). The variant value `VConstr is represented by hash_value("VConstr"). The variant value `VConstr(v) is represented by a block of size 2 and tag 0, with field number 0 containing hash_value("VConstr") and field number 1 containing v. This test program seems to indicate otherwise: *** test.c *** ------------------------------------------------------------------ #include #include void c_calc_variant(value variant) { printf( "variant '%s' gives %d\n", String_val(variant), hash_variant(String_val(variant))); } void c_print_variant(value variant) { printf("variant is %d\n", variant); } *** test.ml *** ------------------------------------------------------------------ type t = YES | NO | MAYBE type p = [`YES | `NO | `MAYBE] external calc_variant: string -> unit = "c_calc_variant" external print_variant: t -> unit = "c_print_variant" external print_pvariant: p -> unit = "c_print_variant" let _ = calc_variant "YES"; print_variant YES; calc_variant "NO"; print_variant NO; calc_variant "MAYBE"; print_variant MAYBE; calc_variant "`YES"; print_pvariant `YES; calc_variant "`NO"; print_pvariant `NO; calc_variant "`MAYBE"; print_variant MAYBE when compiled (ocaml -c test.c; ocamlc -o test -custom test.o test.ml) gives this: variant 'YES' gives 8882703 variant is 1 variant 'NO' gives 34947 variant is 3 variant 'MAYBE' gives 36345617 variant is 5 variant '`YES' gives 2138079567 variant is 8882703 variant '`NO' gives 9582915 variant is 34947 variant '`MAYBE' gives -961552815 variant is 5 It seems that the polymorphic variants are indeed calculated with hash_variant. The plain variants, however, are numbered incrementally starting at 1 and stepping by two, which seems to contradict the manual. Am I reading the manual incorrectly? Also the manual states that both functions "hash_value" and "hash_variant" are used to compute the value of variants, but mlvalues.h seems to declare only "hash_variant". Is there a "hash_value" function? If so, where is it declared? Last, the varcc utility supplied with lablgtk is quite handy when writing bindings for c libraries with a lot of c constants. Is there any prospect of including it in the ocaml distribution? -- miles ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr