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: Re: [Caml-list] Camlp4 3.10.0+beta: lists without $list:...$
Date: Fri, 23 Mar 2007 17:35:21 +0100	[thread overview]
Message-ID: <cd67f63a0703230935t36d2afbfjeb893513c43fbf0f@mail.gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0703222255530.2958@droopy>

On 3/23/07, Martin Jambon <martin.jambon@ens-lyon.org> wrote:
> I managed to create an object type using camlp4orf:
>
> let ctyp_object _loc ml =
>    let fields =
>      List.fold_left
>        (fun o (name, typ) ->
>          let m = <:ctyp< $lid:name$ : $typ$ >> in
>          <:ctyp< $o$; $m$ >>)
>        <:ctyp< >> ml in
>    <:ctyp< < $fields$ > >>
>
> where ml is a list of method declarations of type (string * ctyp) list.
> Before we just had to do this:
>
> <:ctyp< < $list:ml$ > >>
>
> Is there anything simpler than my solution? Is a shortcut available?

In fact there is some shortcuts but it was not for all constructions
(fixed now in CVS).

There is functions underlining these shortcuts like Ast.tySem_of_list
that join a list with semicolons.

I can explain briefly the change...

Before there was lists inside the tree. In order to allow a fully
reflective mechanism we now avoid these from the AST.

So there is now many more concrete syntax for any part of the AST
(match branches, let bindings, record field bindings...).

(p, None, e) -> <:match_case< $p$ -> $e$ >>
(p, Some guard, e) -> <:match_case< $p$ when $guard$ -> $e$ >>
(loc, field, true, typ) -> <:ctyp< { $lid:field$ : mutable $typ$ } >>
...

If m1 and m2 are to match branches (like (p, None, e) for the old one):

<:match_case< $m1$ | $m2$ >>

You want some default cases:

<:match_case< A -> 1 | $m1$ | B x -> x | $m2$ | _ -> assert False >>

So, when one wrote something like:

  let cases = List.mapi (fun i (_,e) -> <:patt< $int:string_of_int i$
>>, None, e) brs in
  let cases = cases @ [<:patt< _ >>, None, <:expr< raise Ulexing.Error >>] in
  let actions = <:expr< match __ulex_state_0 lexbuf with [ $list:cases$ ] >> in

We now can write:

  let cases = List.mapi (fun i (_,e) -> <:match_case< $`int:i$ -> $e$ >>) brs in
  <:expr< match __ulex_state_0 lexbuf with
                 [ $list:cases$
                 | _ -> raise Ulexing.Error ] } >>

Where the error case is inlined in the match directly.

This <:patt< $int:string_of_int i$ >>, None, e is a typical thing
where no syntax were available to it.
Now you can write <:match_case< $`int:i$ -> $e$ >> since any part of
the abstract syntax have a quotation in concrete syntax.

You can note that in `` [ $list:cases$ '' I used the $list:...$
antiquotation that was used to inject some list in a tree. In 3.10 you
can still do that, but  that's a sugar for a function call that will
join all your trees by the proper separator (bar here).

FYI, I've just added all the missing $list:$ sugars to the CVS version
(will be public soon).

> PS: any progress on the manual? It's kind of hard to test new features
> when you don't know what they are.

I agree, but there is still no progress on the documentation :(

BTW, have you read these slides
http://gallium.inria.fr/~pouillar/pub/camlp4/renovation-camlp4-longue.pdf

-- 
Nicolas Pouillard


  parent reply	other threads:[~2007-03-23 16:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-23  6:13 Martin Jambon
2007-03-23 16:03 ` [Caml-list] " Markus Mottl
2007-03-23 16:35 ` Nicolas Pouillard [this message]
2007-03-23 19:00   ` Martin Jambon
2007-03-24 12:21     ` Nicolas Pouillard
2007-03-24 20:24       ` Martin Jambon
2007-03-26  6:48         ` Markus Mottl
2007-03-26  7:27       ` Hendrik Tews

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=cd67f63a0703230935t36d2afbfjeb893513c43fbf0f@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).