Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: brogoff <brogoff@speakeasy.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Pure visitor patterns
Date: Sat, 30 Dec 2006 10:27:37 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.58.0612301013490.531@shell4.speakeasy.net> (raw)
In-Reply-To: <20061228.151753.38663586.garrigue@math.nagoya-u.ac.jp>

On Thu, 28 Dec 2006, Jacques Garrigue wrote:
> From: Jason Hickey <jyh@cs.caltech.edu>
> > 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 = <foo: 'foo; bar: 'bar; ..>
>     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


      parent reply	other threads:[~2006-12-30 18:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-27 19:31 Jason Hickey
2006-12-28  6:17 ` [Caml-list] " Jacques Garrigue
     [not found]   ` <d86ee07e0612272325g4209dfb5s8276f8b5e08ffd63@mail.gmail.com>
2006-12-28  8:06     ` Jason Hickey
2006-12-30 18:27   ` brogoff [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.58.0612301013490.531@shell4.speakeasy.net \
    --to=brogoff@speakeasy.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox