From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr 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 089D1BC37 for ; Fri, 12 Feb 2010 11:59:18 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgsBAE/DdEtRZ90vkWdsb2JhbACbAhUBAQEBCQsKBxMDIL4agkaCEgQ X-IronPort-AV: E=Sophos;i="4.49,459,1262559600"; d="scan'208";a="44716164" Received: from mtaout01-winn.ispmail.ntl.com ([81.103.221.47]) by mail3-smtp-sop.national.inria.fr with ESMTP; 12 Feb 2010 11:59:17 +0100 Received: from aamtaout04-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 <20100212105916.HPQQ4204.mtaout01-winn.ispmail.ntl.com@aamtaout04-winn.ispmail.ntl.com>; Fri, 12 Feb 2010 10:59:16 +0000 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout04-winn.ispmail.ntl.com (InterMail vG.2.02.00.01 201-2161-120-102-20060912) with ESMTP id <20100212105916.CFIY22934.aamtaout04-winn.ispmail.ntl.com@romulus.metastack.com>; Fri, 12 Feb 2010 10:59:16 +0000 Received: from Tenor ([172.16.0.8]) (authenticated bits=0) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id o1CAx2ua028655 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Fri, 12 Feb 2010 10:59:03 GMT From: "David Allsopp" To: "'David Rajchenbach-Teller'" , "=?iso-8859-1?Q?'Gr=E9goire_Seux'?=" Cc: "'caml-list'" References: <1ae8fe881002112232v73a57e97le63cfd48ffac6d48@mail.gmail.com> <8D169DE8-B24E-4A80-8D2D-3743B6EF9A7A@univ-orleans.fr> In-Reply-To: <8D169DE8-B24E-4A80-8D2D-3743B6EF9A7A@univ-orleans.fr> Subject: RE: [Caml-list] define incompatible type Date: Fri, 12 Feb 2010 10:59:02 -0000 Message-ID: <00b001caabd2$63c57fd0$2b507f70$@romulus.metastack.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQF3qk+9aAUjr7aqiP+QYKhr1BT9GgGkvJhNAiLzdBQ= 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=ZtHxNT4mZm3rCuM0SmWmgWxeBwJsziC8EqOrwwVkrhA= c=1 sm=0 a=G-aqcv-2qP0A:10 a=sQLUUKt_ORiZEubXrg8A:9 a=m940GeD0mstIYVDMGoh9IGKV_28A:4 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 X-Spam: no; 0.00; gregoire:01 ocaml:01 integers:01 ocaml:01 integers:01 compiler:01 sig:01 val:01 struct:01 eliminates:98 wrote:01 incompatible:01 caml-list:01 constructor:01 coercion:01 David Rajchenbach-Teller wrote: > =A0=A0 =A0 Hi Gr=E9goire, >=A0It's not directly possible in OCaml, but there are at least three = methods for doing what you > want. > > The first one is to wrap your integers behind a constructor, e.g. You can also use (post OCaml 3.11.0) private types if you want to be = able to use the ID values as integers but only explicitly. While I'm sure that = the ocaml compiler eliminates calls to the identity function, I like the elegance that the conversion from id to int is a type coercion instead = of a function call. In real world uses, chances are that of_int is a useful function doing actual work and not the identity function! module User : sig type id =3D private int val of_int : int -> id end =3D struct type id =3D int let of_int x =3D x end;; let a =3D User.of_int 57 and b =3D User.of_int 57;; a =3D b;; (* true *) a =3D 57;; (* type error *) (a :> int) =3D 57;; (* true *) David