caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin_jambon@emailuser.net>
To: Richard Jones <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Very strange camlp4 error when using records
Date: Fri, 13 May 2005 15:10:49 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.44.0505131453460.3445-100000@localhost> (raw)
In-Reply-To: <20050513195425.GA5994@furbychan.cocan.org>

On Fri, 13 May 2005, Richard Jones wrote:

[...]

> $ 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 *)

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:

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

<:expr< string_of_int $lid:"t"$ . $lid:"a"$ >>

(this is also OK: <:expr< string_of_int t.a >> )

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


> -- pa_test2.ml -----------------------------------------------
> let rec generate loc name = function
>   | <:ctyp< int >> ->
>       <:expr< string_of_int $lid:name$ >>
>   | <:ctyp< { $list:fields$ } >> ->
>       let fields = List.map (
> 	fun (loc, fname, mut, tdef) ->
> 	  let name = name ^ "." ^ fname in
> 	  generate loc name tdef
>       ) fields in
>       <:expr< Array.iter print_endline [| $list:fields$ |] >>
>   | _ -> 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" ->
> 	 let typedefs = <:str_item< type $list:tds$ >> in
> 	 let values = List.map (
> 	   fun ((loc, name), tparams, tdef, cl) ->
> 	     let body = generate loc name tdef in
> 	     <:patt< $lid:name$ >>, <:expr< fun $lid:name$ -> $body$ >>
> 	 ) tds in
> 	 <:str_item<
> 	   declare
> 	     $typedefs$;
> 	     value rec $list:values$;
>            end
>          >> ]];
> 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
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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


      reply	other threads:[~2005-05-13 22:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-13 19:54 Richard Jones
2005-05-13 22:10 ` Martin Jambon [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.44.0505131453460.3445-100000@localhost \
    --to=martin_jambon@emailuser.net \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).