caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dario Teixeira <darioteixeira@yahoo.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Phantom types: transparency vs rogue unification
Date: Fri, 27 Jun 2008 13:35:57 -0700 (PDT)	[thread overview]
Message-ID: <674022.73253.qm@web54607.mail.re2.yahoo.com> (raw)
In-Reply-To: <720820.32869.qm@web54603.mail.re2.yahoo.com>

> 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


      reply	other threads:[~2008-06-27 20:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-25 14:26 Dario Teixeira
2008-06-25 15:02 ` [Caml-list] " Jacques Carette
2008-06-25 16:29   ` Dario Teixeira
2008-06-26 21:41     ` Dario Teixeira
2008-06-27 20:35       ` Dario Teixeira [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=674022.73253.qm@web54607.mail.re2.yahoo.com \
    --to=darioteixeira@yahoo.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).