From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 18CF8BC48 for ; Fri, 25 Mar 2005 12:37:06 +0100 (CET) Received: from ext.lri.fr (ext.lri.fr [129.175.15.4]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j2PBb5v0029304 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Fri, 25 Mar 2005 12:37:05 +0100 Received: from localhost (localhost [127.0.0.1]) by ext.lri.fr (Postfix) with ESMTP id 6B06E19E7AA; Fri, 25 Mar 2005 12:37:05 +0100 (CET) Received: from ext.lri.fr ([127.0.0.1]) by localhost (ext.lri.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 11823-03; Fri, 25 Mar 2005 12:37:05 +0100 (CET) Received: from smtp.lri.fr (serveur3-5 [129.175.3.5]) by ext.lri.fr (Postfix) with ESMTP id 584DE19E7A7; Fri, 25 Mar 2005 12:37:05 +0100 (CET) Received: from pc8-142 (pc9-152 [129.175.9.152]) by smtp.lri.fr (Postfix) with ESMTP id 9AF62CED99; Fri, 25 Mar 2005 12:37:04 +0100 (CET) Received: from filliatr by pc8-142 with local (Exim 3.36 #1 (Debian)) id 1DEn7g-0007we-00; Fri, 25 Mar 2005 12:37:04 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <16963.63456.885441.571929@gargle.gargle.HOWL> Date: Fri, 25 Mar 2005 12:37:04 +0100 To: Hal Daume III Cc: Caml Mailing List Subject: Re: [Caml-list] generic data type -> int function In-Reply-To: References: X-Mailer: VM 7.18 under Emacs 21.3.1 From: Jean-Christophe Filliatre X-Virus-Scanned: by amavisd-new at lri.fr X-Miltered: at concorde with ID 4243F7E1.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 filliatre:01 filliatre:01 lri:01 integers:01 constructors:01 integers:01 constructors:01 ...:98 integer:01 functions:01 int:01 int:01 data:02 data:02 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: Hi, > 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). I don't think there such a built-in function. But using Obj.magic to convert constant constructors to integers is safe (the constant constructors of a type are represented by integers starting from 0): ====================================================================== # type t = A|B|C|D;; type t = A | B | C | D # (Obj.magic A : int);; - : int = 0 # (Obj.magic D : int);; - : int = 3 ====================================================================== Going the way back obviously requires a dynamic check (the integer needs to be within the right bounds). Note that I do not encourage the use of Obj.magic. I even think that writing your own function to convert constructors to integers will be equally fast (since pattern-matching is compiled using a constant time lookup table in this case); and you can macro-generate such functions. Hope this helps, -- Jean-Christophe