[wg-camlp4] My experience with syntax extension

Romain Bardou romain.bardou at inria.fr
Mon Jan 28 14:32:53 GMT 2013


Hello,

Here is my experience with syntax extension, if it can help you. I know 
the goal of -ppx is not syntax extension but more AST rewriting, but well.

- When I was younger I wished for some easy way to add constructs such 
as foreach or stuff like that. Now I'm more for stability, reliability, 
uniformity, simplicity, anything which ensures my project will compile 
easily everywhere (even by, let's say, an impatient non-programmer on 
Windows). And I'm more used to functional programming idioms which make 
those syntax extensions irrelevant.

- I tried CamlP5 (when it was still CamlP4), had some fun, then CamlP4 
arrived with no documentation and lost me.

- I don't use any preprocessor at all nowadays. I think changing the 
syntax does not ease the compilation process nor the maintenance process 
(hard to read a non-standard syntax 6 months later — and lack of support 
from emacs). I've always found other ways.

- The ones I have felt the most need for are local open — which is now 
part of the language — and Lwt-like syntax, especially for let-like 
constructions — which seems to be considered by this working group as a 
potential candidate for official support in some way.

- I'm the author of Melt, which is a preprocessor. But I consider Melt 
to be a domain-specific language which happens to be friendly to OCaml 
users, not an OCaml extension. I did not use CamlP4, because I lacked 
the expertise — as a result, error messages are terribly located.

- I think the Melt preprocessor could be rewritten as an AST rewriter 
using -ppx. This would probably be for the best, except for the special 
approach of quotations from Melt… Which is actually rather important though.

- I understand the OCaml AST quite well, and to me -ppx sounds simple 
and accessible.

- I use custom operators such as "let (++) =" when it's reasonable.

- I don't like the external, "meta" annotations (-pp in Makefile, 
mainly), which are required to compile a program which uses a 
preprocessor. Custom operators are great in that regard: they are a way 
to provide features which look like syntax extensions but which are 
actually part of the language.

- I would really like a way to specify, in a .ml file itself, that the 
file must be preprocessed using some given tool. This would remove the 
need to change the build process, and would make it clear to the reader 
of the file that it *must* be read with the extension in mind. This may 
not be easy for generic extenders such as CamlP4 but for a tool such as 
-ppx whose purpose is actually to use the official syntax, I think it 
should be discussed. If an extension is optional, for instance if it 
only adds debugging information, then it makes sense to use -ppx as an 
optional compilation flag.

- (somehow related, but not really) I found the recent changes to how 
tuareg indents my code very annoying, especially when my code has a lot 
of parentheses. Syntax extensions which allow me to have less 
parentheses have bonus points from me.

Overall, I'm very much in favor of -ppx + generic annotations entry 
points in official OCaml + official integration of largely recognized 
extensions. I would consider using your proposal, but probably never CamlP4.

Cheers,

-- 
Romain Bardou


More information about the wg-camlp4 mailing list