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 FAA30339; Sat, 10 Jan 2004 05:42:16 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id FAA29127 for ; Sat, 10 Jan 2004 05:42:15 +0100 (MET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id i0A4gD528960 for ; Sat, 10 Jan 2004 05:42:14 +0100 (MET) Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3p2-20030924/3.7W) with ESMTP id NAA20046; Sat, 10 Jan 2004 13:42:09 +0900 (JST) To: williamc@paneris.org Cc: caml-list@inria.fr Subject: Re: [Caml-list] Recursive class types (long-standing question ...) In-Reply-To: <20031230210229.ABA5C96BAB@beertje.william.bogus> References: <20031230210229.ABA5C96BAB@beertje.william.bogus> 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: <20040110134333C.garrigue@kurims.kyoto-u.ac.jp> Date: Sat, 10 Jan 2004 13:43:33 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 jacques:01 chesters:01 williamc:01 paneris:01 faq:01 recursion:01 recursion:01 struct:01 workarounds:01 jacques:01 compiler:01 bool:01 bool:01 ocaml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: William Chesters > Here's something I've wondered about for a while and just encountered again. > How come this class .. > > class virtual ['a] c = object > method virtual b: bool c > end > > ... gets a constraint 'a = bool? I.e. for instance this doesn't compile: > > class virtual ['a] c = object > method virtual b: bool c > method virtual i: int c > (* "This type int should be an instance of type bool" *) > end This is almost a FAQ. The deep reason is that ocaml object types are defined by structure, and as a result only regular recursion is allowed. If we were to allow polymorphic recursion in class types as required by your example, the type could not be represented as a regular tree anymore. (to be fully exact, in this particular case it is possible to represent it as a regular tree, but this would break the stronger invariant used by the compiler, which requires all occurences of a type in its own expansion to have the same parameters) Note that this applies only to structural types. For defined types you can use polymorphic recursion. type 'a t = { b: bool t } > Note that the following does work in C++: > > template > struct c { > virtual c b() = 0; > virtual c i() = 0; > }; > > I guess there are workarounds but it seems unnecessary that ML should find > recursive parameterised classes harder than C++? In C++ classes are not equal by structure, so the second case applies. 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