caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
To: "Martin Jambon" <martin.jambon@ens-lyon.org>
Cc: caml-list@inria.fr
Subject: [Camlp4 3.10] The rosetta stone (Was: lists without $list:...$)
Date: Sun, 25 Mar 2007 22:47:00 +0200	[thread overview]
Message-ID: <cd67f63a0703251347p5443fe4cj8c07f836ef232c6b@mail.gmail.com> (raw)

On 3/24/07, Martin Jambon <martin.jambon@ens-lyon.org> wrote:
>
> I would like you or anyone knowledgeable to translate one significant
> syntax extension. You know, some kind of Rosetta stone. You can take
> Markus' sexp syntax extension or ioxml if you prefer, it won't be much
> different.

Here is your rosetta stone :)

I translated pa_json_static.ml.

To highlight a little the changes in the AST,
I want to show an input snippet and two translations:

For the old camlp4 it was:

| Object l ->
   let ml = List.map (fun x -> (x.field_caml_name,
                                convert x.field_type)) l in
   <:ctyp< < $list:ml$ > >>

The type of `ml' is (string * ctyp) list. The concept of quotations is
to get concrete syntax for abstract terms and then avoid to learn all
constructors and types. Alas for some of them you have to know the type.
In the new version you can express any term (except one) by concrete syntax.

The closest version is:

| Object l ->
   let ml = List.map (fun x ->
     <:ctyp< $lid:x.field_caml_name$ : $convert x.field_type$ >>) l in
   <:ctyp< < $list:ml$ > >>

Here one doesn't know if methods declarations are a pair or something else and
we don't care. Since one knows the syntax << method_name : method_type >>.
At this place the list antiquotation $list:ml$ is a sugar for
$Ast.tySem_of_list ml$.

By changing a little more the code one can use something closer to the
object syntax.

| Object l ->
   let ml = List.fold_right (fun x acc ->
     <:ctyp< $lid:x.field_caml_name$ : $convert x.field_type$ ; $acc$ >>)
     l <:ctyp<>> in
   <:ctyp< < $ml$ > >>

The general syntax of object types (omiting `..' for the row variable) is
< meth1 : type1 ; ... ; methN : typeN > then one can avoid to construct a list
(since map is a fold_right with `::') and then call a function that destruct it.

Another thing to point out is the use of the nil type <:ctyp<>> that is quite
useful to start the folding.

In this translation I used both styles depending of the context.

The patch:
  http://gallium.inria.fr/~pouillar/pub/camlp4/rosetta/pa_json_static/pa_json_static.patch

The new version (compiles with camlp4orf):
  http://gallium.inria.fr/~pouillar/pub/camlp4/rosetta/pa_json_static/pa_json_static.ml

Cheers,

--
Nicolas Pouillard


             reply	other threads:[~2007-03-25 20:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-25 20:47 Nicolas Pouillard [this message]
2007-04-01  8:04 ` Martin Jambon
2007-04-01 11:46 ` [Caml-list] " Dmitry Bely
2007-04-01 17:24   ` Nicolas Pouillard
  -- strict thread matches above, loose matches on Subject: below --
2007-03-25 20:42 Nicolas Pouillard

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=cd67f63a0703251347p5443fe4cj8c07f836ef232c6b@mail.gmail.com \
    --to=nicolas.pouillard@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=martin.jambon@ens-lyon.org \
    /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).