<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2005/05/a7326443f6c82f42c54ec9f3bf6d1258"
  from="Martin Jambon &lt;martin_jambon@e...&gt;"
  author="Martin Jambon"
  date="2005-05-13T22:10:54"
  subject="Re: [Caml-list] Very strange camlp4 error when using records"
  prev="2005/05/0c6354a37e41522a51d1e4e6e2881dcd"
  next="2005/05/cca8c59a7c3690d265534bc52f2cbbf7"
  prev-in-thread="2005/05/4c6f9679e2048c3a128f2fd42dedaafb"
  prev-thread="2005/05/dd9819d3a2a504a8a319330b6d40b766"
  next-thread="2005/05/e70f7a4442b38c866f2eca47cddb462d"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="Very strange camlp4 error when using records">
<msg 
  url="2005/05/4c6f9679e2048c3a128f2fd42dedaafb"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2005-05-13T19:51:41"
  subject="Very strange camlp4 error when using records">
<msg 
  url="2005/05/a7326443f6c82f42c54ec9f3bf6d1258"
  from="Martin Jambon &lt;martin_jambon@e...&gt;"
  author="Martin Jambon"
  date="2005-05-13T22:10:54"
  subject="Re: [Caml-list] Very strange camlp4 error when using records">
</msg>
</msg>
</thread>

<contents>
On Fri, 13 May 2005, Richard Jones wrote:

[...]

&gt; $ ocamlc -pp "camlp4o pa_extend.cmo q_MLast.cmo" -I +camlp4 -c pa_test2.ml
&gt; $ ocamlc -pp "camlp4o ./pa_test2.cmo" -I +camlp4 -c test2.ml
&gt; File "test2.ml", line 1, characters 11-18:
&gt; Unbound value t.a
&gt; $ camlp4o ./pa_test2.cmo pr_o.cmo test2.ml
&gt; type t = { a : int }
&gt; let rec t t = Array.iter print_endline [| string_of_int t.a |]
&gt;       (* generated code above compiles just fine in toplevel or ocamlc *)

You have to be careful that the strings that you introduce in the AST as
"lid" (lowercase identifier), "uid" (uppercase identifier), "str"
(escaped string) or "chr" (escaped char) are *not checked*.

What you do indirectly in your AST generator ("generate") is:

&lt;:expr&lt; string_of_int $lid: "t.a" &gt;&gt;
                             ^^^
                             that's not a lid
                             but it is pretty-printed as if it was one
Instead of:

&lt;:expr&lt; string_of_int $lid:"t"$ . $lid:"a"$ &gt;&gt;

(this is also OK: &lt;:expr&lt; string_of_int t.a &gt;&gt; )

This why you get an error message that you would never get in hand-written
OCaml (it would be "Unbound value t" or "Unbound record field label a").


Martin


&gt; -- pa_test2.ml -----------------------------------------------
&gt; let rec generate loc name = function
&gt;   | &lt;:ctyp&lt; int &gt;&gt; -&gt;
&gt;       &lt;:expr&lt; string_of_int $lid:name$ &gt;&gt;
&gt;   | &lt;:ctyp&lt; { $list:fields$ } &gt;&gt; -&gt;
&gt;       let fields = List.map (
&gt; 	fun (loc, fname, mut, tdef) -&gt;
&gt; 	  let name = name ^ "." ^ fname in
&gt; 	  generate loc name tdef
&gt;       ) fields in
&gt;       &lt;:expr&lt; Array.iter print_endline [| $list:fields$ |] &gt;&gt;
&gt;   | _ -&gt; assert false
&gt;
&gt; DELETE_RULE
&gt;   Pcaml.str_item: "type"; LIST1 Pcaml.type_declaration SEP "and"
&gt; END
&gt;
&gt; EXTEND
&gt;   GLOBAL: Pcaml.str_item;
&gt;
&gt;   Pcaml.str_item:
&gt;     [[ "type"; tds = LIST1 Pcaml.type_declaration SEP "and" -&gt;
&gt; 	 let typedefs = &lt;:str_item&lt; type $list:tds$ &gt;&gt; in
&gt; 	 let values = List.map (
&gt; 	   fun ((loc, name), tparams, tdef, cl) -&gt;
&gt; 	     let body = generate loc name tdef in
&gt; 	     &lt;:patt&lt; $lid:name$ &gt;&gt;, &lt;:expr&lt; fun $lid:name$ -&gt; $body$ &gt;&gt;
&gt; 	 ) tds in
&gt; 	 &lt;:str_item&lt;
&gt; 	   declare
&gt; 	     $typedefs$;
&gt; 	     value rec $list:values$;
&gt;            end
&gt;          &gt;&gt; ]];
&gt; END
&gt; --------------------------------------------------------------
&gt;
&gt; -- test2.ml --------------------------------------------------
&gt; type t = { a : int }
&gt; --------------------------------------------------------------
&gt;
&gt; Ideas?
&gt;
&gt; Rich.
&gt;
&gt; --
&gt; Richard Jones, CTO Merjis Ltd.
&gt; Merjis - web marketing and technology - http://merjis.com
&gt; Team Notepad - intranets and extranets for business - http://team-notepad.com
&gt;
&gt; _______________________________________________
&gt; Caml-list mailing list. Subscription management:
&gt; http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
&gt; Archives: http://caml.inria.fr
&gt; Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
&gt; Bug reports: http://caml.inria.fr/bin/caml-bugs
&gt;

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

</contents>

</message>

