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=1.4 required=5.0 tests=SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 17FFCBC0B for ; Mon, 8 Jan 2007 23:23:29 +0100 (CET) Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.243]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l08MNS03008843 for ; Mon, 8 Jan 2007 23:23:28 +0100 Received: by an-out-0708.google.com with SMTP id c24so2207146ana for ; Mon, 08 Jan 2007 14:23:27 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=k7azxa6r1qsUiVFNZ6Y0nAD+Ifm/gcnw8TFB2qbxqyxEgqeiMZZNKUPR8ABePT07o1GVzRuvT/nA+KwrX613wvD7Bzp/7f8QhVvxYhdVFrNwjFfYa1jhhhL2QyRXd1YfJe9HjRk4mg/dkkk4PYKxRa3nHMRw3Sa4yLMAwCLcMYw= Received: by 10.78.160.2 with SMTP id i2mr7944616hue.1168295007115; Mon, 08 Jan 2007 14:23:27 -0800 (PST) Received: by 10.78.198.14 with HTTP; Mon, 8 Jan 2007 14:23:26 -0800 (PST) Message-ID: Date: Mon, 8 Jan 2007 14:23:26 -0800 From: "Nathaniel Gray" To: "Martin Jambon" Subject: Re: [Caml-list] Question on writing efficient Ocaml. Cc: brogoff , caml-list@yquem.inria.fr In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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> X-j-chkmail-Score: MSGID : 45A2C460.001 on discorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at discorde with ID 45A2C460.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 ens-lyon:01 mattias:01 mattias:01 ocaml:01 sig:01 val:01 val:01 struct:01 annotation:01 2007,:98 2007,:98 innocent:98 wrote:01 wrote:01 Thanks to everybody for the discussion of phantom types. I guess modules are unavoidable then. Too bad... :^( On 1/5/07, Martin Jambon wrote: > On Fri, 5 Jan 2007, brogoff wrote: > > > On Fri, 5 Jan 2007, Nathaniel Gray wrote: > >> On 12/29/06, Mattias Engdeg=E5rd wrote: > >>> Is there a reason for this? To my innocent eyes, code like > >>> > >>> type length =3D 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=3D3978 > >> > >> 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 =3D > > 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 =3D > > struct > > type 'a t =3D int > > let inj n =3D n > > let prj t =3D t > > let plus x y =3D x + y > > end;; > > > > let f : string Boink.t =3D inj 20;; > > let g : int Boink.t =3D 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 =3D int_t 2007 > let next_year x : [`Year] int_t =3D 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 > --=20 >>>-- Nathaniel Gray -- Caltech Computer Science ------> >>>-- Mojave Project -- http://mojave.cs.caltech.edu -->