caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Phantom types: transparency vs rogue unification
@ 2008-06-25 14:26 Dario Teixeira
  2008-06-25 15:02 ` [Caml-list] " Jacques Carette
  0 siblings, 1 reply; 5+ messages in thread
From: Dario Teixeira @ 2008-06-25 14:26 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-06-27 20:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-25 14:26 Phantom types: transparency vs rogue unification Dario Teixeira
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

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).