* camlp4 & free variables
@ 2006-07-19 23:27 Christophe TROESTLER
2006-07-20 8:28 ` [Caml-list] " Nicolas Pouillard
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Christophe TROESTLER @ 2006-07-19 23:27 UTC (permalink / raw)
To: O'Caml Mailing List
Hi,
Is it possible to write a camlp4 syntax extension that "extract" the
free variables of an expression? 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
(of course I'd like good error reporting in case of an error in the
expression). If it is not possible, what is the best way to build a
tool that does that (short of writing a parser for the OCaml language)?
Thanks,
ChriS
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] camlp4 & free variables
2006-07-19 23:27 camlp4 & free variables Christophe TROESTLER
@ 2006-07-20 8:28 ` Nicolas Pouillard
[not found] ` <20060720.163930.91910937.Christophe.Troestler@umh.ac.be>
2006-07-20 9:25 ` Guido Kollerie
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Nicolas Pouillard @ 2006-07-20 8:28 UTC (permalink / raw)
To: Christophe TROESTLER; +Cc: O'Caml Mailing List
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] camlp4 & free variables
2006-07-19 23:27 camlp4 & free variables Christophe TROESTLER
2006-07-20 8:28 ` [Caml-list] " Nicolas Pouillard
@ 2006-07-20 9:25 ` Guido Kollerie
2006-07-20 9:25 ` Guido Kollerie
2006-07-20 9:25 ` [Caml-list] camlp4 & free variables Guido Kollerie
3 siblings, 0 replies; 8+ messages in thread
From: Guido Kollerie @ 2006-07-20 9:25 UTC (permalink / raw)
To: Christophe TROESTLER; +Cc: O'Caml Mailing List
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] camlp4 & free variables
2006-07-19 23:27 camlp4 & free variables Christophe TROESTLER
2006-07-20 8:28 ` [Caml-list] " Nicolas Pouillard
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 ` [Caml-list] camlp4 & free variables Guido Kollerie
3 siblings, 1 reply; 8+ messages in thread
From: Guido Kollerie @ 2006-07-20 9:25 UTC (permalink / raw)
To: Christophe TROESTLER; +Cc: O'Caml Mailing List
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] camlp4 & free variables
2006-07-19 23:27 camlp4 & free variables Christophe TROESTLER
` (2 preceding siblings ...)
2006-07-20 9:25 ` Guido Kollerie
@ 2006-07-20 9:25 ` Guido Kollerie
3 siblings, 0 replies; 8+ messages in thread
From: Guido Kollerie @ 2006-07-20 9:25 UTC (permalink / raw)
To: Christophe TROESTLER; +Cc: O'Caml Mailing List
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
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2006-07-21 18:17 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-19 23:27 camlp4 & free variables 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 ` [Caml-list] camlp4 & free variables Guido Kollerie
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox