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: Re: [Caml-list] Pure visitor patterns
> On 12/27/06, Jacques Garrigue <garrigue@math.nagoya-u.ac.jp> wrote:
>> From: Jason Hickey <jyh@cs.caltech.edu>
>>
>> > Here are my questions:
>> >
>> >     - Why does 'a escape its scope in the recursive definition?
>>
>> Because during recursive definitions parameters of these definitions
>> are handled as monomorphic. So you cannot generalize the 'a locally.

Ah, that makes perfect sense.  If I understand correctly, the  
quantifiers in a mutual recursive class definition are hoisted, like  
this:

The definition
    class type ['a] c1 = ... and ['b] c2 = ...
is really more like the following (pardon my notation):
    ['a, 'b] (class type c1 = ... and c2 = ...)

The mistake is to think of it like simple recursive type definitions,  
like the following (rather useless) definition.

     type 'a visitor =
        { visit_foo : 'a -> foo -> 'a;
          visit_bar : 'a -> bar -> 'a
        }
     and foo = { accept : 'a. 'a -> 'a visitor -> 'a; examine : int }
     and bar = { accept : 'a. 'a -> 'a visitor -> 'a; examine : bool };;

I'm not complaining--the fact that you can write any of these types  
is very cool.

>> Another trick to make this pattern more scalable is to use  
>> constraints
>> for parameters.

Very good suggestion.  This makes it _much_ easier to deal with the  
multiplicity of types, since the constraints are linear, not  
quadratic, in the number of cases.

Many thanks for your explanation!

Jason

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