List Info

Thread: "ocaml_beginners"::[] polymorphic methods (and I've read the same named section of the manual :-)




"ocaml_beginners"::[] polymorphic methods (and I've read the same named section of the manual :-)
user name
2006-07-28 19:28:00
On Fri, 28 Jul 2006, vincent.aravantinos wrote:

> --- In ocaml_beginners@yahoogroups.com, Martin Jambon
> <martin_jambon...> wrote:
>>
>> On Thu, 27 Jul 2006, vincent.aravantinos wrote:
>>
>>> Even if I only define the class type :
>>>
>>> class type ['a] myclass =
>>>  object val the_list : 'a list method map :
'a -> 'b myclass end;;
>>>
>>> then it automatically turns it into :
>>>
>>> class type ['a] myclass =
>>>  object val the_list : 'a list method map :
'a -> 'a myclass end
>>>
>>> ????
>>
>> It's not specific to classes:
>>
>> # let rec f x y = if false then f y x;;
>> val f : 'a -> 'a -> unit = <fun>
>>
>> could be:
>>
>> val f : 'a -> 'b -> unit = <fun>
>
> Are you sure this is the same problem ?

Looks like it.

# let rec f a b = f b a;;
val f : 'a -> 'a -> 'b = <fun>

(not 'a -> 'b -> 'c)

That's the record version of your class where the map
method is not 
polymorphic:

# type ('a, 'b) t = { list : 'a list; map : ('a ->
'b) -> ('a, 'b) t };;
type ('a, 'b) t = { list : 'a list; map : ('a -> 'b)
-> ('a, 'b) t; }
# let rec create l = { list = l; map = fun f -> create
(List.map f l) };;
val create : 'a list -> ('a, 'a) t = <fun>

And it's not possible to make the map method polymorphic
because 'b will 
be equal to 'a, which is fixed because of the list field:

# type 'a t = { list : 'a list; map : 'b.('a -> 'b)
-> 'b t };;
type 'a t = { list : 'a list; map : 'b. ('a -> 'b)
-> 'b t; }
# let rec create l = { list = l; map = fun f -> create
(List.map f l) };;
                                       
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This field value has type ('a -> 'a) -> 'a t which
is less general than
   'b. ('c -> 'b) -> 'b t


Using tuples and the -rectypes option it goes like this:

# let rec create l = (l, fun f -> create (List.map f
l));;
val create : 'a list -> ('a list * (('a -> 'a)
-> 'b) as 'b) = <fun>

so we still have the problem that all the occurrences of
create 
are unified to the same type.


Martin

--
Martin Jambon, PhD
http://martin.jambon.fre
e.fr


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 


"ocaml_beginners"::[] Re: polymorphic methods (and I've read the same named section of the manual
user name
2006-07-28 21:09:02
--- In ocaml_beginners@yahoogroups.com, Martin Jambon 
<martin_jambon...> wrote:
>
> # let rec f a b = f b a;;
> val f : 'a -> 'a -> 'b = <fun>
> 
> (not 'a -> 'b -> 'c)

IMHO it's normal :
'f' generally has type 'a -> 'b - > 'c
so that here 'a' has type 'a
             'b' has type 'b
so if we admit f b a is well defined,
then 'f' also has type 'b -> 'a -> 'c

therefore 'a = 'b

so it looks normal to me ; am I wrong ?

> That's the record version of your class where the map
method is not 
> polymorphic:
> 
> # type ('a, 'b) t = { list : 'a list; map : ('a
-> 'b) -> ('a, 'b) 
t };;
> type ('a, 'b) t = { list : 'a list; map : ('a ->
'b) -> ('a, 'b) 
t; }
> # let rec create l = { list = l; map = fun f ->
create (List.map f 
l) };;
> val create : 'a list -> ('a, 'a) t = <fun>
> 
> And it's not possible to make the map method
polymorphic because 'b 
will 
> be equal to 'a, which is fixed because of the list
field:

I don't understand why the type is fixed for one instance ;
but why 
should it be identical among all instances ? Sorry I don't 
understand :(

> # type 'a t = { list : 'a list; map : 'b.('a ->
'b) -> 'b t };;
> type 'a t = { list : 'a list; map : 'b. ('a ->
'b) -> 'b t; }
> # let rec create l = { list = l; map = fun f ->
create (List.map f 
l) };;
>                                         
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> This field value has type ('a -> 'a) -> 'a t
which is less general 
than
>    'b. ('c -> 'b) -> 'b t
> 
> 
> Using tuples and the -rectypes option it goes like
this:
> 
> # let rec create l = (l, fun f -> create (List.map f
l));;
> val create : 'a list -> ('a list * (('a ->
'a) -> 'b) as 'b) = <fun>
> 
> so we still have the problem that all the occurrences
of create 
> are unified to the same type.

Ok but is that a justified compiler behaviour ?
Isn't that a bug ?

> 
> Martin
> 
> --
> Martin Jambon, PhD
> http://martin.jambon.fre
e.fr
>






Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )