Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Bruno Daniel <bruno.daniel@g...>
Subject: Re: [Caml-list] Representation of different polymorphic variants guaranteed to be different?
Dear Eric Cooper,

thank you very much for your answer. I tried it out and I found that it really
works, even with the variants defined in different ml-files linked together:

--- module1.ml ---
let a1 = if !Sys.interactive then `a else `zyctRecABC;;
--- module2.ml ---
let a2 = `ABC;;

let _ = a2 = Module1.a1;;
------------------

or

--- module2.ml ---
let a2 = `ABC;;

let h1 = Hashtb.create 100;;

let _ =
  Hashtbl.replace h1 a2 0;
  Hashtbl.replace h1 Module1.a1 1;;
------------------

In both cases I get the following error message when compiling the second module:
"Error: Variant tags `ABC and `zyctRecABC have the same hash value.
Change one of them."

But it's clear from this discussion that I'll never be allowed to use
Obj.magic on variant types. The following goes through unchecked:

--- module2.ml ---
let a2 = `ABC;;

let h1 : (int, int) Hashtbl.t = Hashtbl.create 100;;

let _ =
  Hashtbl.replace h1 (Obj.magic a2) 0;
  Hashtbl.replace h1 (Obj.magic Module1.a1) 1;;
------------------

Best regards
  Bruno Daniel

Eric Cooper wrote:
> On Wed, Jul 08, 2009 at 08:35:27PM +0200, Bruno Daniel wrote:
>> 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?
> 
> See this thread:
>     http://caml.inria.fr/pub/ml-archives/caml-list/2005/03/544288096a47d82ec870d01c8396f5fe.fr.html 
> 
> Short answer: collisions could theoretically occur, but are detected
> at link time.
> 
> -- 
> Eric Cooper             e c c @ c m u . e d u
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs