Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] About polymorphic methods and recursive classes
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] About polymorphic methods and recursive classes
From: Sebastien Briais <sebastien.briais@epfl.ch>

> So, if I understand you well, it is not possible to write something like 
> that in ocaml:
> 
> class virtual ['a,'b] list_visitor =
> object
>   method virtual caseNil : 'a
>   method virtual caseCons : 'b -> 'b list -> 'a
> end
> and virtual ['b] list =
> object
>   method virtual visit : 'a.(('a,'b) list_visitor -> 'a)
> end
> 
> But what is the reason of this limitation ? 
> I do not see the reasons since for example, in Generic Java, such a 
> definition is correct.

My previous answer was a bit vague, but actually the problem is more
intrinsic to ocaml objects.
Contrary to Java, Ocaml object types are defined structurally, rather
than by name. For this reason, recursion has to be limited to cases
where recursive occurences of a type constructor have always the
same arguments. But in your example, 'b list in list_visitor expands
to < visit : 'a. ('a,'b) list_visitor -> 'a >, where list_visitor has
different arguments from its definition (the 'a here is an universal
variable, different from the normal 'a listed as parameter in the
definition).

As I showed, the only workaround is to untie the mutual recursion by
replacing 'b list by an extra parameter.

This is a bit similar to the reason you cannot define a map method:
class type ['a] list = object
  method map : 'b. ('a -> 'b) -> 'b list
end
but in that case you cannot untie the recursion: 'b in 'b list is
locally bound.

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners