From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p2N8vtng008655 for ; Wed, 23 Mar 2011 09:57:55 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlEIAPZSiU2NTDBjgWdsb2JhbACYSox4FAEBFiYlw1CFaQSMb4Z7 X-IronPort-AV: E=Sophos;i="4.63,230,1299452400"; d="scan'208";a="78894172" Received: from os.inf.tu-dresden.de ([141.76.48.99]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 23 Mar 2011 09:57:50 +0100 Received: from [2002:8d4c:3001:48:222:68ff:fe19:71d] (helo=blau.inf.tu-dresden.de) by os.inf.tu-dresden.de with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.74) id 1Q2Jsy-0007Xk-UO for caml-list@yquem.inria.fr; Wed, 23 Mar 2011 09:57:49 +0100 Received: from tews by blau.inf.tu-dresden.de with local (Exim 4.72) (envelope-from ) id 1Q2Jsy-0003TA-Bv for caml-list@yquem.inria.fr; Wed, 23 Mar 2011 09:57:48 +0100 From: Hendrik Tews To: caml-list@yquem.inria.fr References: Date: Wed, 23 Mar 2011 09:57:47 +0100 In-Reply-To: (eagriffis@gmail.com's message of "Mon, 14 Mar 2011 20:32:15 +0100") Message-ID: <6x7hbqxlh0.fsf@blau.inf.tu-dresden.de> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: Re: [Caml-list] Camlp4 type constructor transformation eagriffis@gmail.com writes: I am having trouble getting Camlp4 to detect and transform type constructors that take parameters. I believe the problem is with my grammar. Here is a base case (call it base.ml): If you want to transform the syntax tree after parsing you should not modify the grammar. Instead you should install a filter function. Transforming the syntax tree is the very purpose of filter functions. If you look at the sources of the camlp4 parser, you see expr: LEVEL "apply" (* LEFTA *) [ [ e1 = SELF; e2 = SELF -> match (is_expr_constr_call e1, e2) with [ (True, <:expr< ( $tup:e$ ) >>) -> List.fold_left (fun e1 e2 -> <:expr< $e1$ $e2$ >>) e1 (Ast.list_of_expr e []) | _ -> <:expr< $e1$ $e2$ >> ] ] ]; i.e, your expression "Foo(1,1)" is first parsed as Foo applied to a pair and only if is_expr_constr_call detects a constructor it is transformed into Foo applied to 1 applied to 1. Your rule expr: LEVEL "apply" [ LEFTA [ e1 = SELF; e2 = SELF -> apply _loc e1 e2 ] ]; does not curry constructor calls. I don't understand the code in Camlp4/Struct/Camlp4Ast2OCamlAst.ml, but I would expect that you get strange errors if you connect this parser to the ocaml compiler. It seems that my constructor of arity 2 can be located as an uppercase identifier nested twice deep in ExApps. So either I do not understand how to modify the grammar rule to catch this, or the problem is not in the rule? This is true, but only after the transformation in the original parser. Filter functions run after the parsing completed, so a filter function sees precisely what you expect. Bye, Hendrik