From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 795F0BC57 for ; Mon, 7 Jun 2010 21:46:20 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkECAJ3pDEzRVaE0mGdsb2JhbACeFAgVAQEBAQEICQwHESKvfYIBhVguiE8BAQMFglSCPgSDRQ X-IronPort-AV: E=Sophos;i="4.53,380,1272837600"; d="scan'208";a="51926442" Received: from mail-fx0-f52.google.com ([209.85.161.52]) by mail3-smtp-sop.national.inria.fr with ESMTP; 07 Jun 2010 21:46:19 +0200 Received: by fxm19 with SMTP id 19so2017206fxm.39 for ; Mon, 07 Jun 2010 12:46:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :x-enigmail-version:openpgp:content-type:content-transfer-encoding; bh=4lCQq73JXONnBbHiD56DOQ4/7zTY/wi7qpUFVVzyiPU=; b=eN1naWwnZYdo1zdEIjXFDxCLsC8hyv/FD7K91Ks9dXDpXUXBlwhQWqtvf60F+zXn3r 4k+lsH8iI0BOaPcHRFpQSg+fuP6xMrGAUIFBgQ8Sv0oOmU8uHknwVKkPSad+VaenUD3n a7/jo6aEfJ6I51z+rTk6eRdDQAkgrUZoa3Yko= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:x-enigmail-version:openpgp:content-type :content-transfer-encoding; b=VkTT/RhsMVDe05lshZXqOMvppBO3TXfYiGuSwsqxx/RXgFMU0bOPz0SCqzrjTxNDiq Owv5qElvejUjhjK6sOSFBpGKqPBFI/Af4u8IiHVpht6eMkPYEo/VFFDhqGDkgiIcdLsF VTcGxC50dSrQZrWeg+tNEjlPIzCtwqRER+diM= Received: by 10.223.101.4 with SMTP id a4mr237425fao.71.1275939978684; Mon, 07 Jun 2010 12:46:18 -0700 (PDT) Received: from debian ([79.114.32.39]) by mx.google.com with ESMTPS id 15sm21712051fad.10.2010.06.07.12.46.17 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 07 Jun 2010 12:46:17 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) by debian (Postfix) with ESMTP id B8800B3103; Mon, 7 Jun 2010 22:46:16 +0300 (EEST) Message-ID: <4C0D4C88.4070102@gmail.com> Date: Mon, 07 Jun 2010 22:46:16 +0300 From: =?ISO-8859-1?Q?T=F6r=F6k_Edwin?= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100515 Icedove/3.0.4 MIME-Version: 1.0 To: David Allsopp , bluestorm Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Converting variants with only constant constructors to integers References: <4C0D3557.1020200@gmail.com> <010d01cb0672$09f3eb90$1ddbc2b0$@romulus.metastack.com> In-Reply-To: <010d01cb0672$09f3eb90$1ddbc2b0$@romulus.metastack.com> X-Enigmail-Version: 1.0.1 OpenPGP: id=5379965D Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; variants:01 constructors:01 integers:01 constructors:01 foo:01 foo:01 ocaml:01 variants:01 ocaml:01 bindings:01 camlp:01 camlp:01 enum:01 bool:01 failwith:01 On 06/07/2010 09:48 PM, David Allsopp wrote: > Török Edwin wrote: >> What is the recommended way to convert a variant that has only constant >> constructors to an integer? (an integer that is unique for each constant >> constructor, preferably sequential in the order in which they are >> declared). > > type foo = A | B;; > external int_of_foo : foo -> int = "%identity";; > > Which is another way of saying Obj.magic - but %identity is an OCaml > primitive, where Obj.magic is part of a restricted module so there is a > small semantic difference in terms of future support. The internal > representation of variants won't change until at least OCaml 4.0.0 - > there're simply far too many C bindings out there! foo_of_int obviously > can't be done quite so trivially written although having checked that the > int represents a valid constructor you can then use %identity in the same > way. Thanks, that "%identity" looks exactly what I wanted. I don't need a foo_of_int right now. On 06/07/2010 09:45 PM, bluestorm wrote: > 2010/6/7 W Dan Meyer > >> I can imagine you could use an association list, function with pattern >> matching, code generation tool (based on CamlP4), etc. > > I wrote a type-conv ( > http://www.ocaml.info/home/ocaml_sources.html#type-conv ) generator for > just that a few years ago : > http://bluestorm.info/camlp4/ty_enum_to_int.ml.html > > Example use : > > type test = | A | B | C | D of bool with to_int > > > Generated functions : > > let test_to_int = function | A -> 0 | B -> 1 | C -> 2 | D _ -> 3 > > let test_of_int = function > | 0 -> A > | 1 -> B > | 2 -> C > | 3 -> failwith "can't convert to a constructor with parameters: D" > | _ -> invalid_argument "test_of_int" > If I'll need a foo_of_int this sounds like a good solution, thanks. Best regards, --Edwin