<?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/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"
  prev="2005/05/dd9819d3a2a504a8a319330b6d40b766"
  next="2005/05/e70f7a4442b38c866f2eca47cddb462d"
  next-in-thread="2005/05/a7326443f6c82f42c54ec9f3bf6d1258"
  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>
I have a strange case where the generated code looks completely
correct - indeed, I can pretty-print the code, save it to a file, and
compile it using ocamlc just fine.  However when using the camlp4
preprocessor directly (ocamlc -pp "camlp4o ./pa_test2.cmo" ...), it
fails to compile.

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

-- pa_test2.ml -----------------------------------------------
let rec generate loc name = function
  | &lt;:ctyp&lt; int &gt;&gt; -&gt;
      &lt;:expr&lt; string_of_int $lid:name$ &gt;&gt;
  | &lt;:ctyp&lt; { $list:fields$ } &gt;&gt; -&gt;
      let fields = List.map (
	fun (loc, fname, mut, tdef) -&gt;
	  let name = name ^ "." ^ fname in
	  generate loc name tdef
      ) fields in
      &lt;:expr&lt; Array.iter print_endline [| $list:fields$ |] &gt;&gt;
  | _ -&gt; assert false

DELETE_RULE
  Pcaml.str_item: "type"; LIST1 Pcaml.type_declaration SEP "and"
END

EXTEND
  GLOBAL: Pcaml.str_item;

  Pcaml.str_item:
    [[ "type"; tds = LIST1 Pcaml.type_declaration SEP "and" -&gt;
	 let typedefs = &lt;:str_item&lt; type $list:tds$ &gt;&gt; in
	 let values = List.map (
	   fun ((loc, name), tparams, tdef, cl) -&gt;
	     let body = generate loc name tdef in
	     &lt;:patt&lt; $lid:name$ &gt;&gt;, &lt;:expr&lt; fun $lid:name$ -&gt; $body$ &gt;&gt;
	 ) tds in
	 &lt;:str_item&lt;
	   declare
	     $typedefs$;
	     value rec $list:values$;
           end
         &gt;&gt; ]];
END
--------------------------------------------------------------

-- test2.ml --------------------------------------------------
type t = { a : int }
--------------------------------------------------------------

Ideas?

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com

</contents>

</message>

