# PPX and reflection

**URL:** <https://discuss.ocaml.org/t/ppx-and-reflection/1946>\
**Category:** Ecosystem\
**Tags:** ppx\
**Created:** [May 3, 2018, 12:22pm UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946 "2018-05-03T12:22:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![progman](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@progman](https://discuss.ocaml.org/u/progman)\
**Post date:** [May 3, 2018, 12:22pm UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/1 "2018-05-03T12:22:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zozozo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zozozo/32/59_2.png) [@zozozo](https://discuss.ocaml.org/u/zozozo)\
**Post date:** [May 3, 2018, 1:20pm UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/2 "2018-05-03T13:20:09Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![smolkaj](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/smolkaj/32/266_2.png) [@smolkaj](https://discuss.ocaml.org/u/smolkaj)\
**Post date:** [May 3, 2018, 1:51pm UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/3 "2018-05-03T13:51:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![progman](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@progman](https://discuss.ocaml.org/u/progman)\
**Post date:** [May 3, 2018, 2:23pm UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/4 "2018-05-03T14:23:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![progman](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@progman](https://discuss.ocaml.org/u/progman)\
**Post date:** [May 3, 2018, 2:53pm UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/5 "2018-05-03T14:53:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![smolkaj](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/smolkaj/32/266_2.png) [@smolkaj](https://discuss.ocaml.org/u/smolkaj)\
**Post date:** [May 3, 2018, 4:22pm UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/6 "2018-05-03T16:22:51Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![pveber](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/pveber/32/44_2.png) [@pveber](https://discuss.ocaml.org/u/pveber)\
**Post date:** [May 4, 2018, 8:59pm UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/7 "2018-05-04T20:59:26Z")

</div>

I think Jun Furuse has done quite some work in this direction. How about [typpx](https://bitbucket.org/camlspotter/typpx) or [ppx\_implicits](https://bitbucket.org/camlspotter/ppx_implicits)?

---

<div class="post-metadata">

**Author:** ![progman](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@progman](https://discuss.ocaml.org/u/progman)\
**Post date:** [May 5, 2018, 4:31am UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/8 "2018-05-05T04:31:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![etc](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/etc/32/2972_2.png) [@etc](https://discuss.ocaml.org/u/etc)\
**Post date:** [May 5, 2018, 4:54am UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/9 "2018-05-05T04:54:24Z")

</div>

I’ve also been working on a typing ppx [here](https://github.com/cyrus-/relit/tree/ppx_clone). It does some pretty strange things but I’m definitely happy to talk about / learn more about typing ppx’s

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [May 5, 2018, 12:08pm UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/10 "2018-05-05T12:08:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![etc](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/etc/32/2972_2.png) [@etc](https://discuss.ocaml.org/u/etc)\
**Post date:** [May 6, 2018, 3:21am UTC](https://discuss.ocaml.org/t/ppx-and-reflection/1946/11 "2018-05-06T03:21:20Z")

</div>

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](https://www.cs.cmu.edu/~comar/omar-thesis.pdf). Typing is used to resolve a name so you can ‘import’ a literal notation exactly as you would in OCaml.
