caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
To: "Loup Vaillant" <loup.vaillant@gmail.com>
Cc: "Caml List" <caml-list@inria.fr>
Subject: Re: [Caml-list] Where are the AST specs?
Date: Mon, 19 Mar 2007 16:37:18 +0100	[thread overview]
Message-ID: <cd67f63a0703190837x664e1b44pac7b4f0f4f9a90e0@mail.gmail.com> (raw)
In-Reply-To: <6f9f8f4a0703190742w3cd42978ib23a78163542729f@mail.gmail.com>

On 3/19/07, Loup Vaillant <loup.vaillant@gmail.com> wrote:
> 2007/3/19, Nicolas Pouillard <nicolas.pouillard@gmail.com>:
> > On 3/19/07, Loup Vaillant <loup.vaillant@gmail.com> wrote:
> > > another example:
> > > (+ x y z)
> > >
> > > (plus (plus x y) z)
> >
> > Yes the macro extension is to weak for that.
> > Here is a camlp4 snippet todo it:
> > [...]
>
> Looks promising. Now, I have more:
> (* x y z)
> (:: x y z [])
> etc. You see the pattern? I need a "fold" macro that writes those macros.

open Camlp4.PreCast;;
AstFilters.register_str_item_filter begin
  Ast.map_expr begin function
  | <:expr@loc< foldl($lid:op$, $e$) >> ->
      let rec foldl =
        function
        | [] -> assert false
        | [x] -> x
        | x :: xs -> <:expr@loc< $lid:op$ $foldl xs$ $x$ >>
      in foldl (List.rev (Ast.list_of_expr e []))
  | <:expr@loc< foldr($lid:op$, $e$) >> ->
      let rec foldr =
        function
        | [] -> assert false
        | [x] -> x
        | x :: xs -> <:expr@loc< $lid:op$ $x$ $foldr xs$ >>
      in foldr (Ast.list_of_expr e [])
  | e -> e
  end
end#str_item;;

$ cat test_macros.ml
let cons x xs = x :: xs;;

foldl(( + ), 1, 2, 3, 4);;
foldr(cons, 1, 2, 3, []);;

$ camlp4o ./macros.cmo test_macros.ml
let cons x xs = x :: xs
let _ = ((1 + 2) + 3) + 4
let _ = cons 1 (cons 2 (cons 3 []))

> Besides, I think my biggest challenge will probably be the automatic
> handling of "loc", and a beautiful anti quotation system.

You're right loc is something hard.
What's wrong with the current anti quotation system?

-- 
Nicolas Pouillard


  parent reply	other threads:[~2007-03-19 15:37 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-18 17:22 Loup Vaillant
2007-03-18 18:21 ` [Caml-list] " Tom
2007-03-19  8:45 ` Olivier Andrieu
2007-03-19  9:17   ` Loup Vaillant
2007-03-19 10:20     ` Nicolas Pouillard
2007-03-19 10:53       ` Loup Vaillant
2007-03-19 13:02         ` Nicolas Pouillard
     [not found]           ` <6f9f8f4a0703190742w3cd42978ib23a78163542729f@mail.gmail.com>
2007-03-19 15:37             ` Nicolas Pouillard [this message]
2007-03-19 16:08               ` Loup Vaillant
2007-03-19 16:37                 ` Nicolas Pouillard
2007-03-23  9:59                   ` Loup Vaillant

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=cd67f63a0703190837x664e1b44pac7b4f0f4f9a90e0@mail.gmail.com \
    --to=nicolas.pouillard@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=loup.vaillant@gmail.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).