This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Pure visitor patterns
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2006-12-28 (08:07) From: Jason Hickey 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.