* AST transformation and scrapping boilerplate code
@ 2007-04-29 14:40 Joel Reymont
2007-04-30 8:21 ` [Caml-list] " Nicolas Pouillard
0 siblings, 1 reply; 11+ messages in thread
From: Joel Reymont @ 2007-04-29 14:40 UTC (permalink / raw)
To: Caml List
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?
Thanks, Joel
[1] http://tinyurl.com/36jj3q
[2] Incomplete AST
let 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 =
[
...
| `FunCall of ty * string * expr list * bars_ago * instrument
| `Mul of expr * expr
...
| `PrintExpr of expr * expr * expr
...
| `TokenPos of expr * pos list
]
--
http://wagerlabs.com/
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] AST transformation and scrapping boilerplate code
2007-04-29 14:40 AST transformation and scrapping boilerplate code Joel Reymont
@ 2007-04-30 8:21 ` Nicolas Pouillard
2007-04-30 8:41 ` Joel Reymont
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Nicolas Pouillard @ 2007-04-30 8:21 UTC (permalink / raw)
To: Joel Reymont; +Cc: Caml List
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
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] AST transformation and scrapping boilerplate code
2007-04-30 8:21 ` [Caml-list] " Nicolas Pouillard
@ 2007-04-30 8:41 ` Joel Reymont
2007-04-30 9:47 ` Nicolas Pouillard
2007-04-30 8:53 ` Joel Reymont
2007-04-30 12:19 ` Joel Reymont
2 siblings, 1 reply; 11+ messages in thread
From: Joel Reymont @ 2007-04-30 8:41 UTC (permalink / raw)
To: Nicolas Pouillard; +Cc: Caml List
Thank you Nicolas! This helps a lot!
What needs to be done to make the generators handle polymorphic
variants?
On Apr 30, 2007, at 9:21 AM, Nicolas Pouillard wrote:
> 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.
--
http://wagerlabs.com/
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] AST transformation and scrapping boilerplate code
2007-04-30 8:21 ` [Caml-list] " Nicolas Pouillard
2007-04-30 8:41 ` Joel Reymont
@ 2007-04-30 8:53 ` Joel Reymont
2007-04-30 12:19 ` Joel Reymont
2 siblings, 0 replies; 11+ messages in thread
From: Joel Reymont @ 2007-04-30 8:53 UTC (permalink / raw)
To: Nicolas Pouillard; +Cc: Caml List
A combination of polymoprhic variants with generated map and fold
traversals would be incredibly useful in AST transformations.
Imagine the case where some constructors are "shared" between
different ASTs, e.g. type ty = [`A a|`B b] in one module and type ty
= [`B b|`C c|` d] in another. You need to transform the first type
into the second.
You could use the example below and add matches for special cases
where either the target constructor is different or different actions
need to be taken.
Is adding polymorphic variants to camlp4 something that can be
accomplished by mere mortals? Any pointers on where to start and how
to proceed?
Thanks, Joel
On Apr 30, 2007, at 9:21 AM, Nicolas Pouillard wrote:
> However these to generators don't handle polymorphic variants.
> [...]
> 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
--
http://wagerlabs.com/
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] AST transformation and scrapping boilerplate code
2007-04-30 8:21 ` [Caml-list] " Nicolas Pouillard
2007-04-30 8:41 ` Joel Reymont
2007-04-30 8:53 ` Joel Reymont
@ 2007-04-30 12:19 ` Joel Reymont
2007-04-30 12:42 ` Nicolas Pouillard
2 siblings, 1 reply; 11+ messages in thread
From: Joel Reymont @ 2007-04-30 12:19 UTC (permalink / raw)
To: Nicolas Pouillard; +Cc: Caml List
I get this error when try to compile my version of the token remover.
The only difference is that my version has "open AST" at the top
whereas in the example everything is in one file.
What am I missing?
Thanks, Joel
---
./build.sh test.byte
+ ocamlfind ocamlc -package ounit -c -I +camlp4 -pp 'camlp4o -filter
map -filter fold' -o easy_strip.cmo easy_strip.ml
File "easy_strip.ml", line 8, characters 10-15:
This expression has no method expr
Command exited with code 2.
--
http://wagerlabs.com/
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] AST transformation and scrapping boilerplate code
2007-04-30 12:19 ` Joel Reymont
@ 2007-04-30 12:42 ` Nicolas Pouillard
2007-04-30 12:46 ` Joel Reymont
0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Pouillard @ 2007-04-30 12:42 UTC (permalink / raw)
To: Joel Reymont; +Cc: Caml List
On 4/30/07, Joel Reymont <joelr1@gmail.com> wrote:
> I get this error when try to compile my version of the token remover.
> The only difference is that my version has "open AST" at the top
> whereas in the example everything is in one file.
>
> What am I missing?
The generator needs to see youre types definitions.
--
Nicolas Pouillard
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] AST transformation and scrapping boilerplate code
2007-04-30 12:42 ` Nicolas Pouillard
@ 2007-04-30 12:46 ` Joel Reymont
2007-04-30 12:58 ` Nicolas Pouillard
0 siblings, 1 reply; 11+ messages in thread
From: Joel Reymont @ 2007-04-30 12:46 UTC (permalink / raw)
To: Nicolas Pouillard; +Cc: Caml List
On Apr 30, 2007, at 1:42 PM, Nicolas Pouillard wrote:
> The generator needs to see youre types definitions.
What does it mean? Open <AST module> doesn't do it?
How do I make the type defs in a separate module available to the
generator?
Thanks, Joel
--
http://wagerlabs.com/
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] AST transformation and scrapping boilerplate code
2007-04-30 12:46 ` Joel Reymont
@ 2007-04-30 12:58 ` Nicolas Pouillard
2007-04-30 13:06 ` Joel Reymont
0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Pouillard @ 2007-04-30 12:58 UTC (permalink / raw)
To: Joel Reymont; +Cc: Caml List
On 4/30/07, Joel Reymont <joelr1@gmail.com> wrote:
>
> On Apr 30, 2007, at 1:42 PM, Nicolas Pouillard wrote:
>
> > The generator needs to see youre types definitions.
>
> What does it mean? Open <AST module> doesn't do it?
>
> How do I make the type defs in a separate module available to the
> generator?
>
Here is the hack I use:
module Camlp4Trash = struct
INCLUDE "AST.ml";;
end;;
Use camlp4of (since INCLUDE is provided by camlp4 macros).
Also add -filter trash to the camlp4 options (after map and fold), in
order to remove the Camlp4Trash module.
--
Nicolas Pouillard
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] AST transformation and scrapping boilerplate code
2007-04-30 12:58 ` Nicolas Pouillard
@ 2007-04-30 13:06 ` Joel Reymont
2007-04-30 13:09 ` Nicolas Pouillard
0 siblings, 1 reply; 11+ messages in thread
From: Joel Reymont @ 2007-04-30 13:06 UTC (permalink / raw)
To: Nicolas Pouillard; +Cc: Caml List
On Apr 30, 2007, at 1:58 PM, Nicolas Pouillard wrote:
> Use camlp4of (since INCLUDE is provided by camlp4 macros).
> Also add -filter trash to the camlp4 options (after map and fold), in
> order to remove the Camlp4Trash module.
Including the AST module worked and I'm almost there.
+ ocamlfind ocamlc -package ounit -c -I +camlp4 -pp 'camlp4of -filter
map -filter fold -filter trash ' -o easy_strip.cmo easy_strip.ml
File "ghost-location", line 1, characters 0-0:
Unbound type constructor var_decl
This is surprising since the AST looks like this, i.e. var_decl is
part of it.
What is the explanation for this and how should I fix it?
...
and statement =
[
...
| `VarDecls of var_decl list
...
]
...
and var_decl =
[
| `VarDecl of id * ty * expr
]
Thanks, Joel
--
http://wagerlabs.com/
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] AST transformation and scrapping boilerplate code
2007-04-30 13:06 ` Joel Reymont
@ 2007-04-30 13:09 ` Nicolas Pouillard
0 siblings, 0 replies; 11+ messages in thread
From: Nicolas Pouillard @ 2007-04-30 13:09 UTC (permalink / raw)
To: Joel Reymont; +Cc: Caml List
On 4/30/07, Joel Reymont <joelr1@gmail.com> wrote:
>
> On Apr 30, 2007, at 1:58 PM, Nicolas Pouillard wrote:
>
> > Use camlp4of (since INCLUDE is provided by camlp4 macros).
> > Also add -filter trash to the camlp4 options (after map and fold), in
> > order to remove the Camlp4Trash module.
>
> Including the AST module worked and I'm almost there.
>
> + ocamlfind ocamlc -package ounit -c -I +camlp4 -pp 'camlp4of -filter
> map -filter fold -filter trash ' -o easy_strip.cmo easy_strip.ml
> File "ghost-location", line 1, characters 0-0:
> Unbound type constructor var_decl
Some open AST is missing I think.
--
Nicolas Pouillard
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2007-04-30 13:09 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-29 14:40 AST transformation and scrapping boilerplate code Joel Reymont
2007-04-30 8:21 ` [Caml-list] " Nicolas Pouillard
2007-04-30 8:41 ` 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox