PPX transformation from a Typed AST


I’m interested in writing source-to-source program transformation that exploit some typing information. Thus, I’m looking for a variant of ast_mapper that would provide as input a typed AST (typedtree) and produces an untyped AST (parsetree), so that I don’t have to bother rebuilding valid types.

Afaik, the typed AST carries the required information for rebuilding the untyped AST. (Note that I only need this to work for the core language.)

Has anyone written such a variant of the ast_mapper?
Anyone seeing particular difficulties in doing so?


The Untypeast module from the compiler libraries (typing/untypeast.ml in the source repository) is providing that, it might be good enough for your use case.
(As far as I remember, the typedtree is not very faithful, if you want to stay very close to the original file it might be problematic).


Thanks a lot, I’ll try using it.

(I had missed this file, despite searching for some ppx documentation, and reading blog posts about it.)

The typedtree is pretty faithful nowadays and keep almost all the information (it’s sometimes difficult to find them, but untypeast put everything back together).
A few versions ago, I tested that the composition of typing and untyping was the identity. It worked fine.

1 Like

Did you take a look at camlspotter’s excellent typpx?
I’m not sure typed ppxs work well with dune though. At least I had trouble when I tried 6 months ago.

1 Like

I am wondering how to smoothly integrate type-directed transformations in the compilation chain, just like “ppx” are handled.

By combining the application of a mapper (in the sense of “untypeast.ml”) from a typed AST to an untyped AST, together with a pass of retyping, we could get conveniently define transformations from typed AST to typed AST.

Thus, we may wish to add an option similar to -ppx, but just for rewriters operating at the typedtree level. Would that make sense?

This seems to have disappeared from bitbucket. Thankfully opam has a cached copy, which I was able to download, but … does anybody know where it moved to?

I’ve been thinking that maybe it might be useful to write a linter starting from the typed tree.