<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2009/01/fee560f02105bf4cb1f9565646c60a90"
  from="Dario Teixeira &lt;darioteixeira@y...&gt;"
  author="Dario Teixeira"
  date="2009-01-31T16:55:27"
  subject="Union of type variables"
  prev="2009/01/61637113b8c509a660eb92246d3ccff4"
  next="2009/01/2a47f7b2c917b9f58a6fd9ef41dabbfe"
  next-in-thread="2009/02/21abc89b6f4c6ec96e0bf39524c70e31"
  prev-thread="2009/01/ff989057cc196f94952b5a35a0dd7dd0"
  next-thread="2009/01/2a47f7b2c917b9f58a6fd9ef41dabbfe"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="Union of type variables">
<msg 
  url="2009/01/fee560f02105bf4cb1f9565646c60a90"
  from="Dario Teixeira &lt;darioteixeira@y...&gt;"
  author="Dario Teixeira"
  date="2009-01-31T16:55:27"
  subject="Union of type variables">
<msg 
  url="2009/02/21abc89b6f4c6ec96e0bf39524c70e31"
  from="Dario Teixeira &lt;darioteixeira@y...&gt;"
  author="Dario Teixeira"
  date="2009-02-02T17:07:44"
  subject="Re: [Caml-list] Union of type variables">
</msg>
</msg>
</thread>

<contents>
Hi,

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 -&gt; [&gt; `Foo ] t
	val b: int -&gt; [&gt; `Foo ] t
	val c: int -&gt; [&gt; `Bar ] t
	val d: int -&gt; [&gt; `Bar ] t

	val union: 'a t list -&gt; '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 -&gt; 'y t -&gt; [&gt; 'x | 'y ] t
let union x y = Union (x, y)

Thanks in advance!
Best regards,
Dario Teixeira



      

</contents>

</message>

