[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Nicolas Pouillard <nicolas.pouillard@g...> |
| Subject: | Re: [Caml-list] camlp4 3.10: Hashtbl.t and <:ctyp< $tp1$ . $tp2$ >> |
On 4/30/07, Joel Reymont <joelr1@gmail.com> wrote:
> Consider the following function. My understanding is that for, say,
> Hashtbl.t it should return a list of types where the 1st element
> represents "t" and the second represents "Hashtbl".
>
> (* [tp_path tp] takes a type. @return a module path (list of strings)
> denoting this type. *)
> let rec tp_path = function
> | <:ctyp< $lid:id$ >> | <:ctyp< $uid:id$ >> -> [id]
> | <:ctyp< $tp1$ . $tp2$ >> ->
> (match tp_path tp2 with [n] -> n | _ -> assert false) ::
> tp_path tp1
> | _ -> invalid_arg "tp_path"
>
> Nicolas suggested the following approach which doesn't work because
> tp1 and tp2 become idents. There's a function to convert from ctyp to
> ident but I could not find the opposite from searching the camlp4
> source tree.
>
> let rec tp_path = function
> | <:ctyp< $lid:id$ >> | <:ctyp< $uid:id$ >> -> [id]
> | <:ctyp< $id:id$ >> ->
> (match id with
> | <:ident< $tp1$ . $tp2$ >> ->
> (match tp_path tp2 with [n] -> n | _ -> assert false) ::
> tp_path tp1
> | _ -> invalid_arg "tp_path")
> | _ -> invalid_arg "tp_path"
>
> Since Hashtbl.t is a valid type, there should be a camlp4 ctyp
> pattern to match it, no?
>
> Am I missing something obvious?
To see how is represented something:
$ camlp4of -str '<:ctyp< Hashtbl.t >>'
Ast.TyId (_loc,
Ast.IdAcc (_loc, Ast.IdUid (_loc, "Hashtbl"), Ast.IdLid (_loc, "t")))
So the function tp_path doesn't need to recurse on types since there
is only one node which is TyId(_,_) -> <:ctyp< $id:_$ >>
let tp_path = function
| <:ctyp< $id:path$ >> ->
let rec loop =
function
| <:ident< $i1$.$i2$ >> -> ...
| ...
in loop path
| _ -> fail....
--
Nicolas Pouillard