caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] hash_variant anomalies
@ 2001-07-03  1:02 Miles Egan
  2001-07-03  2:18 ` Chris Quinn
  2001-07-03  7:42 ` Xavier Leroy
  0 siblings, 2 replies; 6+ messages in thread
From: Miles Egan @ 2001-07-03  1:02 UTC (permalink / raw)
  To: caml-list

The manual states:

<quote>
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 <caml/mlvalues.h>): 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.
</quote>

This test program seems to indicate otherwise:

*** test.c ***
------------------------------------------------------------------
#include <stdio.h>
#include <caml/mlvalues.h>

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:

<quote>
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
</quote>

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


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

end of thread, other threads:[~2001-07-05  5:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-03  1:02 [Caml-list] hash_variant anomalies Miles Egan
2001-07-03  2:18 ` Chris Quinn
2001-07-03  7:42 ` Xavier Leroy
2001-07-03 15:15   ` Miles Egan
2001-07-04  0:19     ` Jacques Garrigue
2001-07-05  5:58       ` Miles Egan

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