From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.1 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 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 3CE0CBC69 for ; Mon, 30 Apr 2007 10:21:18 +0200 (CEST) Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.239]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l3U8LHf1013821 for ; Mon, 30 Apr 2007 10:21:17 +0200 Received: by wr-out-0506.google.com with SMTP id l58so1445763wrl for ; Mon, 30 Apr 2007 01:21:16 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=ni9Oe3nAFfuCbGQCg2ahtNAHq82J5RIMho5WB9ZayEjs5RcJAIGiAm2BCxIoiDTEoxLOExmqFrY+Zatp/FSjHhb4zB4Ssu4klIclubW7VBxX6pR7F/6tIrXeG5UdqrHny0oP0vUKPVmRYwiyRqq1lybGXUHSuMMeMWgb1hwO1Ko= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=CuSG3kEkv252F97CpvpEd44lWgdxYx/1Asg95+6HoxU57nBxZHhvRottWGteLAUVV+cGrcj8ywFI4c7sAb3OMb3nw8/I23Ka4MhcaFrm3T+FOpusqHg8dGgEdW5fTWJMIghkFUro2ypX1I6bFH2g6SLnAhLa4++pzLviBq6pePk= Received: by 10.114.81.1 with SMTP id e1mr1918032wab.1177921276111; Mon, 30 Apr 2007 01:21:16 -0700 (PDT) Received: by 10.114.181.18 with HTTP; Mon, 30 Apr 2007 01:21:16 -0700 (PDT) Message-ID: Date: Mon, 30 Apr 2007 10:21:16 +0200 From: "Nicolas Pouillard" To: "Joel Reymont" Subject: Re: [Caml-list] AST transformation and scrapping boilerplate code Cc: "Caml List" In-Reply-To: <0DF43C61-05DE-4129-A5F6-06AF8722A8D5@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <0DF43C61-05DE-4129-A5F6-06AF8722A8D5@gmail.com> X-j-chkmail-Score: MSGID : 4635A6FD.001 on concorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at concorde with ID 4635A6FD.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; haskell:01 ocaml:01 invokes:01 constructors:01 expr:01 camlp:01 bindings:01 variants:01 camlp:01 variants:01 decl:01 decl:01 expr:01 foo:01 ocamlc:01 On 4/29/07, Joel Reymont wrote: > Folks, > > I have a large AST [2] that I would like to strip of token locations. > Using the "Scrap your boilerplate" approach [1] the Haskell code > looks like this: > > strip :: (Data a) => a -> a > strip = everywhere (mkT f) > where f (TokenPos a _) = a > f x = x > > Is there a way to accomplish a similar feat in OCaml without writing > out heaps of code that recursively invokes strip for various > constructors to get to expr and strip it of TokenPos? > The new Camlp4 can generate extensible map and fold traversals for a given data structure. alphaCaml [1] also generetates some similar code (and a lot more since its goal is to treat bindings). However these to generators don't handle polymorphic variants. Here is an example using camlp4, but without polymorphic variants: -------------------8<---------------------------------------------------------------------------------- type statement = | InputDecls of input_decl list | VarDecls of var_decl list and subscript = expr list and input_decl = | InputDecl of id * ty * expr | FunArgDecl of id * ty * subscript and expr = | Num of int | Mul of expr * expr | PrintExpr of expr * expr * expr | TokenPos of expr * pos list and id = string and ty = Int and var_decl = Var_decl and pos = int ;; class map = Camlp4Filters.GenerateMap.generated;; class fold = Camlp4Filters.GenerateFold.generated;; let strip_postions = object inherit map as super method expr e = match super#expr e with | TokenPos(a, _) -> a | e -> e end let x = InputDecls [InputDecl("foo", Int, TokenPos( Mul(TokenPos(Num 3,[12]), Num 4), [15]))] ;; let y = strip_postions#statement x --------------------------------------8<--------------------------------------------------------------------- $ ocamlc -pp "camlp4o -filter map -filter fold" /tmp/test.ml Hope this helps, [1]: http://cristal.inria.fr/~fpottier/alphaCaml/ -- Nicolas Pouillard