From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 00935BC57 for ; Tue, 8 Jun 2010 10:00:15 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnACAJuVDUzRVdY0kWdsb2JhbACeOQgVAQEBAQkLCgcRAx+sRIIChWYuiE8BAQMFglOCPgSODg X-IronPort-AV: E=Sophos;i="4.53,383,1272837600"; d="scan'208";a="60850570" Received: from mail-bw0-f52.google.com ([209.85.214.52]) by mail1-smtp-roc.national.inria.fr with ESMTP; 08 Jun 2010 10:00:15 +0200 Received: by bwz19 with SMTP id 19so1294345bwz.39 for ; Tue, 08 Jun 2010 01:00:15 -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=bMBLCVSfb3wdd9/DdjQEzBmVYIUhHeAZS5/SqmmqqAw=; b=eGwg/gyptk4U/Q8lpJfyCUUiuj6qQLQ9+g0Pd/LNQZdw4kT3XAYENwTwV8oLfUvEfR iJ/UYseg5pDleY37+4BxSkZIcC9Uj7wIpVZjkYwxhfkp7k/t+CFbHbJZP16UbuAqbGsm sFVJOxpXpWE0qPSsQUgw0MYKjJIEer+rPvFt0= 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=nJzqXgJAN1UMCZaKZiQANxWJ/snLp9sCpvdBOg2XgoXpqFDCmINnGFfsQUWh1+Idb2 VO11O1wll7LbO9CnysBom4XEugbzhA8ZDSZ7utgPXwEKp+mJRParb37tXtsobo5tNOLm 8eA3netI3fXn3dEbaL9gVyD1rEHD1YrMIL+fg= Received: by 10.204.154.207 with SMTP id p15mr7473492bkw.178.1275984015193; Tue, 08 Jun 2010 01:00:15 -0700 (PDT) MIME-Version: 1.0 Sender: gabriel.scherer@gmail.com Received: by 10.204.112.14 with HTTP; Tue, 8 Jun 2010 00:59:55 -0700 (PDT) In-Reply-To: <013a01cb06de$32c6d930$98548b90$@romulus.metastack.com> References: <4C0D3557.1020200@gmail.com> <010d01cb0672$09f3eb90$1ddbc2b0$@romulus.metastack.com> <4C0D4C88.4070102@gmail.com> <877hma78xf.fsf@gmail.com> <013a01cb06de$32c6d930$98548b90$@romulus.metastack.com> From: bluestorm Date: Tue, 8 Jun 2010 09:59:55 +0200 X-Google-Sender-Auth: rl5F7JE_uCOCWhQTSbkdC1e0AVM Message-ID: Subject: Re: [Caml-list] Converting variants with only constant constructors to integers To: David Allsopp Cc: W Dan Meyer , caml-list@yquem.inria.fr Content-Type: multipart/alternative; boundary=001517479572f6f8150488802d4c X-Spam: no; 0.00; variants:01 constructors:01 integers:01 constructors:01 restrictive:01 ocaml:01 restrictive:01 ocaml:01 integer:01 integer:01 caml-list:01 int:01 int:01 variant:02 variant:02 --001517479572f6f8150488802d4c Content-Type: text/plain; charset=ISO-8859-1 > > Incidentally, the general function for getting a unique integer for a > variant type with non-constant constructors isn't that bad either: > > let int_of_gen x = > let x = Obj.repr x > in > if Obj.is_block x > then -1 * Obj.tag x - 1 > else (Obj.magic x : int) > I consider that version much better. You could also write it in a more restrictive way : let to_int x = let repr = Obj.repr x in if Obj.is_int repr then (Obj.obj repr : int) else invalid_arg "to_int";; The good point about those is that they check that the memory representation is compatible before casting (while the "%identity" does not). They're safer. Of course, they still break parametricity if used polymorphically, so as you said type should be constrained. I still would prefer the more direct pattern-matching definition : it may require code generation not to be tedious, but the generated code is an honourable OCaml citizen. --001517479572f6f8150488802d4c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Incidentally, th= e general function for getting a unique integer for a
variant type with non-constant constructors isn't that bad either:

let int_of_gen x =3D
=A0let x =3D Obj.repr x
=A0in
=A0 =A0if Obj.is_block x
=A0 =A0then -1 * Obj.tag x - 1
=A0 =A0else (Obj.magic x : int)

I cons= ider that version much better. You could also write it in a more restrictiv= e way :

=A0let to_int x =3D
=A0=A0 let r= epr =3D Obj.repr x in
=A0=A0 if Obj.is_int repr
=A0=A0 then (Obj.obj repr : int)
=A0=A0 else invalid_arg "to_int";;

<= div>The good point about those is that they check that the memory represent= ation is compatible before casting (while=A0the "%identity" does = not). They're safer. Of course, they still break parametricity if used = polymorphically, so as you said type should be constrained.

I still would prefer the more direct pattern-matching d= efinition : it may require code generation not to be tedious, but the gener= ated code is an honourable OCaml citizen.
--001517479572f6f8150488802d4c--