From: Miles Egan <miles@caddr.com>
To: caml-list@inria.fr
Subject: [Caml-list] hash_variant anomalies
Date: Mon, 2 Jul 2001 18:02:32 -0700 [thread overview]
Message-ID: <20010702180232.A51837@caddr.com> (raw)
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
next reply other threads:[~2001-07-03 1:02 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-07-03 1:02 Miles Egan [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20010702180232.A51837@caddr.com \
--to=miles@caddr.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox