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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by sympa.inria.fr (Postfix) with ESMTPS id F1A0B7EC41 for ; Thu, 25 Oct 2012 13:57:27 +0200 (CEST) Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of didier.cassirame@gmail.com) identity=pra; client-ip=209.85.219.54; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="didier.cassirame@gmail.com"; x-sender="didier.cassirame@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail4-smtp-sop.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=mail4-smtp-sop.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 (mail4-smtp-sop.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=mail4-smtp-sop.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: Au4BAHAoiVDRVds2m2dsb2JhbABEhhapfokmAYhcCCMBAQEBAQgJCwkUJ4IeAQEBAwESAg8dARsSCwEDDAYDAgQHAxcdAgIiAREBBQEKEgYTCAoQh08BAwkGC547YAkDi1lPgnaEdQoZJwMKWYh1AQUMkS+BEwORNoQ+gReNRRYphBM X-IronPort-AV: E=Sophos;i="4.80,646,1344204000"; d="scan'208";a="160367460" Received: from mail-oa0-f54.google.com ([209.85.219.54]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 25 Oct 2012 13:57:26 +0200 Received: by mail-oa0-f54.google.com with SMTP id n9so2668949oag.27 for ; Thu, 25 Oct 2012 04:57:25 -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=hXG4gEaZzXa9BXDB5iXB9Gj8c37L8k0g8buwx0MB+ik=; b=oR01MnjcqHlN+iIXEDytak6K+IVwInBFwep/bHdIVVrrM8qVZFYQMriYeQH24uhqU9 McKuKkiKcG05AjBoSUBmx/4QOPfOG6SVi36q9N6wLWIlw4/inVZPK/8g9/Xj+A9ZfaDA m9YAf/t0xwgypDsBoo6Ka6MLYb1PXKLs9izDRMVsNVi/xI8Z0O0olBNe3ZSr/WXaZ1iB Q8M/32dvpVhvlckLQohU6laH6zGYHO9O+oKV/X9dcgZiqmdVoYi6T8squY8MrJ81ZcZB vwsBDOL/lkngu8S9C0xXF7tDfGxKu/nUocpO6ngnM2j9U8Lprfzx/ZJY96x+DZxQqU8t sB6Q== Received: by 10.182.108.69 with SMTP id hi5mr15858843obb.43.1351166245757; Thu, 25 Oct 2012 04:57:25 -0700 (PDT) MIME-Version: 1.0 Received: by 10.60.137.198 with HTTP; Thu, 25 Oct 2012 04:57:05 -0700 (PDT) In-Reply-To: <87txtiix6r.fsf@golf.niidar.ru> References: <87txtiix6r.fsf@golf.niidar.ru> From: Didier Cassirame Date: Thu, 25 Oct 2012 13:57:05 +0200 Message-ID: To: Ivan Gotovchits Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=f46d0447a0911c644904cce0e8cb Subject: Re: [Caml-list] Polymorphic variants and inheritance --f46d0447a0911c644904cce0e8cb Content-Type: text/plain; charset=UTF-8 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 > --f46d0447a0911c644904cce0e8cb Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi Ivan,

You may use parameterized classes like th= is :

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

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

class type ['a] odd =3D object
=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.

didier


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

--f46d0447a0911c644904cce0e8cb--