On Mon, 19 Mar 2007, Hugo Ferreira wrote:

>
> Hello,
>
> I have come across a weirdness in camlp4. Maybe someone can help with
> this puzzle.
>
> I have the following extension:
>
> let expand_term_list loc l =
>           let nl = <:expr< [] >>                                      in
>           let nl = List.fold_right (fun h t ->
>                   <:expr< [$h$::$t$] >>
>                   ) l nl                              in
>           <:expr<
>           do {
>                  List.iter (fun i -> print_endline i) $nl$
>            }
>           >>
>
> EXTEND
>   Pcaml.expr: [
>     [
>       "|" ; l = LIST0 Pcaml.expr  SEP ";" ; "|"->
>                  expand_term_list loc l ]  ];
> END;;


Didn't you forget the brackets around the list?
Like this:

EXTEND
   Pcaml.expr: [
     [
       "|" ; "["; l = LIST0 Pcaml.expr SEP ";" ; "]"; "|"->
                  expand_term_list loc l ]  ];
END;;



> I compile the source below with the extension:
>
> ....
>
>   let t1 =
>     List.iter (fun i -> print_endline i) ["1"; "f(X,Y)"; "g(X,Y)"; "4"]
>   in
>   ()
>
> And the execution gives me:
>
> 4
>
> If I generate the original and revised syntax output using the extension
> above using for example:
>
> camlp4o -I . pr_o.cmo pa_$(NAME).cmo $(PROG).ml -o $(PROG).ppo
> camlp4o -I . pr_r.cmo pa_$(NAME).cmo $(PROG).ml -o $(PROG).ppr
>
> I get for $(PROG).ppo
>
> ...
>   let t1 =
>     List.iter (fun i -> print_endline i) ["1"; "f(X,Y)"; "g(X,Y)"; "4"]
>   in
>   ()
>
> which is what I expect for the original syntax... but for the revised
> syntax $(PROG).ppr I get:
>
>   let t1 =
>     do {
>       print_string "Not implemented for list ";
>       List.iter (fun i -> print_endline i)
>         [do { "1"; "f(X,Y)"; "g(X,Y)"; "4" }]
>     }
>   in
>   ()
>
> which ... to say the least is *not* what I expected. This explains why I
> initially though I had but one Pcaml.expr and not a list of those.  What
> seems to be happening is that the revised syntax is used to generate the
> code and this is wrong.
>
> Can anyone tell me what mess I have done?
>
> TIA,
> Hugo Ferreira.
>
>
>
>
> Hugo Ferreira wrote:
>> Hello,
>>
>> I would like to convert an Ocaml expression:
>>
>> let t1 = | ["f(X,Y)"; "g(X,Y)"] | in ()
>>
>> to:
>>
>>    let t1 =
>>      List.iter (fun i -> print_endline i) ["f(X,Y)"; "g(X,Y)"]
>>    in
>>    ()
>>
>> To do this I use:
>>
>> let expand_term_list loc l =
>>            let nl = <:expr< [] >>                      in
>>            let nl = List.fold_right (fun h t ->
>>                    <:expr< [$h$::$t$] >>
>>                    ) l nl                              in
>>            <:expr<
>>            do {
>>                  List.iter (fun i -> print_endline i) $nl$
>>             }
>>           >>
>>
>> EXTEND
>>    Pcaml.expr: [
>>      [
>>      "|"; l = LIST0 Pcaml.expr  SEP ";" ; "|" -> expand_term_list loc l ]
>>    ];
>> END;;
>>
>>
>> Unfortunately the output I get is:
>>
>>    let t1 =
>>      List.iter (fun i -> print_endline i) [["f(X,Y)"; "g(X,Y)"]]
>>    in
>>    ()
>>
>> As I understand it, we have to do list construction ourselves seeing as
>> we have no quotations for this. But it seems that the parameter "l" I am
>> passing to the function "expand_term_list" is an AST element and not a
>> list of AST nodes. How do I actual get the list elements?
>>
>> TIA.
>> Hugo F.
>>
>>
>>
>>
>>
>>>
>>
>
>
> >

--
Martin Jambon
http://martin.jambon.free.fr

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"ocaml-developer" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/ocaml-developer?hl=en
For other OCaml forums, see http://caml.inria.fr/resources/forums.en.html
-~----------~----~----~----~------~----~------~--~---

Reply via email to