# Code execution in compile time

**URL:** <https://discuss.ocaml.org/t/code-execution-in-compile-time/10725>\
**Category:** Ecosystem\
**Tags:** metaprogramming, ppx\
**Created:** [October 29, 2022, 6:49am UTC](https://discuss.ocaml.org/t/code-execution-in-compile-time/10725 "2022-10-29T06:49:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![egoholic](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/egoholic/32/574_2.png) [@egoholic](https://discuss.ocaml.org/u/egoholic)\
**Post date:** [October 29, 2022, 6:49am UTC](https://discuss.ocaml.org/t/code-execution-in-compile-time/10725/1 "2022-10-29T06:49:59Z")

</div>

I’ve not used PPX yet, but the examples of its use looks ugly to me (sorry, but that’s mine opinion as unexperienced developer in OCaml). Few years ago I came to the idea of let’s call it staged execution, when we have at least two stages: one for compile time and one for runtime and later I found something similar already implemented in Zig and few other languages. I’m curious if it is possible to perform something similar in OCaml by keeping application code to look like normal OCaml code? What the drawbacks of such an approach? Or maybe you just can explain me why I’m wrong and PPX is a great tool.

---

<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:** [October 29, 2022, 7:04am UTC](https://discuss.ocaml.org/t/code-execution-in-compile-time/10725/2 "2022-10-29T07:04:47Z")

</div>

You will be interested by MetaOCaml [BER MetaOCaml](https://okmij.org/ftp/ML/MetaOCaml.html). See also [MetaOCaml GitHub Community · GitHub](https://github.com/metaocaml).

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![egoholic](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/egoholic/32/574_2.png) [@egoholic](https://discuss.ocaml.org/u/egoholic)\
**Post date:** [October 29, 2022, 7:27am UTC](https://discuss.ocaml.org/t/code-execution-in-compile-time/10725/3 "2022-10-29T07:27:09Z")

</div>

Thank you for your response! It looks like the project is dead, because the last version of OCaml, that MetaOCaml supports is 4.11 and the last commit was 2 years ago.

---

<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:** [October 29, 2022, 7:45am UTC](https://discuss.ocaml.org/t/code-execution-in-compile-time/10725/4 "2022-10-29T07:45:25Z")

</div>

My understanding is that the project is very much alive, it is just that syncing with the latest version of OCaml is not a priority. @yallop will know more.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![jbeckford](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jbeckford/32/3027_2.png) [@jbeckford](https://discuss.ocaml.org/u/jbeckford)\
**Post date:** [October 29, 2022, 8:24am UTC](https://discuss.ocaml.org/t/code-execution-in-compile-time/10725/5 "2022-10-29T08:24:53Z")

</div>

I like [metaquot](https://github.com/thierry-martinez/metaquot#readme) (ppx!) as an alternative for staged execution. It is a bit lower-level, but if you:

1. Generate OCaml AST expressions with [metaquot](https://github.com/thierry-martinez/metaquot#readme) and [metapp](https://github.com/thierry-martinez/metapp#readme)
2. Make a Dune `(executable (name gen) (modules ...) (preprocess (pps metaquot.ppx)))` and a `(rule (target stage1.ml) (actions (run gen %{target})))` that will print the OCaml AST into `stage1.ml`

then you can use `Stage1` as a module in your other code (etc).

The [metapp](https://github.com/thierry-martinez/metapp#readme) page describes its differences from `ppx_stage` (which I don’t know about).

---

<div class="post-metadata">

**Author:** ![cemerick](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cemerick/32/1383_2.png) [@cemerick](https://discuss.ocaml.org/u/cemerick)\
**Post date:** [November 10, 2022, 7:15pm UTC](https://discuss.ocaml.org/t/code-execution-in-compile-time/10725/6 "2022-11-10T19:15:05Z")

</div>

As was posted recently in the OCaml compiler “newsletter” ([OCaml compiler development newsletter, issue 6: March 2022 to September 2022](https://discuss.ocaml.org/t/ocaml-compiler-development-newsletter-issue-6-march-2022-to-september-2022/10777/1)), it seems that work on native macros is starting up again (roughly equivalent to TH or MetaOCaml, but module-aware), per @yallop[https://www.cl.cam.ac.uk/~jdy22/projects/modular-macros/](https://www.cl.cam.ac.uk/~jdy22/projects/modular-macros/)

Obviously not anything ready for prime time yet, but it’s great to see progress being made in this area.
