Version française
Home     About     Download     Resources     Contact us    
Browse thread
Pure visitor patterns
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jason Hickey <jyh@c...>
Subject: Pure visitor patterns
I've been trying to write pure visitors (visitors that compute without
side-effects).  The main change is that a visitor returns a value.
Here is a (failed) example specification based on having only one kind
of thing "foo".

     class type ['a] visitor =
       object ('self)
         method visit_foo : foo -> 'a
       end

     and foo =
       object ('self)
         method accept : 'a. 'a visitor -> 'a
         method examine : int
       end

This fails because the variable 'a escapes its scope in the method  
accept.
It can be fixed by breaking apart the mutual type definition.

     class type ['a, 'foo] visitor =
       object ('self)
         method visit_foo : 'foo -> 'a
       end

     class type foo =
       object ('self)
         method accept : 'a. ('a, foo) visitor -> 'a
         method examine : int
       end

The second form works, but it is hard to use because of the number
of type parameters needed for the visitor (in general).

Here are my questions:

    - Why does 'a escape its scope in the recursive definition?
    - Is there some other style that would solve this problem?

Thanks!

Jason

P.S. Here is an alternate scheme with non-polymorphic visitors, where
the returned value is just a visitor.  The accept method needs to
preserve the type, so this one also has the "escapes its scope"
problem.

     class type visitor =
       object ('self)
         method visit_foo : foo -> 'self
       end

     and foo =
       object ('self)
         method accept : 'a. (#visitor as 'a) -> 'a
       end
     ...

--
Jason Hickey                  http://www.cs.caltech.edu/~jyh
Caltech Computer Science      Tel: 626-395-6568 FAX: 626-792-4257