English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Private types in 3.11, again
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-01-21 (19:11)
From: Dario Teixeira <darioteixeira@y...>
Subject: Re: [Caml-list] Private types in 3.11, again

Thank you very much for your help, Jacques!  It's always enlightening to
see an Ocaml ninja in action, though I'm still digesting the finer points
of your message.  Anyway, I have implemented all three solutions you suggested:

a) Using a private row and PV
b) Using a private abbreviation and PV
c) Using the mapper + object system with regular variants

Solution a) is by far the most convenient, though I'm still having some trouble
with it (more below).  Which brings me to a crucial point where I'm still
fuzzy.  My initial model for the link-nodes-shall-not-be-ancestors-of-their-kin
problem did indeed rely on what I now realise are GADTs.  I ran into a
limitation of the type system and so I recast the model using phantom types
because I hoped they could provide a workaround.  So my question is whether it
is at all possible to emulate GADTs using only PV + phantom types, or if on the
contrary one needs to use the object system in these cases, as exemplified by
solution c).  In other words, is solution a) futile, or did you use the object
system in solution c) simply because you were also using regular variants?

As for the problem with solution a), it shows up when I implement outside
of module Node any non-trivial function operating on 'a Node.t.  Function
capitalise_node, for example:

module Node:
	type 'a node_t =
		[ `Text of string
		| `Bold of 'a list
		| `Href of string
		| `Mref of string * 'a list

	type +'a t = private [< 'b node_t ] as 'b

	val text: string -> [> `Nonlink ] t
	val bold: 'a t list -> 'a t
	val href: string -> [> `Link ] t
	val mref: string -> [< `Nonlink ] t list -> [> `Link ] t
end =
	type 'a node_t =
		[ `Text of string
		| `Bold of 'a list
		| `Href of string
		| `Mref of string * 'a list

	type +'a t = 'b node_t as 'b

	let text txt = `Text txt
	let bold seq = `Bold seq
	let href lnk = `Href lnk
	let mref lnk seq = `Mref (lnk, seq)

module Foobar:
	val capitalise_node: 'a Node.t -> 'a Node.t
end =
	let capitalise_node node =
		let rec capitalise_node_aux forbid_link node = match (forbid_link, node) with
			| (_, `Text txt)		-> Node.text (String.capitalize txt)
			| (x, `Bold seq)		-> Node.bold (List.map (capitalise_node_aux x) seq)
			| (false, `Href lnk)		-> Node.href lnk
			| (false, `Mref (lnk, seq))	-> Node.mref lnk (List.map (capitalise_node_aux true) seq)
			| _				-> failwith "this shouldn't happen"
		in capitalise_node_aux false node

The code above produces the compiler error below (though remember that this
same function works fine inside the Node module):

Error: This expression has type ([> `Link | `Nonlink ] as 'a) Node.t list
       but is here used with type ([< `Nonlink ] as 'b) Node.t list
         'a Node.t =
           [< `Bold of 'a Node.t list
            | `Href of string
            | `Mref of string * 'a Node.t list
            | `Text of string ]
       is not compatible with type
         'b Node.t =
           [< `Bold of 'b Node.t list
            | `Href of string
            | `Mref of string * 'b Node.t list
            | `Text of string ]
       The first variant type does not allow tag(s) `Link

Thanks again for all your time!
Best regards,
Dario Teixeira