From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
To: "Joel Reymont" <joelr1@gmail.com>
Cc: "Caml List" <caml-list@inria.fr>
Subject: Re: [Caml-list] AST transformation and scrapping boilerplate code
Date: Mon, 30 Apr 2007 10:21:16 +0200 [thread overview]
Message-ID: <cd67f63a0704300121i46af58dcxe458867b09c34af3@mail.gmail.com> (raw)
In-Reply-To: <0DF43C61-05DE-4129-A5F6-06AF8722A8D5@gmail.com>
On 4/29/07, Joel Reymont <joelr1@gmail.com> 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
next prev parent reply other threads:[~2007-04-30 8:21 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-29 14:40 Joel Reymont
2007-04-30 8:21 ` Nicolas Pouillard [this message]
2007-04-30 8:41 ` [Caml-list] " Joel Reymont
2007-04-30 9:47 ` Nicolas Pouillard
2007-04-30 8:53 ` Joel Reymont
2007-04-30 12:19 ` Joel Reymont
2007-04-30 12:42 ` Nicolas Pouillard
2007-04-30 12:46 ` Joel Reymont
2007-04-30 12:58 ` Nicolas Pouillard
2007-04-30 13:06 ` Joel Reymont
2007-04-30 13:09 ` Nicolas Pouillard
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=cd67f63a0704300121i46af58dcxe458867b09c34af3@mail.gmail.com \
--to=nicolas.pouillard@gmail.com \
--cc=caml-list@inria.fr \
--cc=joelr1@gmail.com \
/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