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 68D85BC57 for ; Tue, 8 Jun 2010 09:43:11 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmkCACeRDUxRZ90vkWdsb2JhbACeQRUBAQEBCQsKBxEDH71cgliCPgQ X-IronPort-AV: E=Sophos;i="4.53,383,1272837600"; d="scan'208";a="51950737" Received: from mtaout01-winn.ispmail.ntl.com ([81.103.221.47]) by mail3-smtp-sop.national.inria.fr with ESMTP; 08 Jun 2010 09:43:11 +0200 Received: from aamtaout02-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout01-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20100608074307.TWSF3266.mtaout01-winn.ispmail.ntl.com@aamtaout02-winn.ispmail.ntl.com>; Tue, 8 Jun 2010 08:43:07 +0100 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout02-winn.ispmail.ntl.com (InterMail vG.2.02.00.01 201-2161-120-102-20060912) with ESMTP id <20100608074307.RGOK1586.aamtaout02-winn.ispmail.ntl.com@romulus.metastack.com>; Tue, 8 Jun 2010 08:43:07 +0100 Received: from Tenor ([172.16.0.8]) (authenticated bits=0) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id o587gnHQ008262 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Tue, 8 Jun 2010 08:42:51 +0100 From: "David Allsopp" To: "'W Dan Meyer'" , "'bluestorm'" Cc: References: <4C0D3557.1020200@gmail.com> <010d01cb0672$09f3eb90$1ddbc2b0$@romulus.metastack.com> <4C0D4C88.4070102@gmail.com> <877hma78xf.fsf@gmail.com> In-Reply-To: <877hma78xf.fsf@gmail.com> Subject: RE: [Caml-list] Converting variants with only constant constructors to integers Date: Tue, 8 Jun 2010 08:42:49 +0100 Message-ID: <013a01cb06de$32c6d930$98548b90$@romulus.metastack.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQK3OCAoaAveGworypJxQY6jcfYL4QG5R1OKAl4LCCQB8HFXWwG5f4A0AZ1ERFM= Content-Language: en-gb Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.1 cv=W3tOLUehizD4qj6VhtReFuw5MKb8d+XqjIxlDsIazEA= c=1 sm=0 a=fYD0aCV3DZkA:10 a=VZGMvYHv9TIA:10 a=8sW17jYxVu0A:10 a=kj9zAlcOel0A:10 a=pGLkceISAAAA:8 a=A6MtFH_FOEI5Q9Lguc0A:9 a=E3JyD8xBQg1HL8Z7PFfk3PCOb6kA:4 a=CjuIK1q_8ugA:10 a=MSl-tDqOz04A:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 X-Spam: no; 0.00; variants:01 constructors:01 integers:01 foo:01 foo:01 constructors:01 wrote:01 integer:01 caml-list:01 unsafe:01 int:01 int:01 writes:01 variant:02 breaks:02 W Dan Meyer wrote: > bluestorm writes: > > > Beware that "%identity" is an unsafe feature that breaks all safety > > guarantees if used badly. > > Yes %identity is another solution. Although as safe as Obj.magic. It's a bit pedantic but the %identity *primitive* is not the evil thing - what is evil about Obj.magic is that it has an illegal type, namely 'a -> 'b. %identity constrained to a type like foo -> int is a safe use of it (obviously with the side-condition that foo only has constant constructors, as noted - but that's a local check, i.e. in your code, not your callers'). 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) Naturally, any specific use should constrain the type of this function beyond 'a -> int David