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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id D07C47EC41 for ; Thu, 25 Oct 2012 14:15:20 +0200 (CEST) Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of didier.cassirame@gmail.com) identity=pra; client-ip=209.85.219.54; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="didier.cassirame@gmail.com"; x-sender="didier.cassirame@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail1-smtp-roc.national.inria.fr: domain of didier.cassirame@gmail.com designates 209.85.219.54 as permitted sender) identity=mailfrom; client-ip=209.85.219.54; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="didier.cassirame@gmail.com"; x-sender="didier.cassirame@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-oa0-f54.google.com) identity=helo; client-ip=209.85.219.54; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="didier.cassirame@gmail.com"; x-sender="postmaster@mail-oa0-f54.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Au4BADcsiVDRVds2m2dsb2JhbABEhhapfokmAYhcCCMBAQEBAQgJCwkUJ4IeAQEBAwESAg8dARsSCwEDAQsGAwILAxcdAgIhAQERAQUBChIGExIQh08BAwkGC55IYAkDi1lPgnaEeQoZJwMKWYh1AQUMim6GQYETA5E2gmmBVYEXihaDLxYphBM X-IronPort-AV: E=Sophos;i="4.80,646,1344204000"; d="scan'208";a="178898556" Received: from mail-oa0-f54.google.com ([209.85.219.54]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 25 Oct 2012 14:15:19 +0200 Received: by mail-oa0-f54.google.com with SMTP id n9so2694910oag.27 for ; Thu, 25 Oct 2012 05:15:18 -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=a8rFZHqGyV6cfbVW46kEQwgffE0jwxPNLMO4I17KoRU=; b=jLLydcSfTGIFFolNau7veR5Llf3JG04BHy39jf5UCBTiDDQDxTtVrCS/0Dw6WRDDTz pGpsoaWBiADbGKOBGh5qmq9+ZVNxsYKQPW9fN5x3QtqIoHmOt6/i9nu9T0JrVGibTJyG EwyRLzR2TmgP9F4SpLVXBWxTlRD8b5Vi59Hd7ECO3/I0Hy6Ji2idTi5Qu6fkJibONQjU ++B1VFZ9aFTgIXLiWrinBwik31yv67e/YjWahPLChB8oOBUQ0Oxcx3/2mqDDXVKdRIrt kS9n6P1b0itHcSK33A/pGNKxnhif/224wEFn3iI6BLcmsuhFuZTs/r0luTf6lppeb4HJ k5Og== Received: by 10.60.31.101 with SMTP id z5mr17101965oeh.110.1351167318478; Thu, 25 Oct 2012 05:15:18 -0700 (PDT) MIME-Version: 1.0 Received: by 10.60.137.198 with HTTP; Thu, 25 Oct 2012 05:14:58 -0700 (PDT) In-Reply-To: References: <87txtiix6r.fsf@golf.niidar.ru> From: Didier Cassirame Date: Thu, 25 Oct 2012 14:14:58 +0200 Message-ID: To: Ivan Gotovchits Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=e89a8fb1f1960cd0c204cce12830 Subject: Re: [Caml-list] Polymorphic variants and inheritance --e89a8fb1f1960cd0c204cce12830 Content-Type: text/plain; charset=UTF-8 type odds = [`One | `Three ] type evens = [`Two | `Four ] type numbers = [ odds | evens ] class type ['a] number = object constraint 'a = [< numbers] method category: 'a end class type odd = object inherit [odds] number method category: odds end class type even = object inherit [evens] number method category: evens end It's probably even better like this :) Cheers, didier 2012/10/25 Didier Cassirame > Hi Ivan, > > You may use parameterized classes like this : > > type odds = [`One | `Three ] > type evens = [`Two | `Four ] > type numbers = [ odds | evens ] > > class type ['a] number = object > constraint 'a = [< numbers] > method category: 'a > end > > class type ['a] odd = object > inherit ['a] number > > constraint 'a = odds > > method category: 'a > end > > > class type ['a] even = object > inherit ['a] number > constraint 'a = evens > method category: 'a > end > > This passes the compilation. > > didier > > > 2012/10/25 Ivan Gotovchits > >> >> Here a simple example that illustrates the problem: >> >> type odds = [`One | `Three ] >> type evens = [`Two | `Four ] >> type numbers = [ odds | evens ] >> >> class type number = object >> method category: numbers >> end >> >> class type odd = object >> inherit number >> method category: odds >> end >> >> >> class type even = object >> inherit number >> method category: evens >> end >> >> >> >> It fails to compile, stating the following error: >> The method category has type odds but is expected to have type >> numbers >> Type odds = [ `One | `Three ] is not compatible with type >> numbers = [ `Four | `One | `Three | `Two ] >> The first variant type does not allow tag(s) `Four, `Two >> >> Just by theory, odds are covariant to numbers, so this kind of subtyping >> can be done. Is it possible to persuade compiler? >> >> -- >> (__) >> (oo) >> /------\/ >> / | || >> * /\---/\ >> ~~ ~~ >> ...."Have you mooed today?"... >> >> -- >> Caml-list mailing list. Subscription management and archives: >> https://sympa.inria.fr/sympa/arc/caml-list >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs >> > > --e89a8fb1f1960cd0c204cce12830 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

type odds =3D [`One | `Three ]
type even= s =3D [`Two | `Four ]
type numbers =3D [ odds | evens ]

class type ['a] number =3D object
=C2=A0 cons= traint 'a =3D [< numbers]
=C2=A0 method category: 'a
end

= class type odd =3D object
=C2=A0 inherit [odds] number
= =C2=A0 method category: odds
end

class t= ype even =3D object
=C2=A0 inherit [evens] number=C2=A0
=C2=A0 method category: = evens
end

It's probably even b= etter like this :)

Cheers,

didier


2012/10/25 Didier = Cassirame <didier.cassirame@gmail.com>
Hi Ivan,

You may use parameterized classes like th= is :

type odds =3D [`One | `Three= ]
type evens =3D [`Two | `Four ]
type numbers =3D [ od= ds | evens ]

class type ['a] number =3D object
=C2=A0= constraint 'a =3D [< numbers]
=C2=A0 method category: = 9;a
end

class type ['a] odd =3D obje= ct
=C2=A0 inherit ['a] number

=C2=A0 constraint 'a =3D odds

<= div>=C2=A0 method category: 'a
end

<= br>
class type ['a] even =3D object
=C2=A0 inherit = ['a] number=C2=A0
=C2=A0 constraint 'a =3D evens
=C2=A0 method category: &= #39;a
end

This passes the compilation.

didi= er


2012/10/25 Ivan Gotovchits <ivg@ieee.org>

Here a simple example that illustrates the problem:

=C2=A0 =C2=A0type odds =3D [`One | `Three ]
=C2=A0 =C2=A0type evens =3D [`Two | `Four ]
=C2=A0 =C2=A0type numbers =3D [ odds | evens ]

=C2=A0 =C2=A0class type number =3D object
=C2=A0 =C2=A0 =C2=A0method category: numbers
=C2=A0 =C2=A0end

=C2=A0 =C2=A0class type odd =3D object
=C2=A0 =C2=A0 =C2=A0inherit number
=C2=A0 =C2=A0 =C2=A0method category: odds
=C2=A0 =C2=A0end


=C2=A0 =C2=A0class type even =3D object
=C2=A0 =C2=A0 =C2=A0inherit number
=C2=A0 =C2=A0 =C2=A0method category: evens
=C2=A0 =C2=A0end



It fails to compile, stating the following error:
=C2=A0 =C2=A0 =C2=A0 =C2=A0The method category has type odds but is expecte= d to have type numbers
=C2=A0 =C2=A0 =C2=A0 =C2=A0Type odds =3D [ `One | `Three ] is not compatibl= e with type
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0numbers =3D [ `Four | `One | `Three | `Tw= o ]
=C2=A0 =C2=A0 =C2=A0 =C2=A0The first variant type does not allow tag(s) `Fo= ur, `Two

Just by theory, odds are covariant to numbers, so this kind of subtyping
can be done. Is it possible to persuade compiler?

--
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(__)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(oo)
=C2=A0 =C2=A0/------\/
=C2=A0 / | =C2=A0 =C2=A0||
=C2=A0* =C2=A0/\---/\
=C2=A0 =C2=A0 ~~ =C2=A0 ~~
...."Have you mooed today?"...

--
Caml-list mailing list. =C2=A0Subscription management and archives:
ht= tps://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


--e89a8fb1f1960cd0c204cce12830--