From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 87334BC69 for ; Tue, 17 Jul 2007 13:32:40 +0200 (CEST) Received: from stirner.roentgeninstitut.de (stirner.roentgeninstitut.de [212.110.109.210]) by concorde.inria.fr (8.13.6/8.13.6) with SMTP id l6HBWbNu004277 for ; Tue, 17 Jul 2007 13:32:40 +0200 Received: (qmail 10217 invoked by uid 1000); 17 Jul 2007 04:32:37 -0700 Date: Tue, 17 Jul 2007 04:32:37 -0700 From: Christian Stork To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] camlp4: question about functor-style syntax extensions Message-ID: <20070717113237.GA9674@stirner.roentgeninstitut.de> Mail-Followup-To: caml-list@yquem.inria.fr References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Archive: encrypt User-Agent: Mutt/1.5.13 (2006-08-11) X-Miltered: at concorde with ID 469CA8D5.002 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; camlp:01 syntax:01 0200,:01 functor:01 camlp:01 syntax:01 functor:01 toplevel:01 sig:01 sig:01 struct:01 structurally:01 transforming:01 clashes:01 expr:01 On Tue, Jul 17, 2007 at 11:08:09AM +0200, Bruno De Fraine wrote: ... > As mentioned in the documentation, an extension (such as Pi) must be > functor: Camlp4Syntax -> Camlp4Syntax, and > Register.OCamlSyntaxExtension requires this type. This is why I have > to "include" (instead of "open") the original syntax: to produce a > valid output syntax. My question is: how is this output syntax ever > used? (Note that the EXTEND-statement does not make any structural > changes to the syntax module, just dynamic changes as a side-effect > upon functor application.) > > To put all my cards on the table: I believe the output syntax is > never used. For example, you can sabotage one of the main grammar > entries by finishing the definition of Pi with: > > let top_phrase : Ast.str_item option Gram.Entry.t = Obj.magic 0 > > And the extension keeps working all the same from the toplevel. In > fact, a look at the code of OCamlSyntaxExtension in Register.ml > confirms it is never used: > > module OCamlSyntaxExtension > (Id : Sig.Id) (Maker : functor (Syn : Sig.Camlp4Syntax) -> > Sig.Camlp4Syntax) = > struct > declare_dyn_module Id.name (fun _ -> let module M = Maker Syntax > in ()); > end; > > The output syntax M is thrown away, i.e. the syntax extension relies > entirely on side-effects of the functor application. I think the type > required for a syntax extension could just as well have been a > functor that return an empty module: Camlp4Syntax -> sig end > > Why bother making this remark if you can just include the original > syntax at the beginning and it works? I believe there are two > important reasons. The first is didactical: the signature > Camlp4Syntax -> Camlp4Syntax suggests that the syntax extension works > by structurally transforming one syntax into another, while this is > not what is going on. This situation makes the workings of camlp4 all > the more difficult to understand for novice (and perhaps seasoned) > camlp4 developers. The second reason is practical: you can easily > define something in your extension that clashes with a name from > Camlp4Syntax (e.g. "expr"), and then the compiler will complain if > the types do not agree. You can assure you export the exact original > definitions by putting "include Syntax" at the end of the extension > instead of the beginning, but then you still need an "open Syntax" at > the beginning to have the useful modules (like Ast, Gram, etc.) > available. All of this is an annoying redundant idiom given that the > output Syntax is not used. Bruno, I had exactly the same thoughts when I originally studied the new camlp4 sources and I definitely think that these observations should be part of the camlp4 documentation. Maybe you could update the Camlp4 Wiki with it? :-) I intended to do so already but didn't find the time yet. Anyway, when I asked Nicolas about it he hinted at the possibility that one day the current grammar update via module initialization side effect would be replaced by "true" functor applications. Would be interesting to know how realistic such a possibility is. -- Chris Stork <> Support eff.org! <> http://www.ics.uci.edu/~cstork/ OpenPGP fingerprint: B08B 602C C806 C492 D069 021E 41F3 8C8D 50F9 CA2F