caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Typing of a parameterized class where the type parameter is the type of self.
@ 2014-10-30 14:51 Philippe Veber
  2014-10-30 15:19 ` Leo White
  0 siblings, 1 reply; 5+ messages in thread
From: Philippe Veber @ 2014-10-30 14:51 UTC (permalink / raw)
  To: caml users

[-- Attachment #1: Type: text/plain, Size: 969 bytes --]

Dear list,

I'm not sure why the following class definition is typed the way it is:

        OCaml version 4.02.1

# class type ['a] c = object ('a) method m : 'b end;;
class type ['a] c = object ('a) constraint 'a = < m : 'b. 'b; .. > method m
: 'b end

In particular I don't understand why the method m gets a polymorphic type.
This bites me when I try to specialize the type :

# type 'a t = (< m : int ; .. > as 'a) c;;
Error: This type < m : int; .. > should be an instance of type 'a c as 'a
Types for method m are incompatible

Instead of the type with polymorphic methods, I would have expected the
following type:

# class type ['a] c = object ('a) constraint 'a = < m : 'b; .. > method m :
'b end;;
class type ['a] c = object ('a) constraint 'a = < m : 'b; .. > method m :
'b end

And then have no problems doing what I wanted:

# type 'a t = (< m : int ; .. > as 'a) c;;
type 'a t = 'a constraint 'a = < m : int >

What did I get wrong?

Cheers,
  Philippe.

[-- Attachment #2: Type: text/html, Size: 1540 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Typing of a parameterized class where the type parameter is the type of self.
  2014-10-30 14:51 [Caml-list] Typing of a parameterized class where the type parameter is the type of self Philippe Veber
@ 2014-10-30 15:19 ` Leo White
  2014-10-30 16:00   ` Philippe Veber
  0 siblings, 1 reply; 5+ messages in thread
From: Leo White @ 2014-10-30 15:19 UTC (permalink / raw)
  To: Philippe Veber; +Cc: caml users

> # class type ['a] c = object ('a) method m : 'b end;;
> class type ['a] c = object ('a) constraint 'a = < m : 'b. 'b; .. > method m : 'b end
>
> In particular I don't understand why the method m gets a polymorphic
> type.

To allow shorter type annotations for class types, OCaml tries to
automatically turn methods with polymorphic types into polymorphic
methods. In this case it is being a bit over eager and making your
method polymorphic even though it doesn't need to.

You can fix this by making it more obvious that your method's
polymorphism is contained within the class parameter 'a. You can do this
by writing in the constraint by hand:

    class type ['a] c = object ('a)
      constraint 'a = < m : 'b; ..>
      method m : 'b
    end

Which now allows your type definition to work:

    # type 'a t = (< m : int ; .. > as 'a) c;;
    type 'a t = 'a c constraint 'a = < m : int >

Regards,

Leo

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Typing of a parameterized class where the type parameter is the type of self.
  2014-10-30 15:19 ` Leo White
@ 2014-10-30 16:00   ` Philippe Veber
  2014-10-30 16:07     ` Leo White
  0 siblings, 1 reply; 5+ messages in thread
From: Philippe Veber @ 2014-10-30 16:00 UTC (permalink / raw)
  To: Leo White; +Cc: caml users

[-- Attachment #1: Type: text/plain, Size: 1697 bytes --]

Thanks Leo!

This default behaviour is a bit surprising since usually, methods are not
typed as polymorphic unless stated explicitely. Here, if I wanted the
method m to be more polymorphic I could have written this:

# class type ['a] c = object ('a) method m : 'b. 'b end;;
class type ['a] c = object ('a) constraint 'a = < m : 'b. 'b; .. > method m
: 'b end

This was an easy way to say that the polymorphism of m was not captured by
'a. I don't want to abuse your time, but if you have a tiny example
illustrating when this automatic promotion is indeed useful, I'd be curious
to see it. In any case, many thanks for the fix, which I'll use right away.

cheers,
  ph.





2014-10-30 16:19 GMT+01:00 Leo White <lpw25@cam.ac.uk>:

> > # class type ['a] c = object ('a) method m : 'b end;;
> > class type ['a] c = object ('a) constraint 'a = < m : 'b. 'b; .. >
> method m : 'b end
> >
> > In particular I don't understand why the method m gets a polymorphic
> > type.
>
> To allow shorter type annotations for class types, OCaml tries to
> automatically turn methods with polymorphic types into polymorphic
> methods. In this case it is being a bit over eager and making your
> method polymorphic even though it doesn't need to.
>
> You can fix this by making it more obvious that your method's
> polymorphism is contained within the class parameter 'a. You can do this
> by writing in the constraint by hand:
>
>     class type ['a] c = object ('a)
>       constraint 'a = < m : 'b; ..>
>       method m : 'b
>     end
>
> Which now allows your type definition to work:
>
>     # type 'a t = (< m : int ; .. > as 'a) c;;
>     type 'a t = 'a c constraint 'a = < m : int >
>
> Regards,
>
> Leo
>

[-- Attachment #2: Type: text/html, Size: 2379 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Typing of a parameterized class where the type parameter is the type of self.
  2014-10-30 16:00   ` Philippe Veber
@ 2014-10-30 16:07     ` Leo White
  2014-10-30 16:13       ` Philippe Veber
  0 siblings, 1 reply; 5+ messages in thread
From: Leo White @ 2014-10-30 16:07 UTC (permalink / raw)
  To: Philippe Veber; +Cc: caml users

> This was an easy way to say that the polymorphism of m was not captured by 'a. I don't want to abuse your time, but if
> you have a tiny example illustrating when this automatic promotion is indeed useful, I'd be curious to see it. In any
> case, many thanks for the fix, which I'll use right away.

I think that the automatic promotion is just for convenience to avoid
writing "'a . ..." on lots of methods. Your example should probably be
considered a bug since the promotion should really only happen when the
method type is genuinely polymorphic, but I don't know how easy it is to
fix.

Regards,

Leo

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Typing of a parameterized class where the type parameter is the type of self.
  2014-10-30 16:07     ` Leo White
@ 2014-10-30 16:13       ` Philippe Veber
  0 siblings, 0 replies; 5+ messages in thread
From: Philippe Veber @ 2014-10-30 16:13 UTC (permalink / raw)
  To: Leo White; +Cc: caml users

[-- Attachment #1: Type: text/plain, Size: 782 bytes --]

OK, thanks a lot Leo. I'll report that on mantis to keep track of that.
Cheers,
  ph.


2014-10-30 17:07 GMT+01:00 Leo White <lpw25@cam.ac.uk>:

> > This was an easy way to say that the polymorphism of m was not captured
> by 'a. I don't want to abuse your time, but if
> > you have a tiny example illustrating when this automatic promotion is
> indeed useful, I'd be curious to see it. In any
> > case, many thanks for the fix, which I'll use right away.
>
> I think that the automatic promotion is just for convenience to avoid
> writing "'a . ..." on lots of methods. Your example should probably be
> considered a bug since the promotion should really only happen when the
> method type is genuinely polymorphic, but I don't know how easy it is to
> fix.
>
> Regards,
>
> Leo
>

[-- Attachment #2: Type: text/html, Size: 1190 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-10-30 16:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-30 14:51 [Caml-list] Typing of a parameterized class where the type parameter is the type of self Philippe Veber
2014-10-30 15:19 ` Leo White
2014-10-30 16:00   ` Philippe Veber
2014-10-30 16:07     ` Leo White
2014-10-30 16:13       ` Philippe Veber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).