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=1.1 required=5.0 tests=AWL,SPF_SOFTFAIL autolearn=disabled version=3.1.3 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id A7F29BC0B for ; Sat, 30 Dec 2006 19:27:40 +0100 (CET) Received: from mail4.sea5.speakeasy.net (mail4.sea5.speakeasy.net [69.17.117.6]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id kBUIRcaA023886 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Sat, 30 Dec 2006 19:27:40 +0100 Received: (qmail 27368 invoked from network); 30 Dec 2006 18:27:37 -0000 Received: from shell4.sea5.speakeasy.net ([69.17.116.5]) (envelope-sender ) by mail4.sea5.speakeasy.net (qmail-ldap-1.03) with AES256-SHA encrypted SMTP for ; 30 Dec 2006 18:27:37 -0000 Date: Sat, 30 Dec 2006 10:27:37 -0800 (PST) From: brogoff To: caml-list@inria.fr Subject: Re: [Caml-list] Pure visitor patterns In-Reply-To: <20061228.151753.38663586.garrigue@math.nagoya-u.ac.jp> Message-ID: References: <20061228.151753.38663586.garrigue@math.nagoya-u.ac.jp> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at concorde with ID 4596AF9A.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 foo:01 foo:01 recursive:01 recursive:01 ocaml:01 scalable:01 recursions:01 non-obvious:01 'self:01 'foo:01 'foo:01 'self:01 bool:01 wrote:01 On Thu, 28 Dec 2006, Jacques Garrigue wrote: > From: Jason Hickey > > I've been trying to write pure visitors (visitors that compute without > > side-effects). I'm curious, what's the application? I asked a similar question to yours a while ago and Jacques (I believe?) suggested that I would be better off using an imperative approach in OCaml so all my visitors would have foo -> unit types. I was disappointed at the time but I think it was a very good suggestion. My visitors are rather complicated and I found it useful to have open_foo/close_foo (before_visit/after_visit) methods with different types than the visits. I decided that using side effects is better than getting too complex with types. > > - Is there some other style that would solve this problem? > > Not really. Using private rows and recursive allow for some more > expressiveness (in particular you can then define pure visitors on > extensible on an extensible collection of classes), but they are a bit > tricky to use in this context, so I'm not sure this is an improvement > for simple cases. I guess you mean recursive modules above. My usual issue with rows is that they force you to write a lot of stuff out by hand when you wish there was a way to assemble them from pieces, if you get my meaning. A petty complaint, to be sure, but there you have it. BTW, I assume that the virtual instance variables in the next OCaml are for extensible visitors, right? > Another trick to make this pattern more scalable is to use constraints > for parameters. That's a nice trick! I knew every little piece of it from reading the docs and knowing how to break some recursions, but I never put it all together. Thanks. It would be great if you could flesh out a few of these non-obvious tricks and put them in the OCaml manual. > class type ['a, 'cases] visitor = > object ('self) > constraint 'cases = > method visit_foo : 'foo -> 'a > method visit_bar : 'bar -> 'a > end > class type foo = > object ('self) > method accept : 'a. ('a, cases) visitor -> 'a > method examine : int > end > and bar = > object ('self) > method accept : 'a. ('a, cases) visitor -> 'a > method examine : bool > end > and cases = object method foo : foo method bar : bar end -- Brian