From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 4DB22BC48 for ; Tue, 29 Mar 2005 09:59:03 +0200 (CEST) Received: from first.in-berlin.de (dialin-145-254-065-196.arcor-ip.net [145.254.65.196]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j2T7wwWS028422 for ; Tue, 29 Mar 2005 09:59:01 +0200 Received: by first.in-berlin.de (Postfix, from userid 501) id 8C2A2C77C6; Tue, 29 Mar 2005 09:29:29 +0200 (CEST) Date: Tue, 29 Mar 2005 09:29:29 +0200 From: Oliver Bandel To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] generic data type -> int function Message-ID: <20050329072929.GB365@first.in-berlin.de> References: <1111778159.3062.21.camel@localhost.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1111778159.3062.21.camel@localhost.localdomain> User-Agent: Mutt/1.5.6i X-Miltered: at nez-perce with ID 42490AC2.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; oliver:01 bandel:01 oliver:01 in-berlin:01 caml-list:01 integers:01 hashtbl:01 hash:01 constructors:01 hashtbl:01 hash:01 val:01 computes:01 foo:01 imho:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.2 X-Spam-Level: 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 = 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