English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
camlp4: processing lists
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-03-20 (10:28)
From: Hugo Ferreira <hmf@i...>
Subject: camlp4: processing lists

I have sent this post to the new ocaml-developer list but am resending 
it here in the hopes that someone can help me. I have come across a 
weirdness in camlp4. Maybe someone can explain this puzzle to me.

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
            do {
                   List.iter (fun i -> print_endline i) $nl$

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

I compile the source below with the camlp4 extension above:


    let t1 =
      List.iter (fun i -> print_endline i) ["1"; "f(X,Y)"; "g(X,Y)"; "4"]

And the execution gives me:


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"]

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" }]

which ... to say the least is *not* what I expected. I initially I
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?

Hugo Ferreira.