# Conditional compilation based on inferred types?

**URL:** <https://discuss.ocaml.org/t/conditional-compilation-based-on-inferred-types/7772>\
**Category:** Learning\
**Created:** [May 2, 2021, 9:35am UTC](https://discuss.ocaml.org/t/conditional-compilation-based-on-inferred-types/7772 "2021-05-02T09:35:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rixed](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rixed/32/56_2.png) [@rixed](https://discuss.ocaml.org/u/rixed)\
**Post date:** [May 2, 2021, 9:35am UTC](https://discuss.ocaml.org/t/conditional-compilation-based-on-inferred-types/7772/1 "2021-05-02T09:35:21Z")

</div>

I haven’t followed the evolutions of the macro system nor plugins recently, and I’m wondering if there is anything available these days that would make it possible to select at compilation time a bloc or code or another based on the inferred type of some variable.

I’m thinking of something along the lines of:

```ocaml
let length x =
#match type_of(x) with
# | _ list ->
    List.length x
# | _ string ->
    String.length x
# | _ -> type_error

```

That would come handy for some lightweight metaprogramming.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [May 2, 2021, 10:26am UTC](https://discuss.ocaml.org/t/conditional-compilation-based-on-inferred-types/7772/2 "2021-05-02T10:26:37Z")

</div>

I don’t think this is possible at the moment. You can implement something like your function using a GADT:

```auto
type _ ty = List : 'a list ty | String : string ty 

let length : type a. a ty -> a -> int = fun ty x ->
  match ty with
  | List -> List.length x
  | String -> String.length x

```

so that you can do `length String "hola"` and `length List [1; 2]`, but the type argument needs to be explicitly passed, it is not inferred.

Cheers,  
Nicolás

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [May 2, 2021, 5:35pm UTC](https://discuss.ocaml.org/t/conditional-compilation-based-on-inferred-types/7772/3 "2021-05-02T17:35:39Z")

</div>

Jun Furuse did something like this in `typpx`. I came across it based on a discussion here, and noticed that the project seemed abandoned, so I brought it up-to-date with ocaml 4.10.0 a while back. Jun might have updates to the original project, too. In any case, `https://github.com/chetmurthy/typpx`

---

<div class="post-metadata">

**Author:** ![igarnier](https://avatars.discourse-cdn.com/v4/letter/i/d78d45/32.png) [@igarnier](https://discuss.ocaml.org/u/igarnier)\
**Post date:** [May 2, 2021, 8:40pm UTC](https://discuss.ocaml.org/t/conditional-compilation-based-on-inferred-types/7772/4 "2021-05-02T20:40:14Z")

</div>

He did revive it a while ago, you can find it here [Files · master · Jun Furuse / typpx · GitLab](https://gitlab.com/camlspotter/typpx/-/tree/master)

---

<div class="post-metadata">

**Author:** ![rixed](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rixed/32/56_2.png) [@rixed](https://discuss.ocaml.org/u/rixed)\
**Post date:** [May 4, 2021, 4:48pm UTC](https://discuss.ocaml.org/t/conditional-compilation-based-on-inferred-types/7772/5 "2021-05-04T16:48:48Z")

</div>

Just had a look, I guess you meant more specifically the `type_of` ppx in [examples/ppx\_type\_of · master · Jun Furuse / typpx · GitLab](https://gitlab.com/camlspotter/typpx/-/tree/master/examples/ppx_type_of).

Unfortunately, this produces the type (as a string) in the runtime. But to be able to dispatch on the type of some input there must be a preprocessor that selects the appropriate code. Otherwise, I can’t think of any useful specialisation one could implement with that, given the code still has to manipulate the variable which type has been “revealed” by `type_of` in a generic way only.  
Or at least that’s my understanding based on the READMEs.

But maybe you meant that one could use TyPPX as a guide to implement such a preprocessor.  
Well, that may well be true, but I’ve dipped my toes in PPX once and I’m not in a hurry to get back into it…

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [May 4, 2021, 8:53pm UTC](https://discuss.ocaml.org/t/conditional-compilation-based-on-inferred-types/7772/6 "2021-05-04T20:53:49Z")

</div>

> [@rixed](#):
>
> But maybe you meant that one could use TyPPX as a guide to implement such a preprocessor.  
> Well, that may well be true, but I’ve dipped my toes in PPX once and I’m not in a hurry to get back into it…

Yes, this is what I meant. My original purpose in getting TyPPX working again, was to try to do this. But it seemed like there was a large hill to climb in dealing with typed terms, and I didn’t have the proper tooling to do it well, so I put it off. Specifically, they’re an entire different language (set of AST types) than untyped terms, and I think that any viable approach will have to translate _back_ to untyped terms, annotated with types (which would require an entirely new design). But then there are “environments” and such, and this starts to get out-of-control pretty quickly. So, yeah, I decided it was too much to bite off in one go. I have vague ideas of using attributed-trees and hash-consing as a way of making this viable, but that’s just a vague handwaving mumble.

I don’t see PPX as a significant barrier, personally, but then, I write PPX rewriters all the time, so obviously YMMV.

In any case, I agree with you, that it’s a hard nut to crack.
