Version française
Home     About     Download     Resources     Contact us    
Browse thread
generic data type -> int function
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Oliver Bandel <oliver@f...>
Subject: Re: [Caml-list] generic data type -> int function
On Fri, Mar 25, 2005 at 08:15:59PM +0100, Kim Nguyen wrote:
> Le jeudi 24 mars 2005 à 08:38 -0800, Hal Daume III a écrit :
> > Hi all --
> > 
> > Is there a straightforward way (or a built in function, or...) to 
> > automatically map an enumerated data type to integers (and back, if 
> > possible, but that's not strictly necessary).  In particular, I need 
> > something like:
> 
> Hi,
> 	you can use the polymorphic function : Hashtbl.hash_param
> 	which happens to map constructors to their internal tag.
> 	You should be aware that this is only a (cool) side-effect of 	
> 	the current implementation and could change in the future.
> 	This is a bit of a hack but prevents you from using Obj.magic
> 	or automatically generating pattern matching  (which you should
> 	regenerate every time you change the type).
> 
> 
> # type t = A | B | C | D of int | E of string | G;;
> type t = A | B | C | D of int | E of string | G
> 
> # let to_int x = Hashtbl.hash_param 1 1 x;;
> val to_int : 'a -> int = <fun>

Well, I'm not clear what this function computes.
I never used Hashtbl.hash_param and I do not understand the
descrption.... but:

> 
> # to_int A;; (* first constant constructor *)
> - : int = 0
> 
> # to_int B;;
> - : int = 1
> 
> # to_int C;;
> - : int = 2
> 
> # to_int (D(42));; (* first non-constant constructor *)
> - : int = 0
> 
> # to_int (E("foo"));;
> - : int = 1
> 
> # to_int G;;
> - : int = 3	


It seems to me that your solution does not create the result that was
asked for.

The value 0 and 1 in your above example are more than once
the output of the function to_int.

But as I understand it, there should never be one integer-output
more than once!

IMHO a function is searched, that creates something like a
coding table, as sociologists often do coding their questions/answers
into integers (because of a lack of good software that does this automatically;
maybe today the software is better, but it seems many sociologists
do such integer-coding tables nevertheless habitually ;-)).

But when you have an integer as an output more than once,
you go into big trouble then...


So, "to_int A;;" and "to_int (D(42));;" must not have the same output value!!!

Ciao,
   Oliver