From: "Nathaniel Gray" <n8gray@gmail.com>
To: "Martin Jambon" <martin.jambon@ens-lyon.org>
Cc: brogoff <brogoff@speakeasy.net>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Question on writing efficient Ocaml.
Date: Mon, 8 Jan 2007 14:23:26 -0800 [thread overview]
Message-ID: <aee06c9e0701081423s1a20beb1s2f89ef87b1e550a0@mail.gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0701051751370.3347@droopy>
Thanks to everybody for the discussion of phantom types. I guess
modules are unavoidable then. Too bad... :^(
On 1/5/07, Martin Jambon <martin.jambon@ens-lyon.org> wrote:
> On Fri, 5 Jan 2007, brogoff wrote:
>
> > On Fri, 5 Jan 2007, Nathaniel Gray wrote:
> >> On 12/29/06, Mattias Engdegård <mattias@virtutech.se> 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
>
--
>>>-- Nathaniel Gray -- Caltech Computer Science ------>
>>>-- Mojave Project -- http://mojave.cs.caltech.edu -->
next prev parent reply other threads:[~2007-01-08 22:23 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20061203110003.6804FBC6A@yquem.inria.fr>
2006-12-28 11:42 ` Ian Oversby
2006-12-28 16:26 ` [Caml-list] " Jon Harrop
2006-12-28 17:13 ` skaller
2006-12-29 6:05 ` Jacques Garrigue
2006-12-29 11:15 ` Mattias Engdegård
2007-01-06 0:52 ` Nathaniel Gray
2007-01-06 1:01 ` Philippe Wang
2007-01-06 1:15 ` brogoff
2007-01-06 2:27 ` Martin Jambon
2007-01-08 22:23 ` Nathaniel Gray [this message]
2006-12-28 19:24 ` Manfred Lotz
2006-12-29 1:23 ` [Caml-list] " Andrej Bauer
2006-12-29 9:58 ` Ian Oversby
2006-12-29 2:07 ` Jon Harrop
2007-01-03 16:43 ` Serge Aleynikov
[not found] <15946.213.30.139.86.1167315231.squirrel@webmail.nerim.net>
2006-12-28 16:03 ` Ian Oversby
2006-12-28 17:00 ` Richard Jones
2006-12-28 22:23 ` Jon Harrop
2006-12-29 9:42 ` Ian Oversby
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=aee06c9e0701081423s1a20beb1s2f89ef87b1e550a0@mail.gmail.com \
--to=n8gray@gmail.com \
--cc=brogoff@speakeasy.net \
--cc=caml-list@yquem.inria.fr \
--cc=martin.jambon@ens-lyon.org \
/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).