From: Guido Kollerie <gkoller@inter.nl.net>
To: "Christophe TROESTLER" <Christophe.Troestler@umh.ac.be>
Cc: "O'Caml Mailing List" <caml-list@inria.fr>
Subject: Re: [Caml-list] camlp4 & free variables
Date: Thu, 20 Jul 2006 11:25:43 +0200 [thread overview]
Message-ID: <8F1A0E34-5DC3-417F-BE5F-01A8890DFF17@inter.nl.net> (raw)
In-Reply-To: <20060720.012706.109316902.Christophe.Troestler@umh.ac.be>
On 7/20/06, Christophe TROESTLER <Christophe.Troestler@umh.ac.be> wrote:
> Hi,
>
> Is it possible to write a camlp4 syntax extension that "extract" the
> free variables of an expression?
You are quite lucky, the camlp4 version has one module to do that!
> As an example, say I write
>
> FUNCTION(let z = x + 2 in x + 2 * y * x * z)
>
> and it becomes
>
> fun ~x ~y -> let z = x + 2 in x + 2 * y * x * z
#default_quotation "expr";
open Camlp4.PreCast;
open Format;
module FV = Camlp4.Struct.FreeVars.Make Ast;
module PP = Camlp4.Printers.OCaml.Make Syntax;
module S = FV.S;
value _loc = Loc.ghost;
value pervasives =
let list =
[ "+"; "-"; "/"; "*" (* ... *) ]
in List.fold_right S.add list S.empty;
value f e =
let fv = FV.free_vars pervasives e in
S.fold (fun x acc -> << fun ~ $x$ -> $acc$ >>) fv e;
value print_expr = (new PP.printer ())#expr;
printf "%a@." print_expr (f <<let z = x + 2 in x + 2 * y * x * z>>);
(* end *)
# using an up to date CVS checkout of ocaml.
$ ocamlc -I +camlp4 Camlp4.cma -pp camlp4rf free_vars_test.ml
$ ./a.out
fun ~y ~x -> let z = x + 2 in x + (((2 * y) * x) * z)
Best regards,
--
Nicolas Pouillard
_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
prev parent reply other threads:[~2006-07-20 9:25 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-07-19 23:27 Christophe TROESTLER
2006-07-20 8:28 ` [Caml-list] " Nicolas Pouillard
[not found] ` <20060720.163930.91910937.Christophe.Troestler@umh.ac.be>
2006-07-21 12:01 ` Nicolas Pouillard
2006-07-21 18:16 ` Martin Jambon
2006-07-20 9:25 ` Guido Kollerie
2006-07-20 9:25 ` Guido Kollerie
2006-07-20 10:25 ` Accidentally bounced my email [Was: [Caml-list] camlp4 & free variables] Guido Kollerie
2006-07-20 9:25 ` Guido Kollerie [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=8F1A0E34-5DC3-417F-BE5F-01A8890DFF17@inter.nl.net \
--to=gkoller@inter.nl.net \
--cc=Christophe.Troestler@umh.ac.be \
--cc=caml-list@inria.fr \
/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