caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Camlp4 3.10] The rosetta stone (Was: lists without $list:...$)
@ 2007-03-25 20:47 Nicolas Pouillard
  2007-04-01  8:04 ` Martin Jambon
  2007-04-01 11:46 ` [Caml-list] " Dmitry Bely
  0 siblings, 2 replies; 4+ messages in thread
From: Nicolas Pouillard @ 2007-03-25 20:47 UTC (permalink / raw)
  To: Martin Jambon; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Camlp4 3.10] The rosetta stone (Was: lists without $list:...$)
  2007-03-25 20:47 [Camlp4 3.10] The rosetta stone (Was: lists without $list:...$) Nicolas Pouillard
@ 2007-04-01  8:04 ` Martin Jambon
  2007-04-01 11:46 ` [Caml-list] " Dmitry Bely
  1 sibling, 0 replies; 4+ messages in thread
From: Martin Jambon @ 2007-04-01  8:04 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: caml-list

On Sun, 25 Mar 2007, Nicolas Pouillard wrote:

> Here is your rosetta stone :)
>
> I translated pa_json_static.ml.

Thanks a lot.

I backported it to camlp4 3.10.0+beta. Compiling the extension and 
using it to preprocess a sample file now work fine (but I haven't tested 
the correctness of the generated code).

The few things that didn't want to work with 3.10.0+beta were commented 
out and replaced by workarounds.

People interested in upgrading their syntax extensions can read the 
side-by-side diffs:

   http://martin.jambon.free.fr/examples/pa_json_static_3100beta.html


Martin

--
Martin Jambon
http://martin.jambon.free.fr


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] [Camlp4 3.10] The rosetta stone (Was: lists without $list:...$)
  2007-03-25 20:47 [Camlp4 3.10] The rosetta stone (Was: lists without $list:...$) Nicolas Pouillard
  2007-04-01  8:04 ` Martin Jambon
@ 2007-04-01 11:46 ` Dmitry Bely
  2007-04-01 17:24   ` Nicolas Pouillard
  1 sibling, 1 reply; 4+ messages in thread
From: Dmitry Bely @ 2007-04-01 11:46 UTC (permalink / raw)
  To: caml-list

On 3/26/07, Nicolas Pouillard <nicolas.pouillard@gmail.com> wrote:
> 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.

For me it compiles but does not work:

D:\Work\Camlp4test>ocamlc -pp camlp4orf -I +camlp4 -c pa_json_static.ml

D:\Work\Camlp4test>camlp4 pa_json_static.cmo -parser OCaml -printer
OCaml test.ml
No level labelled "top" in entry "str_item"
Failure: "Grammar.extend"

(latest CVS with release310 tag)

BTW, have anyone tried to translate pa_IoXML? It thought I would look
into pa_json_static changes and do the same for IoXML, but in fact
it's quite hard, at least for me...

- Dmitry Bely


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] [Camlp4 3.10] The rosetta stone (Was: lists without $list:...$)
  2007-04-01 11:46 ` [Caml-list] " Dmitry Bely
@ 2007-04-01 17:24   ` Nicolas Pouillard
  0 siblings, 0 replies; 4+ messages in thread
From: Nicolas Pouillard @ 2007-04-01 17:24 UTC (permalink / raw)
  To: Dmitry Bely; +Cc: caml-list

On 4/1/07, Dmitry Bely <dmitry.bely@gmail.com> wrote:
> On 3/26/07, Nicolas Pouillard <nicolas.pouillard@gmail.com> wrote:
> > 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.
>
> For me it compiles but does not work:
>
> D:\Work\Camlp4test>ocamlc -pp camlp4orf -I +camlp4 -c pa_json_static.ml
>
> D:\Work\Camlp4test>camlp4 pa_json_static.cmo -parser OCaml -printer
> OCaml test.ml
> No level labelled "top" in entry "str_item"
> Failure: "Grammar.extend"

Wrong order for loaded modules....

camlp4 -parser OCaml pa_json_static.cmo -printer OCaml test.ml

or shorter:

camlp4 -parser o pa_json_static.cmo -printer o test.ml

or event shorter:

camlp4o pa_json_static.cmo -printer o test.ml

-- 
Nicolas Pouillard


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-04-01 17:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-25 20:47 [Camlp4 3.10] The rosetta stone (Was: lists without $list:...$) Nicolas Pouillard
2007-04-01  8:04 ` Martin Jambon
2007-04-01 11:46 ` [Caml-list] " Dmitry Bely
2007-04-01 17:24   ` Nicolas Pouillard

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).