Version française
Home     About     Download     Resources     Contact us    
Browse thread
Phantom types: transparency vs rogue unification
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Dario Teixeira <darioteixeira@y...>
Subject: Phantom types: transparency vs rogue unification
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