From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id B6BE9BC0B for ; Sat, 6 Jan 2007 03:27:17 +0100 (CET) Received: from out5.smtp.messagingengine.com (out5.smtp.messagingengine.com [66.111.4.29]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l062RGoL021574 for ; Sat, 6 Jan 2007 03:27:17 +0100 Received: from out1.internal (unknown [10.202.2.149]) by out1.messagingengine.com (Postfix) with ESMTP id B5B55648F7; Fri, 5 Jan 2007 21:24:21 -0500 (EST) Received: from heartbeat1.messagingengine.com ([10.202.2.160]) by out1.internal (MEProxy); Fri, 05 Jan 2007 21:24:21 -0500 X-Sasl-enc: PxlZagZw7U6dnkbMRjKKyPUyLukuqNbGqKkNupCIMXz1 1168050261 Received: from adsl-75-20-183-225.dsl.sndg02.sbcglobal.net (adsl-75-20-183-225.dsl.sndg02.sbcglobal.net [75.20.183.225]) by mail.messagingengine.com (Postfix) with ESMTP id D841928B8C; Fri, 5 Jan 2007 21:24:20 -0500 (EST) Date: Fri, 5 Jan 2007 18:27:07 -0800 (PST) From: Martin Jambon X-X-Sender: martin@droopy To: brogoff Cc: Nathaniel Gray , caml-list@yquem.inria.fr Subject: Re: [Caml-list] Question on writing efficient Ocaml. In-Reply-To: Message-ID: References: <200612281626.38328.jon@ffconsultancy.com> <1167326001.5338.0.camel@rosella.wigram> <20061229.150507.32045202.garrigue@math.nagoya-u.ac.jp> <200612291115.kBTBF9E26561@virtutech.se> MIME-Version: 1.0 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-80841990-1168049031=:3347" Content-ID: X-Miltered: at concorde with ID 459F0904.003 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ens-lyon:01 ocaml:01 readable:01 unreadable:01 mime-aware:01 mattias:01 mattias:01 ocaml:01 sig:01 val:01 val:01 struct:01 annotation:01 2007,:98 2007,:98 This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-80841990-1168049031=:3347 Content-Type: TEXT/PLAIN; CHARSET=iso-8859-1; FORMAT=flowed Content-Transfer-Encoding: 8BIT Content-ID: On Fri, 5 Jan 2007, brogoff wrote: > On Fri, 5 Jan 2007, Nathaniel Gray wrote: >> On 12/29/06, Mattias Engdegård wrote: >>> Is there a reason for this? To my innocent eyes, code like >>> >>> type length = Length of int >>> >>> looks quite reasonable and could be useful at times. >> >> I agree. Sadly, the ocaml devs don't. >> >> http://caml.inria.fr/mantis/view.php?id=3978 >> >> As Xavier points out, one can use modules to hide basic types, but >> this is pretty clumsy in practice. There's rumored to be a solution >> using phantom types, but my attempts don't work: > > You need to use modules to make phantom types work. Something like this > > module type BOINK = > sig > type 'a t > val inj : int -> 'a t > val prj : 'a t -> int > val plus : 'a t -> 'a t -> 'a t > end;; > > module Boink : BOINK = > struct > type 'a t = int > let inj n = n > let prj t = t > let plus x y = x + y > end;; > > let f : string Boink.t = inj 20;; > let g : int Boink.t = inj 30;; > > Boink.plus f g;; > > I didn't compile that, but you get the idea... In case anyone finds it useful, I have this code which is ready to use (copy the files it into your project): http://martin.jambon.free.fr/ocaml.html#opaque It works only for strings and ints. You can write: open Opaque let x : [`Year] int_t = int_t 2007 let next_year x : [`Year] int_t = int_t (t_int x + 1) It gives you: val x : [ `Year ] Opaque.int_t val next_year : 'a Opaque.int_t -> [ `Year ] Opaque.int_t Note that we need one more type annotation if we want to get the following signature: val next_year : [ `Year ] Opaque.int_t -> [ `Year ] Opaque.int_t or we can just use "successor" which is equivalent to "succ": val successor : 'a Opaque.int_t -> 'a Opaque.int_t As you can see, things can get pretty ugly, but I found this technique useful to avoid confusion between identifiers that identify different types of objects. Not in my average "hello world" script. Martin -- Martin Jambon http://martin.jambon.free.fr --8323328-80841990-1168049031=:3347--