Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Representation of different polymorphic variants guaranteed to be different?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-07-08 (18:37)
From: Bruno Daniel <bruno.daniel@g...>
Subject: Representation of different polymorphic variants guaranteed to be different?
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/ 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