[wg-camlp4] [Caml-list] Working Group: the future of syntax extensions in OCaml, after camlp4

Jeremy Yallop yallop at gmail.com
Tue Jan 29 09:05:29 GMT 2013


On 29 January 2013 07:44, Alain Frisch <alain at frisch.fr> wrote:
> Let me throw a crazy idea: maybe we could merge the grammar for patterns and
> expressions, and let the type checker reject bad patterns and bad
> expressions from this more admissible class.  This would give plenty of
> space for extensions.

Indeed, this opens up lots of possibilities.  For example, consider
the binding form introduced by omonad:

    perform (
       patt <-- expr1;
       expr2
   )

which is desugared to

   bind expr1 (function patt -> expr2
                      | _    -> fail "...")

At the moment the "new" syntax is handled by parsing 'patt' as an
expression and then translating it to a pattern in the AST
transformer:

    https://github.com/danmey/omonad/blob/master/src/ppx_monad.ml#L115-L134

This works pretty well, except for the few pattern forms that don't
look like expressions.  If patterns were parsed as expresssions then
there wouldn't be any such gaps, and the introduction of the new
binding form would be more convincing.

Other campl4 extensions such as Richard Jones' bitstring (Erlang-style
matching over bits) and Martin Jambon's micmatch (pattern matching for
regular expressions) could also be implemented as AST transformers if
the grammars were merged.

> Assuming it is indeed technically possible, do you think it would be a good
> idea to merge the syntactic categories of patterns and expressions?

I think it would be a good idea.  It'd also eliminate a few dozen
productions from the grammar, which is probably no bad thing.


More information about the wg-camlp4 mailing list