caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
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 10:28:25 +0200	[thread overview]
Message-ID: <cd67f63a0607200128l3c93574aka6a399326eaa8505@mail.gmail.com> (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


  reply	other threads:[~2006-07-20  8:28 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 ` Nicolas Pouillard [this message]
     [not found]   ` <20060720.163930.91910937.Christophe.Troestler@umh.ac.be>
2006-07-21 12:01     ` [Caml-list] " 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

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=cd67f63a0607200128l3c93574aka6a399326eaa8505@mail.gmail.com \
    --to=nicolas.pouillard@gmail.com \
    --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;
as well as URLs for NNTP newsgroup(s).