caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Markus Mottl <markus.mottl@gmail.com>
To: ocaml@optimojoe.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Encoding an extensible parse tree and subtyping  within OCaml
Date: Wed, 3 Mar 2010 12:47:27 -0500	[thread overview]
Message-ID: <f8560b81003030947y2ba72bacq332ec6947753d624@mail.gmail.com> (raw)
In-Reply-To: <1267636293.17362.1362924937@webmail.messagingengine.com>

On Wed, Mar 3, 2010 at 12:11,  <ocaml@optimojoe.com> wrote:
> type basic'=('a basic as 'a) basic;;
> type ext'=('a ext as 'a) ext;;

This could also be written as:

  type basic' = basic' basic
  type ext' = ext' ext

Cyclic types are acceptable to the compiler if the cycle is on
polymorphic variants.

> let (x:'a basic)=`Add (`Int 1,`Int 2);;
> let (y:'a ext)=`Mul (x,`Int 3);;
> let (z:'a basic)=`Add (`Int 3,x);;
> let w=`Add (`Mul (`Int 1,`Int 2),`Int 3);;
> let (bad:'a basic)=`Add (1,2);;

When solving the problem with "bad", type constraints are your friend.
 Just define type 'basic" as e.g.:

  type 'a basic= [base | `Add of 'a*'a | `Sub of 'a*'a ] constraint 'a
= [> base];;

To fix the "Junk" problem, you may need to specify the type for one of
the patterns, e.g.:

       match x with
       | (`Int x : ext')-> Printf.printf "%d" x
       ...

But the function itself is not extensible.  To achieve this, you'll
have to define it in a similar way as we did for the type by
introducing another parameter to "tie the recursive knot".  E.g.
(ignore incorrect printing of arithmetic expressions):

  let pp_base (`Int n) = Printf.printf "%d" n

  let pp_basic pp = function
    | #base as base -> pp_base base
    | `Add (l, r) -> pp l; Printf.printf "+"; pp r
    | `Sub (l, r) -> pp l; Printf.printf "-"; pp r

  let pp_ext pp = function
    | #basic as basic -> pp_basic pp basic
    | `Mul (l, r) -> pp l; Printf.printf "*"; pp r

  let rec pp_basic' basic' = pp_basic pp_basic' basic'
  let rec pp_ext' ext' = pp_ext pp_ext' ext'

You will need to constrain a pattern with a type again if you want to
make sure that the pattern match fails in the right place (match case)
if unsupported tags are added.  You could also turn warnings into
errors, which should also fail on the "unused match case" then.

IMHO, these features of polymorphic variants are the best thing since
sliced bread, since they allow for elegant extensibility of e.g.
recursive DSLs.

Regards,
Markus

-- 
Markus Mottl        http://www.ocaml.info        markus.mottl@gmail.com


      reply	other threads:[~2010-03-03 17:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-03 17:11 ocaml
2010-03-03 17:47 ` Markus Mottl [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=f8560b81003030947y2ba72bacq332ec6947753d624@mail.gmail.com \
    --to=markus.mottl@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=ocaml@optimojoe.com \
    /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).