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 KAA16759; Thu, 13 Mar 2003 10:06:31 +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 KAA16383 for ; Thu, 13 Mar 2003 10:06:29 +0100 (MET) Received: from avalon.ens-lsh.fr (mailhost.ens-lsh.fr [193.51.131.2]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h2D96TX27343 for ; Thu, 13 Mar 2003 10:06:29 +0100 (MET) Received: from avalon.ens-lsh.fr (localhost [127.0.0.1]) by avalon.ens-lsh.fr (8.12.8/8.12.8) with ESMTP id h2DA4Xfm026039 for ; Thu, 13 Mar 2003 10:04:33 GMT Received: from ens-notes1.ens-lsh.fr (ens-notes1.ens-lsh.fr [10.2.1.44]) by avalon.ens-lsh.fr (8.12.8/8.12.8) with ESMTP id h2DA4Xto026036 for ; Thu, 13 Mar 2003 10:04:33 GMT Received: from ens-lyon.fr ([10.3.25.133]) by ens-notes1.ens-lsh.fr (Lotus Domino Release 5.0.10) with SMTP id 2003031310052325:8723 ; Thu, 13 Mar 2003 10:05:23 +0100 Date: Thu, 13 Mar 2003 10:04:02 +0100 From: Damien Cc: caml-list@pauillac.inria.fr Subject: Re: [Caml-list] polymorphic methods Message-Id: <20030313100402.02e1ce82.Damien.Pous@ens-lyon.fr> In-Reply-To: <20030313105656R.garrigue@kurims.kyoto-u.ac.jp> References: <20030313000716.3e6bb57c.Damien.Pous@ens-lyon.fr> <20030313105656R.garrigue@kurims.kyoto-u.ac.jp> Organization: mosthagloups X-Mailer: Sylpheed version 0.8.10claws13 (GTK+ 1.2.10; i386-debian-linux-gnu) Mime-Version: 1.0 X-MIMETrack: Itemize by SMTP Server on ens-notes1/ENS-LSH(Release 5.0.10 |March 22, 2002) at 13/03/2003 10:05:23, Serialize by Router on ens-notes1/ENS-LSH(Release 5.0.10 |March 22, 2002) at 13/03/2003 10:05:23 Content-Type: multipart/mixed; boundary="Multipart_Thu__13_Mar_2003_10:04:02_+0100_083b35a0" X-Spam: no; 0.00; damien:01 ens-lyon:01 caml-list:01 jacques:01 quantified:01 coercions:01 val:01 coerced:01 dummy:01 mutable:01 garrigue:01 subtype:01 coercion:01 polymorphic:01 abbreviation:02 X-Attachments: type="application/octet-stream" name="a_trees.ml" name="a_trees.ml" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk --Multipart_Thu__13_Mar_2003_10:04:02_+0100_083b35a0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII On Thu, 13 Mar 2003 10:56:56 +0900 Jacques Garrigue wrote: > 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 ! 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 class type definition being accepted, why is the former rejected ? both should be either rejected if this make no sense, or accepted else then I tried to implement it, but I couldn't do it "meaningfully" : ># class a: a_t = object > val mutable l = [] > method m: 'a.( unit; ..> as 'a) -> unit = > fun o -> l <- (o:>a_t) :: l > end;; > Characters 116-117: > fun o -> l <- (o:>a_t) :: l > ^ >This expression cannot be coerced to type > a_t = < m : 'a. (< m : 'a -> unit; .. > as 'a) -> unit >; >it has type < m : 'b -> unit; .. > as 'b but is here used with type > 'b = < m : 'c. < m : 'c -> unit; .. > -> unit; .. >. >This simple coercion was not fully general. Consider using a double >coercion. > > ># class a: a_t = object > val mutable l = [] > method m: 'a.( unit; ..> as 'a) -> unit = > fun o -> l <- (o:( unit; ..> as 'a):>a_t) :: l > end;; > > Characters 115-151: > fun o -> l <- (o:( unit; ..> as 'a):>a_t) :: l > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >Type < m : 'a -> unit; .. > as 'a is not a subtype of type > a_t = < m : 'b. (< m : 'b -> unit; .. > as 'b) -> unit > >The universal variable 'b would escape its scope any idea ? damien --Multipart_Thu__13_Mar_2003_10:04:02_+0100_083b35a0 Content-Type: application/octet-stream; name="a_trees.ml" Content-Disposition: attachment; filename="a_trees.ml" Content-Transfer-Encoding: base64 KCogdXNpbmcgdGhlIHRyaWNrICopCgpjbGFzcyB0eXBlIGFfdCA9IG9iamVjdAogIG1ldGhvZCBh ZGQ6IDxpdDogJ2IuICg8IGFkZCA6ICdhOyBjaGlsZHJlbjogYV90IGxpc3Q7IGdhOiBpbnQ7IC4u ID4gYXMgJ2IpIC0+IHVuaXQgPiBhcyAnYSAKICBtZXRob2QgY2hpbGRyZW46IGFfdCBsaXN0CiAg bWV0aG9kIGdhOiBpbnQKZW5kCgpjbGFzcyBbJ2FdIHdyYXAgbCA9IG9iamVjdAogIG1ldGhvZCBp dDogJ2IuICg8IGFkZCA6ICdhOyBjaGlsZHJlbjogYV90IGxpc3Q7IGdhOiBpbnQ7IC4uID4gYXMg J2IpIC0+IHVuaXQgPSAKICAgIGZ1biBvIC0+IGwgOj0gKG8gOj4gPGFkZDogJ2E7IGNoaWx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9 IG5ldyBhYjEgYQpsZXQgYTIgPSBuZXcgYWIyIGMKbGV0IF8gPSBjI2FkZCBhCmxldCBfID0gYyNh ZGQgYTIKbGV0IF8gPSBhI2FkZCBhMQpsZXQgbCA9IGMjYWwK --Multipart_Thu__13_Mar_2003_10:04:02_+0100_083b35a0-- ------------------- 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