caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dario Teixeira <darioteixeira@yahoo.com>
To: caml-list@yquem.inria.fr
Subject: Phantom types: transparency vs rogue unification
Date: Wed, 25 Jun 2008 07:26:30 -0700 (PDT)	[thread overview]
Message-ID: <767974.3892.qm@web54607.mail.re2.yahoo.com> (raw)

Hi,

I've been experimenting with phantom types since I've read Richard Jones'
excellent intro to the subject [1].  I've now come across a tricky problem,
however.  Essentially, I need to reconcile two seemingly incompatible goals:
making a module's phantom type opaque to avoid the "rogue unification" issue,
and making it transparent so that external modules can produce values of
that type.

So, suppose I have a "Stringies" module, which uses a phantom string to
parametrise floats.  Because the phantom type is opaque, the rogue unification
is prevented in the last line:

_______________________________________________________________
module Stringies:
sig
        type 'a t
        val make: float -> string t
        val add: string t -> string t -> string t
end =
struct
        type 'a t = float
        let make x = x
        let add a b = a +. b
end

open Stringies
let foo = add (make 1.0) (make 2.0);;
let bar = add (make 1.0) 2.0;;  (* Error! *)
_______________________________________________________________


But suppose you wish to create an external module with a function to parse
strings into Stringies.t.  The phantom must become visible, but that allows
for rogue unification to take place:

_______________________________________________________________
module Stringies:
sig
        type 'a t = float
        val make: float -> string t
        val add: string t -> string t -> string t
end =
struct
        type 'a t = float
        let make x = x
        let add a b = a +. b
end


module Stringies_IO:
sig
        val parse: string -> string Stringies.t
end =
struct
        let parse = float_of_string
end


open Stringies
let foo = add (make 1.0) (Stringies_IO.parse "2.0");;
let bar = add (make 1.0) 2.0;;  (* Rogue unification! *)
_______________________________________________________________


How would you go about solving this problem?

Thanks in advance!
cheers,
Dario Teixeira

[1] http://camltastic.blogspot.com/2008/05/phantom-types.html



      __________________________________________________________
Sent from Yahoo! Mail.
A Smarter Email http://uk.docs.yahoo.com/nowyoucan.html


             reply	other threads:[~2008-06-25 14:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-25 14:26 Dario Teixeira [this message]
2008-06-25 15:02 ` [Caml-list] " Jacques Carette
2008-06-25 16:29   ` Dario Teixeira
2008-06-26 21:41     ` Dario Teixeira
2008-06-27 20:35       ` Dario Teixeira

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=767974.3892.qm@web54607.mail.re2.yahoo.com \
    --to=darioteixeira@yahoo.com \
    --cc=caml-list@yquem.inria.fr \
    /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).