English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] hash_variant anomalies
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-07-03 (01:02)
From: Miles Egan <miles@c...>
Subject: [Caml-list] hash_variant anomalies
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 <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.

This test program seems to indicate otherwise:

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

void c_calc_variant(value variant)
        "variant '%s' gives %d\n", 

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

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

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?

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