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 concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 46553BB83 for ; Thu, 20 Jul 2006 10:28:27 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k6K8SQxA030762 for ; Thu, 20 Jul 2006 10:28:26 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA31524 for ; Thu, 20 Jul 2006 10:28:26 +0200 (MET DST) Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.187]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k6K8SPBn031535 for ; Thu, 20 Jul 2006 10:28:26 +0200 Received: by nf-out-0910.google.com with SMTP id p77so458833nfc for ; Thu, 20 Jul 2006 01:28:25 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=JJNMsk4HCzDp5W9x5V1iMCwU8xtXdo4MSmy6myvDaC69jh8KNpOiqGs7EJLzf39AS074dsFhmcDag/L9RmYhsd8z+GWej/+oGXRFEWmxxQm1zVqw+04yQ6gunw0sgyKkkQz/ZI+iQLwUV9hF4oAhXyTzIcQoTJh4dc0QQJfx258= Received: by 10.78.195.9 with SMTP id s9mr191415huf; Thu, 20 Jul 2006 01:28:25 -0700 (PDT) Received: by 10.78.13.17 with HTTP; Thu, 20 Jul 2006 01:28:25 -0700 (PDT) Message-ID: Date: Thu, 20 Jul 2006 10:28:25 +0200 From: "Nicolas Pouillard" To: "Christophe TROESTLER" Subject: Re: [Caml-list] camlp4 & free variables Cc: "O'Caml Mailing List" In-Reply-To: <20060720.012706.109316902.Christophe.Troestler@umh.ac.be> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <20060720.012706.109316902.Christophe.Troestler@umh.ac.be> X-j-chkmail-Score: MSGID : 44BF3EA9.000 on nez-perce : j-chkmail score : X : 0/20 1 X-Miltered: at concorde with ID 44BF3EAA.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 44BF3EA9.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=RCVD_BY_IP 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