[wg-camlp4] Time for a summary?

Gerd Stolpmann info at gerd-stolpmann.de
Thu Feb 7 13:16:14 GMT 2013


Am 07.02.2013 11:09:29 schrieb(en) Alain Frisch:
>>     4. It's easy to port P4's code base to Fan, it only takes me 2  
>> hours
>> to port Alain's ulex to Fan
> 
> It did not take that much longer to port ulex from Camlp4 to -ppx,  
> and as an extra added bonus, it really gave me the feeling that I  
> could finally *breathe* and understand exactly what I'm writing.  
> Frankly, I'm more comfortable writing:
> 
>   E.let_ Recursive states
>     (E.sequence
>        (appfun "Sedlexing.start" [eid lexbuf])
>        (E.match_ (appfun (state_fun 0) [eid lexbuf])
>           (cases @ [P.any (), error])
>        )
>     )
> 
> than:
> 
>   <:expr< fun lexbuf ->
>     let rec $list:Array.to_list states$ in
>     do { Ulexing.start lexbuf;
>          match __ulex_state_0 lexbuf with
>          [ $list:Array.to_list cases$ | _ -> raise Ulexing.Error ]  
> } >>
> 
> which requires me to learn two new "sub-languages" (the revised  
> syntax and the notion of quotation / antiquotation).

Frankly, this is a quite subjective experience. For me (and many  
developers who are not familiar with parsetrees) there is not much  
difference doing either. Of course, the revised syntax is an extra  
hurdle.

Currently, there is no documentation about parsetrees, and I would need  
to guess how they are built up. But if there was some documentation  
plus some examples it would probably be fully acceptable.

>  Imagine that the "rec" flag above should be set only according to  
> some condition to be checked; I know directly how to write that in  
> regular OCaml but I would need to dig into Camlp4 documentation (or  
> not) to see how to introduce an "anti-quotation on the rec flag".

My guess here is that this is not directly possible, and you need to  
program two independent cases (if rec_flag then <:expr< ...>> else  
<:expr< ...>>). This is the price for an extra abstraction, but you  
also get something back: if, for some reason, the parsetree changes and  
let rec gets a variant of its own, you don't need to change the program.

"let rec" might be a bad example, because such a change is not  
expected, but generally we _will_ run into the problem that changes of  
the parsetree type break ppx processors. (Well, to some degree we can  
avoid this by having wrapper functions, i.e. don't use E.let_ directly,  
but a function mk_let, as then many changes can be done in a way that  
doesn't affect compatibility.)

Gerd
-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd at gerd-stolpmann.de
Creator of GODI and camlcity.org.
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------


More information about the wg-camlp4 mailing list