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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id C90A7BC37 for ; Fri, 12 Feb 2010 08:33:36 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AsMBAIKSdEvRVdvdkGdsb2JhbACFdIkni14IFQEBAQEJCQwHEwMgsUOEV4hyAQEDBYJPDguBawSMCA X-IronPort-AV: E=Sophos;i="4.49,458,1262559600"; d="scan'208,217";a="56916451" Received: from mail-ew0-f221.google.com ([209.85.219.221]) by mail4-smtp-sop.national.inria.fr with ESMTP; 12 Feb 2010 08:33:36 +0100 Received: by ewy21 with SMTP id 21so2492785ewy.22 for ; Thu, 11 Feb 2010 23:33:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:sender:subject:mime-version :content-type:from:in-reply-to:date:cc:message-id:references:to :x-mailer; bh=umrDbV88QYQIBCdYpFvhjN03WQ2k9bTuFatH1yCEjEI=; b=hrPnWzz0NAQGwBGxz+mU4Lfu6nqL/RC7RHSJx6R6ETcmPBP9rf46YR5c8jPX4eMRfC rFrE8SVfHUo3GyBjE/gkl0JR3SBV8H13py5x1oeSsDVs9FPYd5l2xFMxXOO7G911iwdL oD2m7XrtUiWvyqh3CQlNl0ljah0kNgdmxvXYQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:subject:mime-version:content-type:from:in-reply-to:date:cc :message-id:references:to:x-mailer; b=G5Nf3Y3pmH8jXp08a4zII1D/KwWU6jkV9Uz/BXL+Pp/lmjsAypQIZgcgSXbbj4hB7u Qeubn4D4QM0MxL7sFcFc59Ah3zWOGzyF/3grMiyeAdvmrD/jO8EZ/l0mCcSz+d4CShz2 DGWYvPf07EM4BYtLWqEewHr7YcPqBLOGpas3I= Received: by 10.213.41.203 with SMTP id p11mr675383ebe.75.1265960015821; Thu, 11 Feb 2010 23:33:35 -0800 (PST) Received: from ?192.168.0.12? (dau94-10-88-189-211-192.fbx.proxad.net [88.189.211.192]) by mx.google.com with ESMTPS id 13sm2110070ewy.1.2010.02.11.23.33.34 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 11 Feb 2010 23:33:35 -0800 (PST) Sender: David Teller Subject: Re: [Caml-list] define incompatible type Mime-Version: 1.0 (Apple Message framework v1077) Content-Type: multipart/alternative; boundary=Apple-Mail-2--109994127 From: David Rajchenbach-Teller In-Reply-To: <1ae8fe881002112232v73a57e97le63cfd48ffac6d48@mail.gmail.com> Date: Fri, 12 Feb 2010 08:33:34 +0100 Cc: caml-list Message-Id: <8D169DE8-B24E-4A80-8D2D-3743B6EF9A7A@univ-orleans.fr> References: <1ae8fe881002112232v73a57e97le63cfd48ffac6d48@mail.gmail.com> To: =?iso-8859-1?Q?Gr=E9goire_Seux?= X-Mailer: Apple Mail (2.1077) X-Spam: no; 0.00; univ-orleans:01 gregoire:01 ocaml:01 integers:01 struct:01 sig:01 val:01 ocaml:01 coercions:01 coercions:01 gregoire:01 checker:01 checker:01 beginner's:01 bug:01 --Apple-Mail-2--109994127 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=iso-8859-1 Hi Gr=E9goire, It'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. type user_id =3D User of int type movie_id =3D Movie of int let a =3D User 57 and b =3D Movie 80 in if a =3D b then ... This is the technique often used by Haskellites. Variant on the topic: = use singleton records instead of singleton sums. The second one is to make use of modules and abstract types, e.g. module User =3D struct type id =3D int let id_of_int x =3D x ... end : sig type id (*Abstract type*) val id_of_int : int -> id end (*same for b*) let a =3D User.id_of_int 57 and b =3D User.id_of_int 80 in if a =3D b then ... This is probably the most common technique in OCaml, as it fits well = with functorization. Finally, you can use a phantom type, e.g. type 'a id =3D {id: int} (*Type argument used only to differentiate = between various kinds of ids*) type user (*This type has no inhabitant, don't worry, = it's only for coercions*) type movie (*same here*) let a =3D {id:57} : user id and b =3D {id:80}: movie id in if a =3D b then ... It's an elegant technique, which I personally like, but which can = sometimes cause puzzling error messages if you forget coercions. I hope this helps, Regards, David On Feb 12, 2010, at 7:32 AM, Gr=E9goire Seux wrote: > hello ! >=20 > i would like to create two types and use the type checker to verify = the "meaning" of my programs: >=20 > type user_id =3D int > type movie_id =3D int >=20 > i'd like if the type checker would warn me if i write something that = is non-sense: > let a:user_id =3D 57 and b:movie_id =3D 80 in > if a=3Db then ... > =20 > because this is obvioulsy a mistake >=20 > do you know if is this possible ? > thanks by advance ! >=20 >=20 > --=20 > Gr=E9goire > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs --Apple-Mail-2--109994127 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=iso-8859-1
hello = !

i would like to create two types and use the type = checker to verify the "meaning" of my = programs:

type user_id =3D int
type movie_id =3D int

i'd like if the = type checker would warn me if i write something that is = non-sense:
let a:user_id =3D 57 and b:movie_id =3D 80 in
if a=3Db then ...
 
because this is = obvioulsy a mistake

do you know if is this = possible ?
thanks by advance = !


--
Gr=E9goire
_______________________________________________
Caml-list mailing = list. Subscription management:
http://y= quem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's = list: http://groups.yahoo= .com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-= bugs

= --Apple-Mail-2--109994127--