From: Dario Teixeira <darioteixeira@yahoo.com>
To: caml-list@yquem.inria.fr
Subject: Mapping phantom <-> non-phantom
Date: Mon, 7 Jul 2008 06:48:26 -0700 (PDT) [thread overview]
Message-ID: <475424.16703.qm@web54602.mail.re2.yahoo.com> (raw)
Hi,
Suppose you are using polymorphic variants as a phantom type to impose
certain restrictions on a data structure. Is it possible in general to
formulate an equivalent structure that uses only regular variants, or is
the object system required for a full mapping?
As an example, consider the following structure constructed using phantom
types. The "Text" and "Bold" nodes may appear anywhere in a document, but
the "Mref" and "See" nodes produce links, and links may not be the immediate
parents of other links:
module Phantom:
sig
type inline_t = node_t list
and node_t =
private
| Text of string
| Bold of inline_t
| Mref of string * inline_t
| See of string
type 'a t
val text: string -> [> `Nonlink] t
val bold: [< `Link | `Nonlink] t list -> [> `Nonlink] t
val mref: string -> [< `Nonlink] t list -> [> `Link] t
val see: string -> [> `Link] t
end =
struct
type inline_t = node_t list
and node_t =
| Text of string
| Bold of inline_t
| Mref of string * inline_t
| See of string
type 'a t = node_t
let text txt = Text txt
let bold inl = Bold inl
let mref ref inl = Mref (ref, inl)
let see ref = See ref
end
Though a bit contrived, the following structure expresses the same
restrictions using only conventional variants:
module Regular =
struct
type inline_t = super_node_t list
and super_node_t =
| Nonlink_node of nonlink_node_t
| Link_node of link_node_t
and nonlink_node_t =
| Text of string
| Bold of inline_t
and link_node_t =
| Mref of string * nonlink_node_t list
| See of string
end
The version using PV-based phantom types doesn't require all this artifical
scaffolding, and is therefore preferable in my opinion. However, the
ocamlyacc grammar for a document parser will naturally be closer to the
non-phantom version. My doubt is therefore if one could devise a structure
using PV-based phantom types that becomes problematic to parse.
Thanks in advance for your time!
Kind regards,
Dario Teixeira
__________________________________________________________
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-07-07 13:48 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=475424.16703.qm@web54602.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