PPX and reflection

Can ppx be used to capture a ocaml type inferred for an expression? (typedtree)
I wish to transform what I expect to be a concrete type (no variables) to a simpler
representation for injection into code.
or is the best that can be done is transforming type declarations directly?
thanks.

Afaik, ppx are applied on the parsetreee, before typechecking, so while it would be technically possible in a ppx to invoke the typechecker, apply some transformation to it, then transform it back to a parsetree, it seems very complicated.

Maybe with a more detailed explanation of what you want to do, it’d be easier to suggest another way ?

Yes. See https://github.com/ocaml/dune/issues/193 for a relevant discussion.

there is a typedtree hook in the default_mapper which I believe would have given
the necessary type information but I think the manual is saying the ocaml team are keeping it abstract to prevent meddling.

I wanted to capture the type of some use of a specially identified expression (say, Reflect.typeof: 'a -> 'a Reflect.t as applied to some value) for transformation to a simpler constructor type representing a concrete type.
this would then be included as an argument in a re-written parsetree of a function application. By that means I would have some dynamic typing information for use in database querying.

smolkaj
I didn’t understand any of that discussion.
are you saying type definitions are being used in the way I am looking for?

I’m not sure. I just thought it might be a helpful pointer for two reasons:

  • the discussion mentions other ppx rewriters relying on type information
  • the discussion mentions technical challenges in supporting these rewriters in the dune build system (which is used by a large number of projects now)

I think Jun Furuse has done quite some work in this direction. How about typpx or ppx_implicits?

1 Like

that’s the one! thank you pveber.
and thank you too smolkaj/zozozo.

I’ve also been working on a typing ppx here. It does some pretty strange things but I’m definitely happy to talk about / learn more about typing ppx’s

Your repo terribly lacks the README describing what is this repo about…

This is intentional — the code isn’t at all ready for release. I’m just hoping to show another example of a typing ppx if people are interested, as it seems are not many. The purpose of the code is to do some extra parsing at compile time to enable literals for any data, among other things, working of off this research. Typing is used to resolve a name so you can ‘import’ a literal notation exactly as you would in OCaml.