Getting type data in PPX


Let’s say, in PPX I have MyModule.t core_type. What are my options, if any, to read into this type?

Bruteforce option: manually find and read MyModule.{rei,mli,re,ml}, then parse it to AST somehow. But I wouldn’t like to be doing it.


AFAIK this can’t be done in a robust way at the PPX level. MyModule could be another file, a module in the same file, a local module, including one arising from a computation involving functors or first-class modules, etc. It can be brought into scope by opening or including another module, whose name you don’t know from looking at the type. That other module might be the output module in some functor computation, etc.

For reference, odoc has to solve the problem of resolving types, and it uses Typedtree rather than Parsetree, and there is currently a rewrite of its core going on to make this better.


Riiiight, that’s quite a rabbit hole. Some doc-level restrictions can be applied but still it’s not robust as you mentioned.

AFAIU since PPX stage happens before type-checking so no type info is available yet and Typedtree isn’t useful here, is this correct?


Not sure what you mean, is the question that Typedtrees aren’t available? If so, the answer is yes, they aren’t available yet. Only a Parsetree is.


I mean that when PPX transformation is happening, type-checking hasn’t happened yet. And MyModule wasn’t resolved yet, it’s only parsed ast node. So typed data can’t exist at this point, only after type-checking. If my understanding is correct.


I also had the same doubt recently