caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Representation of different polymorphic variants guaranteed to be different?
@ 2009-07-08 18:35 Bruno Daniel
  2009-07-08 18:54 ` [Caml-list] " Eric Cooper
  0 siblings, 1 reply; 5+ messages in thread
From: Bruno Daniel @ 2009-07-08 18:35 UTC (permalink / raw)
  To: caml-list

Dear OCaml developers,

I'm trying to understand the representation of polymorphic variants in OCaml
and I'm a bit worried about possible problems. Section 18.3.6 of the
OCaml 3.11 reference manual reads:

"[...] The variant value `VConstr is represented by hash_variant("VConstr").
The variant value `VConstr(v) is represented by a block of size 2 and tag 0,
with field number 0 containing hash_variant("VConstr") and field number 1
containing v."

I take it from the source code of caml_hash_variant in
ocaml-3.11.1_src/asmrun/hash.c and hash_variant in
ocaml-3.11.1_src/typing/btype.ml that hash_variant returns an OCaml 31-bit
integer value, i.e. there is only room for at most 2147483648 different
polymorphic variants, but even with identifiers composed of only 7 characters
chosen from 26 letters one could easily construct  8031810176 different
polymorphic variant constructors.

How is it ensured that I get a <> b for a and b created as polymorphic
variants from two different identifiers? Will pattern matching give wrong
results if I accidentally choose two different identifiers translated to the
same internal representation?

The same applies to method names according to section 18.3.5:
"Since public method tags are hashed in the same way as variant tags, and
methods are functions taking self as first argument, if you want to do the method
call foo#bar from the C side, you should call:
callback(caml_get_public_method(foo, hash_variant("bar")), foo);".

I apologize if these questions have been answered before. I couldn't find any
discussion about them in the archives. Thank you very much for your help.

Best regards
  Bruno Daniel


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

end of thread, other threads:[~2009-07-08 19:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-08 18:35 Representation of different polymorphic variants guaranteed to be different? Bruno Daniel
2009-07-08 18:54 ` [Caml-list] " Eric Cooper
2009-07-08 19:32   ` Elnatan Reisner
2009-07-08 19:32   ` Bruno Daniel
2009-07-08 19:36   ` Frédéric van der Plancke

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