Browse thread
camlp4 & free variables
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2006-07-20 (08:28) |
From: | Nicolas Pouillard <nicolas.pouillard@g...> |
Subject: | Re: [Caml-list] camlp4 & free variables |
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