caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Rajchenbach-Teller <David.Teller@univ-orleans.fr>
To: "Grégoire Seux" <kamaradclimber@gmail.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] define incompatible type
Date: Fri, 12 Feb 2010 08:33:34 +0100	[thread overview]
Message-ID: <8D169DE8-B24E-4A80-8D2D-3743B6EF9A7A@univ-orleans.fr> (raw)
In-Reply-To: <1ae8fe881002112232v73a57e97le63cfd48ffac6d48@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2249 bytes --]

     Hi Grégoire,
 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 = User of int
   type movie_id = Movie of int

   let a = User 57 and b = Movie 80 in
   if a = 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 =
  struct
     type id = int
     let id_of_int x = x
   ...
  end :
  sig
     type id (*Abstract type*)
     val id_of_int : int -> id
  end
  (*same for b*)
  let a = User.id_of_int 57 and b = User.id_of_int 80 in
  if a = 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 = {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 = {id:57} : user id and b = {id:80}: movie id in
  if a = 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égoire Seux wrote:

> hello !
> 
> i would like to create two types and use the type checker to verify the "meaning" of my programs:
> 
> type user_id = int
> type movie_id = int
> 
> i'd like if the type checker would warn me if i write something that is non-sense:
> let a:user_id = 57 and b:movie_id = 80 in
> if a=b then ...
>  
> because this is obvioulsy a mistake
> 
> do you know if is this possible ?
> thanks by advance !
> 
> 
> -- 
> Grégoire
> _______________________________________________
> 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


[-- Attachment #2: Type: text/html, Size: 4594 bytes --]

  reply	other threads:[~2010-02-12  7:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-12  6:32 Grégoire Seux
2010-02-12  7:33 ` David Rajchenbach-Teller [this message]
2010-02-12  7:43   ` [Caml-list] " Grégoire Seux
2010-02-12 10:59   ` David Allsopp
2010-02-12 20:26     ` Guillaume Yziquel
2010-02-12  9:25 abau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8D169DE8-B24E-4A80-8D2D-3743B6EF9A7A@univ-orleans.fr \
    --to=david.teller@univ-orleans.fr \
    --cc=caml-list@inria.fr \
    --cc=kamaradclimber@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).