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: Re: [Caml-list] Phantom types: transparency vs rogue unification
> I may have spoken too soon about the functor solving the
> problem.  In fact,
> in a non-trivial example the problem is merely shifted to a
> different place.

Hi,

Just to let you know that I have found an embarrassingly simple workaround:
the Document module can just provide node_of_complex and node_of_basic
functions to overcome the opaqueness of Document.t.  I haven't found (yet)
any major disadvantages to this solution, so I reckon the problem can be
solved this way (code below).

Thanks again for your attention,
Dario Teixeira

___________________________________________________________________________

(************************************************************************)
(* Document.								*)
(************************************************************************)

module type DOCUMENT =
sig
	type inline_t = node_t list
	 and node_t =
		private
		| Text of string
		| Bold of inline_t
		| Italic of inline_t

	type 'a t

	val node_of_complex: [< `Basic | `Complex] t -> node_t
	val node_of_basic: [< `Basic] t -> node_t

	val text: string -> [> `Basic] t
	val bold: 'a t list -> 'a t
	val italic: [< `Basic | `Complex] t list -> [> `Complex] t
end


module Document : DOCUMENT =
struct
	type inline_t = node_t list
	 and node_t =
		| Text of string
		| Bold of inline_t
		| Italic of inline_t

	type 'a t = node_t

	let node_of_complex node = node
	let node_of_basic node = node

	let text str = Text str
	let bold inl = Bold inl
	let italic inl = Italic inl
end


(************************************************************************)
(* Document_parser.							*)
(************************************************************************)

module Document_parser:
sig
	exception Invalid_subset

	val parse_complex: string -> [> `Basic | `Complex] Document.t
	val parse_basic: string -> [> `Basic] Document.t
end =
struct
	open Document

	exception Invalid_subset

	let parse_complex = function
		| "complex"     -> italic [bold [text "ola"]]
		| _             -> bold [text "ola"]

	let complex_to_basic node =
		let rec complex_to_basic_aux = function
			| Text str      -> text str
			| Bold inl      -> bold (List.map complex_to_basic_aux inl)
			| Italic inl    -> raise Invalid_subset
		in complex_to_basic_aux (node_of_complex node)

	let parse_basic str = complex_to_basic (parse_complex str)
end


(************************************************************************)
(* top-level.								*)
(************************************************************************)

open Document
open Document_parser

let foo = bold [text "foo"];;

let bar = italic [text "bar"];;

let a = parse_complex "complex";;

let b = parse_complex "basic";;

let c = parse_basic "basic";;

let d = parse_basic "complex";;  (* Raises exception *)
___________________________________________________________________________




      __________________________________________________________
Not happy with your email address?.
Get the one you really want - millions of new email addresses available now at Yahoo! http://uk.docs.yahoo.com/ymail/new.html