Consider the following toy experiment with phantom types. Note that the shown
implementation of function "union" is far from optimal; though it takes only
two arguments, these are being passed as a list. This is basically just a
kludge that forces the phantom type in the return type to be also a union.
module M:
sig
type 'a t
val a: int -> [> `Foo ] t
val b: int -> [> `Foo ] t
val c: int -> [> `Bar ] t
val d: int -> [> `Bar ] t
val union: 'a t list -> 'a t
end =
struct
type foobar_t =
| A of int
| B of int
| C of int
| D of int
| Union of foobar_t * foobar_t
type 'a t = foobar_t
let a x = A x
let b x = B x
let c x = C x
let d x = D x
let union (x :: y :: []) = Union (x, y)
end
Obviously I would like to get rid of this kludge. The signature and
implementation for "union" should be something like the (syntactically
incorrect) code below. But is it at all possible to declare an union
of type variables? (which presupposes they are polymorphic variants)
val union: 'x t -> 'y t -> [> 'x | 'y ] t
let union x y = Union (x, y)
Thanks in advance!
Best regards,
Dario Teixeira
_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
> Obviously I would like to get rid of this kludge. The signature and
> implementation for "union" should be something like the (syntactically
> incorrect) code below. But is it at all possible to declare an union
> of type variables? (which presupposes they are polymorphic variants)
>
> val union: 'x t -> 'y t -> [> 'x |
> 'y ] t
> let union x y = Union (x, y)
In the meantime I realised what the problem was. I neglected to take
into account the open nature of polymorphic variants, meaning that as
long as the phantom type variable is open, then the following code is
enough to do what I want:
val union: 'a t -> 'a t -> 'a t
let union x y = Union (x, y)
Cheers,