From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 45698BC88 for ; Wed, 9 Feb 2005 02:54:24 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j191sNqK003474 for ; Wed, 9 Feb 2005 02:54:23 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA16253 for ; Wed, 9 Feb 2005 02:54:22 +0100 (MET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j191sKg7012879 for ; Wed, 9 Feb 2005 02:54:22 +0100 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j191sGkJ014602; Wed, 9 Feb 2005 10:54:17 +0900 (JST) Date: Wed, 09 Feb 2005 10:53:50 +0900 (JST) Message-Id: <20050209.105350.108297361.garrigue@math.nagoya-u.ac.jp> To: cstork@ics.uci.edu Cc: caml-list@inria.fr Subject: Re: [Caml-list] Unquantifiable escaping type in variation of visitor pattern From: Jacques Garrigue In-Reply-To: <20050208225542.GA20967@anthony.ics.uci.edu> References: <20050208225542.GA20967@anthony.ics.uci.edu> X-Mailer: Mew version 4.1.53 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 42096D4F.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 42096D4C.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 escaping:01 grammar:01 ocaml:01 recursive:01 recursion:01 impl:01 impl:01 ...:98 twist:98 ...:98 baton:98 node:01 node:01 complains:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: From: Christian Stork > I'm tying to implement a framework for visitors over trees (e.g. ASTs). > These trees are built according to some rules. Rules are much like the > productions of a grammar. In general I would advice against using an object-oriented representation for ASTs. Variant types are the proven approach to do that in functional languages, and generally work much better. > The following minimal example demonstrates my current design problem: [...] > The above code has one special twist, it allows the visitor's visit... > methods to hand eachother some argument, called the baton. Different > visitors might want to use different types of batons. The above code > tries to support this by parametrizing the accept method and the > visitor class. Sadly, Ocaml complains about then with This specific typing problem can be solved. It stems from the fact you are defining someRule and ['baton] visitor simultaneously, but use 'baton polymorphically in someRule. Inside mutual recursive definitions of classes, parameters cannot be used polymorphically. The way to a void this problem is to break the recursion. The following code does what you want. type 'rule node = { rule:'rule; kids:'rule node list } class type ['baton,'rule] visitor = object method visitSomeRuleNode : 'rule node -> 'baton -> 'baton end class someRule = object (self) method accept : 'baton . someRule node -> ('baton,someRule) visitor -> 'baton -> 'baton = fun n v b -> v#visitSomeRuleNode n b end class ['baton] visitor_impl = object (self) method visitSomeRuleNode n (b : 'baton) = List.fold_left (fun b' k -> (k.rule : someRule)#accept k (self :> _ visitor_impl) b') b n.kids end Jacques Garrigue