From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 0BF81BBE4 for ; Thu, 20 Jul 2006 11:25:59 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k6K9Pw8m008169 for ; Thu, 20 Jul 2006 11:25:58 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA00002 for ; Thu, 20 Jul 2006 11:25:57 +0200 (MET DST) Received: from altrade.nijmegen.internl.net (altrade.nijmegen.internl.net [217.149.192.18]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k6K9Pva4008901 for ; Thu, 20 Jul 2006 11:25:57 +0200 Received: from [10.19.74.7] by altrade.nijmegen.internl.net via 148-198.bbned.dsl.internl.net [217.149.198.148] with ESMTP id k6K9PfY0024380 (8.13.2/2.04); Thu, 20 Jul 2006 11:25:45 +0200 (MET DST) In-Reply-To: <20060720.012706.109316902.Christophe.Troestler@umh.ac.be> References: <20060720.012706.109316902.Christophe.Troestler@umh.ac.be> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: <8F1A0E34-5DC3-417F-BE5F-01A8890DFF17@inter.nl.net> Cc: "O'Caml Mailing List" Content-Transfer-Encoding: 7bit From: Guido Kollerie Subject: Re: [Caml-list] camlp4 & free variables Date: Thu, 20 Jul 2006 11:25:43 +0200 To: "Christophe TROESTLER" X-Mailer: Apple Mail (2.752.2) X-Miltered: at nez-perce with ID 44BF4C26.003 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 44BF4C25.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; camlp:01 christophe:01 troestler:01 christophe:01 troestler:01 umh:01 camlp:01 syntax:01 expr:01 struct:01 ocaml:01 syntax:01 pervasives:01 vars:01 pervasives:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 On 7/20/06, Christophe TROESTLER 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 <>); (* 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