Metaprogramming and PPX
PPX Syntax Extensions
ppx is the syntax extension format supported currently by OCaml. It replaces older techniques such as Camlp4 by limiting the scope of language extensions and dedicating to them a specific syntax.
- 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.
Articles
- A Guide to PreProcessor EXtensions
- Introduction to the PPX Ecosystem:
Nice and thorough introduction to writing PPXs using
ppxlib
. - A Guide to Extension Points in OCaml
- Extension Points, or how OCaml is becoming more like Lisp
- A guide to writing PPX Deriving plugins
- ppxlib as the new standard
- Older: The Future of PPX: post on discuss
PPX Extensions
- ppx_enum:
A nice, simple ppx using
ppxlib
that serves as a good example for potential ppx writers. - ppx_deriving:
Type-based framework for ppx extensions.
Contains built-in plugins for
show
,eq
,ord
,enum
,iter
,map
,fold
, andmake
.- NOTE:
ppx_deriving
currently doesn’t make use ofppxlib
, making it hard to update for future OCaml releases. As powerful asppx_deriving
is, we recommend trying to find appxlib
-based extension instead, which will then be a lot more robust going forward.
- NOTE:
- ppx_show:
A
show
deriver, creating functions for printing out values. Usesppxlib
, which makes it forward-compatible. - ppx_visitors: Automatically use the visitor object-oriented pattern on a data structure, extending it with behaviors rather than needing to specify each variant’s behavior.
- ppx_import:
Import is a syntax extension that allows to pull in types or signatures from other compiled interface files.
This can be handy when not wanting to repeat a type in both the
.ml
and.mli
file, for example. - ppx_override:
Override allows you to import a module or its types, and then easily change and modify aspects, such as
using
ppx_deriving
on that module’s types. - ppx_optcomp:
Conditional compilation like
#ifdef
for OCaml. - ppx_string_interpolate:
A simple ppx filter to support string interpolation like
[%str "value of foo is $(foo)"]
. - ppx_monad: Monadic syntax extension.
- ppx_let: A monadic syntax extension from Jane Street.
- ocaml-monadic: Another monadic syntax extension.
- ppx_regex:
Contains 2 ppx parsers to OCaml regex libraries:
- ppx_regexp: maps to use Re (untyped regex)
- ppx_tyre: maps to use Tyre for typed regex.
- ppx_expect: Cram-like tests for OCaml. See Testing.
- Bisect_ppx: Code coverage for OCaml. See Code Tools.
- ppx_pgsql: A syntax extension for embedded SQL queries using PG’OCaml. See Databases.
- ppx_cstubs: Write C functions directly in your OCaml code. See FFI.
- ppx-tyxml:
Convert html/xml syntax to
tyxml
function calls.
Protocol-related
- ppx_deriving_yojson: A Yojson codec generator for OCaml. See Serialization.
- ppx_yojson_conv:
Alternative to
ppx_deriving_yojson
from Jane Street. - ppx_yojson:
Convert JSON expressions to
yojson
AST, allowing your code to be cleaner. - ppx_deriving_protobuf: Derive Protobuf files from OCaml types. See Protocols.
- ppx_sexp_conv: Derive converters to s-expressions.
- ppx_protocol_conv: Framework for multiple serializers for different protocols.
Other
- cppo: A simple C++-like preprocessor for OCaml files.
- MetaOCaml: An OCaml dialect for multi-stage programming.
- metapp:
A ppx extension that provides metaquoting facilities similar to
MetaOCaml
. - Fan: Fan is a compile-time metaprogramming system for OCaml, originally inspired from Camlp4. It’s a combination of OCaml and Lispy Macros. It shares the same concrete syntax with OCaml.
- camlp4: Camlp4 is an older way of modifying OCaml syntax and applying metaprogramming. It is generally discouraged nowadays – use ppx instead.
- camlp5: Another variant of metaprogramming that is discouraged nowadays.