From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 791907EFCD for ; Thu, 30 Oct 2014 17:00:40 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of philippe.veber@gmail.com) identity=pra; client-ip=74.125.82.52; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="philippe.veber@gmail.com"; x-sender="philippe.veber@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of philippe.veber@gmail.com designates 74.125.82.52 as permitted sender) identity=mailfrom; client-ip=74.125.82.52; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="philippe.veber@gmail.com"; x-sender="philippe.veber@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wg0-f52.google.com) identity=helo; client-ip=74.125.82.52; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="philippe.veber@gmail.com"; x-sender="postmaster@mail-wg0-f52.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjcBABdgUlRKfVI0m2dsb2JhbABcDoQsBNUlAoEdBxYBAQEBAREBAQEBAQYLCwkULoQDAQEEEi4BGx0BAwwGBQQHAzgiAREBBQEcBhMiiAkBAxKocj0xjSWDEYhrChknDWeDewEBAQEBAQQBAQEBAQEWAQUOkQIHhEsFj26DJ4pVgTGRBoINGCmDboEEQTwvgksBAQE X-IPAS-Result: AjcBABdgUlRKfVI0m2dsb2JhbABcDoQsBNUlAoEdBxYBAQEBAREBAQEBAQYLCwkULoQDAQEEEi4BGx0BAwwGBQQHAzgiAREBBQEcBhMiiAkBAxKocj0xjSWDEYhrChknDWeDewEBAQEBAQQBAQEBAQEWAQUOkQIHhEsFj26DJ4pVgTGRBoINGCmDboEEQTwvgksBAQE X-IronPort-AV: E=Sophos;i="5.07,286,1413237600"; d="scan'208";a="103805717" Received: from mail-wg0-f52.google.com ([74.125.82.52]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 30 Oct 2014 17:00:39 +0100 Received: by mail-wg0-f52.google.com with SMTP id b13so4010086wgh.25 for ; Thu, 30 Oct 2014 09:00:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=oM1aw2d9ZNzMqaRHobMtZvYWvv+udLODFGB58Ow/WGU=; b=ipKQjveTkV2tz3iTJ85i2gMryrhaXYacKB/snYeHKWYgNrUtKgY1JVGroGez08SkOL CqC6ZgSVP25DgUmGLPpDlX6wKk7A7Q4J0XCdv/k1Tqa+7aiB1E7w2M9+2NChpiJlQqtt +kxlFIjjp267fTwbWETYCtyUQR5GIEUOgZPkAzYeJwfug7idWrefyz41BJk5HTWPyPGE o0eh6BQteDOezCA77xTkSToODiArbUaP6XMmJZe9DJOxHfbq6hu9w9hODMFcXnLgJAp4 JTlheo23bHxJFc37OW62pXYf3CGpM3+comJT6hM8ba2epYFVlxr+XuVwG2hcflen1beE +DYw== X-Received: by 10.194.206.36 with SMTP id ll4mr21620900wjc.21.1414684839721; Thu, 30 Oct 2014 09:00:39 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.63.237 with HTTP; Thu, 30 Oct 2014 09:00:19 -0700 (PDT) In-Reply-To: <86y4rxfuus.fsf@cam.ac.uk> References: <86y4rxfuus.fsf@cam.ac.uk> From: Philippe Veber Date: Thu, 30 Oct 2014 17:00:19 +0100 Message-ID: To: Leo White Cc: caml users Content-Type: multipart/alternative; boundary=047d7b6d92345743200506a5fbbc X-Validation-by: philippe.veber@gmail.com Subject: Re: [Caml-list] Typing of a parameterized class where the type parameter is the type of self. --047d7b6d92345743200506a5fbbc Content-Type: text/plain; charset=ISO-8859-1 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 : > > # 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 > --047d7b6d92345743200506a5fbbc Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Thanks Leo!

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

# class type ['a] c =3D object ('a) method m := 'b. 'b end;;
class type ['a] c =3D object ('a) constrai= nt 'a =3D < 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 exampl= e illustrating when this automatic promotion is indeed useful, I'd be c= urious to see it. In any case, many thanks for the fix, which I'll use = right away.

cheers,
=A0 ph.





2014-10-30 16:19 GMT+01:00 = Leo White <lpw25@cam.ac.uk>:
> # class type ['a] c =3D object ('a) method= m : 'b end;;
> class type ['a] c =3D object ('a) constraint 'a =3D < m= : 'b. 'b; .. > method m : 'b end
>
> In particular I don't understand why the method m gets a polymorph= ic
> 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 thi= s
by writing in the constraint by hand:

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

Which now allows your type definition to work:

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

Regards,

Leo

--047d7b6d92345743200506a5fbbc--