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