[wg-camlp4] Negative field report: extension points unsuited for (matches p when e)

Hongbo Zhang hongboz at seas.upenn.edu
Wed May 1 16:13:17 BST 2013


I just wrote a similar extension in fan

open Syntax;
{:create|Gram pred|};;
{:extend|
pred:
  [pat{p};"when"; exp{e} -> {:exp| function | $pat:p when $e -> true |_ ->
false |}
  |pat{p} -> {:exp| function | $pat:p -> true | _ -> false |} ]
|};;

---- test----
let a = {:pred|('a'|'b' as x) when x='a'|} 'a';;

if a then print_string "true" else print_string "false";;
(*true *)
------------------------------------

Compared with Camlp4, it's guaranteed to be functional and you could see
some work is put into make Fan's syntax a subset of OCaml's original syntax.





On Wed, May 1, 2013 at 10:44 AM, Gabriel Scherer
<gabriel.scherer at gmail.com>wrote:

> The recent caml-list message by rixed (archived below) motivated me to
> get pa_matches in shape (it lacked support for "when ..."), and try to
> see how a similar extension could be implemented with the new
> extension_points branch.
>
> The Camlp4 extension allows a new syntax for expressions, (matches
> <pattern>) or (matches <pattern> when <expr>), that desugars into a
> test function returning a boolean: (function <pattern> when <expr> ->
> true | _ -> false). I have attached to this mail the relevant code,
> called pa_matches.ml (pa_ is a traditional prefix for Camlp4 *parser*
> extensions).
>
> I quickly found out that extension points as currently implemented in
> Alain's branch are ill-suited for this task, because the piece of code
> we want to parse (<patt> when <expr>) is not syntactically an
> expression. [%matches <patt> when <expr>] would be rejected by the
> parser.
>
> Note that this is just an experience report, and not a value judgment
> on the extension_points approach itself. I think the choice to only
> accept syntactically valid expressions (in fact structure items) has a
> lot of merits (notably wrt. toolability). The price to pay is that
> potential extension have to be massaged into syntactically correct
> pieces, with a potential loss of concision when compared to
> domain-specific syntax designs. This indicates, I think, that
> extension_points is not always suited for "syntax changes in the
> small", where a very concise syntax is given to an otherwise
> expressible expression (and concision is thus essential for the
> purpose of the extension), except when this extension exactly fits the
> syntactic form of the more high-level OCaml syntactic classes.
>
> For the purpose of experimentation, I still decided to do an
> implementation based on a horrible hack, namely using (the new syntax
> for) string literals that had been presented as a way to get "foreign
> language quotations" in this context, with the syntax [%matches {|
> <patt> when <expr> |}]. I believe this course of action should be
> discouraged; in particular, there is no proper error handling. It
> seems that deploying extensions based on this principle would
> constitute a regression from the current Camlp4 situation.
>
> Note that this implementation technique might be made slightly more
> reasonable if the following ingredients were present:
> - a simple and correct way to add location information from a lexing
> buffer obtained from "Lexing.from_string", to inform the lexer of
> where in the file it's picking up tokens (note that in the above case,
> this is not enough as the quoted string is prefixed with some code to
> make it a valid expression)
> - more entry points in the parser for eg. patterns and match actions
> - maybe support from the ppx wrapper to catch and re-route OCaml errors
>
> I think this is all rather cumbersome and we should rather understand
> and accept the limitations of extension points -- and make unruly
> users respect them!
>
> On Sat, Apr 27, 2013 at 10:04 AM, Gabriel Scherer
> <gabriel.scherer at gmail.com> wrote:
> > This is a subset of Richard Jones' "matches" syntax extension, turning
> > an pattern matching on 'a into a ('a -> bool) function:
> >
> >   if (matches Some x when x > 15) variable then ...
> >
> > (The original extension did not handle "when", but it would be quite
> > easy to add.)
> >
> > On Sat, Apr 27, 2013 at 9:18 AM,  <rixed at happyleptic.org> wrote:
> >> Wouldnt it be nice if, as a special form of the = operator,
> >> "variable = pattern when condition" were equivalent to
> >> "match variable with pattern when condition -> true | _ -> false" ?
> >>
> >> So that one could easily check optional values for instance:
> >>
> >> if variable = Some x when x > 15 then blabla
> >>
> >> What do you think?
> >>
> >>
> >> --
> >> Caml-list mailing list.  Subscription management and archives:
> >> https://sympa.inria.fr/sympa/arc/caml-list
> >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> >> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
> _______________________________________________
> wg-camlp4 mailing list
> wg-camlp4 at lists.ocaml.org
> http://lists.ocaml.org/listinfo/wg-camlp4
>
>


-- 
-- Regards, Hongbo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ocaml.org/pipermail/wg-camlp4/attachments/20130501/c2eeadea/attachment.html>


More information about the wg-camlp4 mailing list