From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id IAA32169; Wed, 26 Mar 2003 08:43:21 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id IAA32241 for ; Wed, 26 Mar 2003 08:43:20 +0100 (MET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h2Q7hHX02939 for ; Wed, 26 Mar 2003 08:43:18 +0100 (MET) Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3/3.7W) with ESMTP id QAA06557; Wed, 26 Mar 2003 16:43:09 +0900 (JST) To: sebastien.briais@epfl.ch Cc: caml-list@inria.fr Subject: Re: [Caml-list] About polymorphic methods and recursive classes In-Reply-To: <3E80547E.2010201@epfl.ch> References: <3E7F457A.80006@epfl.ch> <20030325101545S.garrigue@kurims.kyoto-u.ac.jp> <3E80547E.2010201@epfl.ch> X-Mailer: Mew version 1.94.2 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20030326164309I.garrigue@kurims.kyoto-u.ac.jp> Date: Wed, 26 Mar 2003 16:43:09 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) X-Spam: no; 0.00; jacques:01 caml-list:01 generic:01 intrinsic:01 recursion:01 ocaml:01 workaround:01 garrigue:01 polymorphic:01 vague:02 objects:02 classes:03 recursive:03 constructor:03 variable:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Sebastien Briais > 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