From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 7F4C0BC57 for ; Mon, 7 Jun 2010 20:46:00 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkQCAMnbDEzRVaE0mGdsb2JhbACSD4wJCBUBAQEBAQgJDAcRIrAEggGFTi6ITwEBAwWFEgSODQ X-IronPort-AV: E=Sophos;i="4.53,379,1272837600"; d="scan'208";a="64130976" Received: from mail-fx0-f52.google.com ([209.85.161.52]) by mail4-smtp-sop.national.inria.fr with ESMTP; 07 Jun 2010 20:45:53 +0200 Received: by fxm19 with SMTP id 19so1965048fxm.39 for ; Mon, 07 Jun 2010 11:45:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:sender:received :in-reply-to:references:from:date:x-google-sender-auth:message-id :subject:to:cc:content-type; bh=gJyi2m+4X5QMe5bj3Y+Ey3zJp4iAji3fNakND1eK+mQ=; b=rEskfNp1AZv1hcAKhLt1t0SZAFhmJdKRVeBCsJQfZFhvTVYUvhhe4xGkHGsBGmV9YL d6MHAxQokx12unrRrlgJ6RI1uzHAHhpeBdnzPGp7Z0Uq8D004swNc5Up/ZpasVzgN2Kg brn6vJXcuR9yzud2iTFUuBSARqfUeKc1O3Avw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; b=p85Gr9U87isti+t7GPD09hAtGfDwb2ymweafxX2mlOYqpsF+ZYcruT/0B/EfL8My+N C7I+c0KulbUxGP+TUyCeKDD70tg6mHDVgf7ntYQrH93DCOIzW5GwcmxPz36gON3NVV5u 3/7qyBjId176HcXOMe3WZ+9bZGRhLjhLx/jEE= Received: by 10.204.83.198 with SMTP id g6mr7051662bkl.104.1275936352240; Mon, 07 Jun 2010 11:45:52 -0700 (PDT) MIME-Version: 1.0 Sender: gabriel.scherer@gmail.com Received: by 10.204.112.14 with HTTP; Mon, 7 Jun 2010 11:45:32 -0700 (PDT) In-Reply-To: <87ocfm7l8s.fsf@gmail.com> References: <4C0D3557.1020200@gmail.com> <87ocfm7l8s.fsf@gmail.com> From: bluestorm Date: Mon, 7 Jun 2010 20:45:32 +0200 X-Google-Sender-Auth: 57Y6PEvfDKcSunTUNrbXEGEDNmA Message-ID: Subject: Re: [Caml-list] Converting variants with only constant constructors to integers To: W Dan Meyer Cc: =?ISO-8859-1?Q?T=F6r=F6k_Edwin?= , caml-list@yquem.inria.fr Content-Type: multipart/alternative; boundary=0016e6da934a08077104887515d2 X-Spam: no; 0.00; variants:01 constructors:01 integers:01 camlp:01 ocaml:01 ocaml:01 camlp:01 enum:01 bool:01 failwith:01 enum:01 bool:01 failwith:01 wrote:01 wrote:01 --0016e6da934a08077104887515d2 Content-Type: text/plain; charset=ISO-8859-1 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" --0016e6da934a08077104887515d2 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 2010/6/7 W Dan Meyer <w= ojciech.meyer@googlemail.com>
> 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<= /a> ) generator for just that a few years ago :
=A0=A0
http://bluestorm.info/camlp4= /ty_enum_to_int.ml.html

Example use :

=A0 type test =3D | A | B | C | D of bool with to_= int


Generated functions :

=A0 let test_to_int =3D functio= n | A -> 0 | B -> 1 | C -> 2 | D _ -> 3
=A0
=A0 let test_= of_int =3D function
=A0 | 0 -> A
=A0 | 1 -> B
=A0 | 2 -> C
=A0 | 3 -> fail= with "can't convert to a constructor with parameters: D"
= =A0 | _ -> invalid_argument "test_of_int"

--0016e6da934a08077104887515d2--