Questions tagged [ppx]

Questions about the use or developpement of PPXes (OCaml syntax extension)

PPX Syntax Extensions

ppx is the syntax extension format supported currently by OCaml. PPX rewriters are preprocessors for OCaml programs that are applied to the code before passing it on to the compiler. They work over the AST resulting from its parsing, and compute a new AST that will be actually compiled.

Extension Nodes

OCaml features syntax extensions that are meant to be used by external tools. The kind of extensions exist which are attributes and extension nodes.

  1. Attributes are “decorations” of the syntax tree which are mostly ignored by the type-checker but can be used by external tools.
  2. Extension nodes are generic placeholders in the syntax tree. They are rejected by the type-checker and are intended to be “expanded” by external tools such as -ppx rewriters.

The -ppx option

ocamlc and ocamlopt both take a -ppx command line option. This option takes as argument a program that is executed during the compilation of a file in order to transform it on the fly. This program is called a ppx rewriter. More precisely, once the OCaml compiler has parsed the source file and constructed the corresponding AST, it runs the ppx with this AST as input. The ppx returns a new transformed AST and the compiler continues the compilation process with this new AST, discarding the original one.

Several -ppx options can be passed to the compiler. In this case, the compiler will apply the various ppx rewriters one by one, each one feeding its output to the next one.

Using dune

It is easy to integerate a PPX preprocessing to a workflow. If you want to pass command line flags that do not start with a -, you can separate library names from flags using --. So for instance from the following preprocess field:

(preprocess (pps ppx1 -foo ppx2 -- -bar 42))

ppxlib

The modern solution for writing PPX extensions. Without this library, writing PPX extensions is fragile and breaks with OCaml version changes. ppxlib merges several older projects together to provide a complete platform for writing efficient, resilient PPX extensions.

OCaml-migrate-parsetree

This library converts OCaml parsetrees between different major versions thus making a PPX written for a given version of OCaml portable with older versions.

30 questions
9
votes
1 answer

Writing a parser for a DSL in OCaml using ppx and extension point

Recently, it was announced in OCaml official github that Camlp4 is replaced by ppx rewriters and extension points (https://github.com/ocaml/camlp4): Camlp4 was part of the official OCaml distribution until its version 4.01.0. Since then it has…
Trung Ta
  • 1,582
  • 1
  • 16
  • 25
5
votes
2 answers

Is this possible to create an operator that evaluates argument left-to-right in OCaml

When you define an operator such as let (++) a b = a :: b When you do let v = foo a ++ bar b bar is evaluated before foo. A workaround is to use let expression, i.e. let e1 = foo a in let e2 = bar b in e1 ++ e2 However, sometimes it'd be handy…
Butanium
  • 726
  • 5
  • 19
5
votes
1 answer

How to handle different OCaml versions when generating AST with ppxlib

I am making a ppx extension rewriter as part of a code library Ideally the library will be usable with some range of OCaml versions I have noticed that when building AST nodes to output from my rewriter it is unavoidable to have to construct some…
Anentropic
  • 32,188
  • 12
  • 99
  • 147
4
votes
0 answers

Incomplete expression in ppx extension

I want to try to write my own ppx to allow named arguments in formatting strings: From Format.printf [%fmt "!(abc) !(qsd)"] to Format.printf "%s %s" abc qsd When dumping with ppx_tools I want to go from: {pexp_desc = Pexp_apply ({pexp_desc =…
Lhooq
  • 4,281
  • 1
  • 18
  • 37
4
votes
1 answer

how to derive to_string methods for data types using ppx

How can I derive to_string methods for data types using ppx with jbuilder? For instance, I'm trying to use @@deriving show to derive show_* methods for a data type. I have a simple main.ml file that looks like this: open Core type foo = Bar | Baz …
illabout
  • 3,517
  • 1
  • 18
  • 39
4
votes
1 answer

How to use jbuild and ppx_driver with ppx_deriving

I am trying to use jbuilder together with ppx_deriving (ppx_deriving_yojson specifically) but got stuck for well over an hour now. My current approach is a jbuild file, containing the following: (jbuild_version 1) (executables ((names…
phaer
  • 51
  • 2
4
votes
1 answer

OCaml: how to use batteries and ppx_deriving.* together?

Currently I'm trying to use Batteries with ppx_deriving.show or something similar. I am wondering how to use them together usefully. To create dumping functions, I feel ppx_deriving.show is useful. But I have a little bit troubles using them…
nomaddo
  • 416
  • 3
  • 12
4
votes
1 answer

Make compatible ocaml, camlp4, ppx, node, js_of_ocaml, ocamlbuild

After installing npm and node, compiling OCaml files with js_of_ocaml gave errors, thus I did opam switch reinstall system: :testweb $ opam switch reinstall system Your system compiler has been changed. Do you want to upgrade your OPAM installation…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
4
votes
2 answers

OCaml attributes

I was looking at the manual and found that there are attributes in OCaml for declaring things as deprecated (see http://caml.inria.fr/pub/docs/manual-ocaml/extn.html), but I can not figure out how to get them to be recognized by the compiler. Here's…
Gregory
  • 1,205
  • 10
  • 17
3
votes
1 answer

How to write custom ppx decorator to rescript?

I need to generate a value with a different type from my passed type. This is the first time I write on ocaml-like, and for example, in a familiar me haskell I would use Data.Generics. How I have understood I need to use decorator and ppx. I wrote…
Gleb Patcia
  • 365
  • 2
  • 15
3
votes
1 answer

Any good usage examples of an AST transformation with ppx_driver (register_transformation_using_ocaml_current_ast)?

tl;dr I'm trying to make a source transformation binary using AST_mapper and ppx_driver. I can't figure out how to get the example in the AST_mapper docs to be used by ppx_driver. Are there any good examples of how to use…
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
2
votes
1 answer

How to use ppx derive in utop?

How can I use ppx derive (https://github.com/ocaml-ppx/ppx_deriving) in my utop? For example, I have to following code: module A = struct module T = struct type t = int [@@deriving hash, sexp, compare] end include…
David 天宇 Wong
  • 3,724
  • 4
  • 35
  • 47
2
votes
2 answers

What are PPXes?

In many projects using ReasonML, the acronym PPX is often used, but rarely explained. What are PPXes? Why does ReasonML need them?
Enieber
  • 87
  • 6
2
votes
2 answers

How do I setup ocamlinit so that ppx works?

This is my ocamlinit: (* Added by OPAM. *) let () = try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with Not_found -> () ;; (* ## added by OPAM user-setup for ocamltop / base ## 3ec62baf6f9c219ae06d9814069da862 ## you can edit, but…
ackerleytng
  • 416
  • 6
  • 17
2
votes
1 answer

OCaml code transformed through my PPX syntax extension can't find other packages

So, I've written a simple PPX for OCaml that transforms a silly DSL into some library calls [%jsx div] Becomes something like ReactJS.create_element "div" However, whenever I attempt to compile it, I get 'Unbound value ReactJS.create_element'.…
1
2