Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] polymorphic methods
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] polymorphic methods
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 = <fun>
> 
> 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