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 DAA01303; Mon, 15 Jul 2002 03:05:13 +0200 (MET DST) 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 DAA01447 for ; Mon, 15 Jul 2002 03:05:12 +0200 (MET DST) 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 g6F15Aj07412 for ; Mon, 15 Jul 2002 03:05:11 +0200 (MET DST) 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 KAA04504; Mon, 15 Jul 2002 10:05:05 +0900 (JST) To: nadji@noos.fr Cc: caml-list@inria.fr Subject: Re: [Caml-list] polymorphic methods In-Reply-To: <3D320665.AC5539CD@noos.fr> References: <3D320665.AC5539CD@noos.fr> X-Mailer: Mew version 1.94.2 on Emacs 20.7 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20020715100504O.garrigue@kurims.kyoto-u.ac.jp> Date: Mon, 15 Jul 2002 10:05:04 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: nadji@noos.fr > While playing with the current cvs version of ocaml (3.04+15), > several questions came to me concerning the semantics of > polymorphic methods. For example : > > # class c = object > method m: 'a . unit -> 'a -> 'a = fun _ x -> x > end;; > class c : object method m : unit -> 'a -> 'a end > # let f = (new c)#m ();; > val f : '_a -> '_a = > > Shouldn't the type of f be : 'a -> 'a ? > (Would it be sound/implementable/easy to add ?) Unsound. RTFM (polymorphism restricted to values) Try "let f x = (new c)#m () x" But I don't see why you need (). > Second question: > # class ['a] d = object > method m: 'a -> 'a = fun x -> x > end;; > class ['a] d : object method m : 'a -> 'a end > # class subd = object > method ~m: 'a. 'a -> 'a = fun x -> x > end;; > class subd : object method m : 'a -> 'a end > # let v = (new subd :> 'a d);; > Characters 9-17: > let v = (new subd :> 'a d);; > ^^^^^^^^ > This expression cannot be coerced to type 'a d = < m : 'a -> 'a >; > it has type subd = < m : 'b. 'b -> 'b > but is here used with type 'a d > > Or even the easier : > # class d' = object > method m x = x + 0 > end;; > class d' : object method m : int -> int end > # let v' = (new subd :> d');; > Characters 10-18: > let v' = (new subd :> d');; > ^^^^^^^^ > This expression cannot be coerced to type d' = < m : int -> int >; > it has type subd = < m : 'a. 'a -> 'a > but is here used with type d' > > But the type ('b. 'b -> 'b) is more general than ('a -> 'a) > or (int -> int), no ? > Can someone give me some hints why I can't coerce subd ? Sorry, but there is no handling of instanciation via subtyping. Currently subtyping and instanciation are orthogonal concepts: you cannot subtype in an .mli, and you cannot instanciate an explicitly polymorphic type when subtyping. I believe it's correct, but you won't make me write the code without a proof :-) 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