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 KAA17652; Thu, 13 Mar 2003 10:27:11 +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 KAA18085 for ; Thu, 13 Mar 2003 10:27:10 +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 h2D9R8X28287 for ; Thu, 13 Mar 2003 10:27:09 +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 SAA28710; Thu, 13 Mar 2003 18:27:02 +0900 (JST) To: Damien.Pous@ens-lyon.fr Cc: caml-list@pauillac.inria.fr Subject: Re: [Caml-list] polymorphic methods In-Reply-To: <20030313100402.02e1ce82.Damien.Pous@ens-lyon.fr> References: <20030313000716.3e6bb57c.Damien.Pous@ens-lyon.fr> <20030313105656R.garrigue@kurims.kyoto-u.ac.jp> <20030313100402.02e1ce82.Damien.Pous@ens-lyon.fr> 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: <20030313182702L.garrigue@kurims.kyoto-u.ac.jp> Date: Thu, 13 Mar 2003 18:27:02 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) X-Spam: no; 0.00; jacques:01 caml-list:01 damien:01 ens-lyon:01 quantified:01 coercions:01 lablgtk:01 avoids:01 recursion:01 monomorphic:01 incompatible:01 dummy:01 inherit:01 garrigue:01 subtype:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Damien > > > Does someone know why the following class type > > > is not accepted ? > > > > > > # class type a = object method m: 'a. (#a as 'a) -> unit end;; > > > The abbreviation #a expands to type #a but is used with type < .. > > > [...] > > Now, the serious part: can such a type be defined? > > After working a bit on this riddle, I'm afraid the answer is no. > > The reason is that there is no way to recurse on a quantified > > polymorphic method rather than on the whole object. > > The closest I found is: > > class type a = > > object method m : < d : 'b. (< m : 'a; .. > as 'b) -> unit > as 'a > > end > > The dummy wrapper is just there to allow one to recurse > > on the polymoprhic method itself. You can call the method m by doing > > [a#m#d a'] in place of [a#m a']. > I worked with this class type, but it is a bit tedious :-( > > My goal is to define a tree, whose node's type is a class > type a_t, containing a method for adding children. > but the child type can be any subtype of a_t > > * this method can safely be typed [a_t->unit], but this will require a > lot of coercions in the rest of the code... > > * that's why I wanted to type it ['a. (#a_t as 'a)->unit] (i.e. the > coercion is written only once, in the method) > > using your trick, I obtained the attached code. It works fine, > but that's not really a beautiful piece of code ! And I would not have expected it to be! This was just a theoretical answer. If you are bothered by coercions, you may have a look at the trick in lablgtk, which avoids polymorphic methods: give the parent as argument. let new_node ?parent () = let n = new node in begin match parent with None -> () | Some (p : #a_t) -> p#add (n :> a_t) end; n You can also do the trick in an initializer. Alternatively, you might just have an interface of a_t without the method m. This way you have no problem with recursion: class type a0_t = object ... everything but add ... end class type a_t = object inherit a0_t method add : #a0_t -> unit end > I don't know what do you mean when saying > "such a type cannot be defined", in fact, I could define this > equivalent (I think it is...) : > > >#class type a_t = object > > method m: 'a.( unit; ..> as 'a) -> unit > >end This is not equivalent: the m in a_t is a polymorphic method, but not the one in the recursion. Polymorphic and monomorphic methods are incompatible, so you cannot pass an a_t to m. 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