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 034CD7F1AA for ; Thu, 10 Sep 2015 21:14:24 +0200 (CEST) IronPort-PHdr: 9a23:DREjXRfVDRRqiE5FILcfvhLRlGMj4u6mDksu8pMizoh2WeGdxc6zZB7h7PlgxGXEQZ/co6odzbGG7+a5BydZusnJmUtBWaIPfidNsd8RkQ0kDZzNImzAB9muURYHGt9fXkRu5XCxPBsdMs//Y1rPvi/6tmZKSV3BPAZ4bt74BpTVx5zukbvip9uKPU4R32X1SIgxBSv1hD2ZjtMRj4pmJ/R54TryiVwMRd5rw3h1L0mYhRf265T41pdi9yNNp6BprJYYAu3SNp41Rr1ADTkgL3t9pIiy7UGCHkOz4S45W38KkxwAIQHA5hb8FsP0uzHmt+w72iCfN8rwZb0yQnGm/qBtDhjyh3FUGSQ+9TTyg9ZsjaQTjB+Eqhh7xMaAZYiPKPt4cq7YcNceQkJOW89QU2pKBYbqPNhHNPYIIesN99q1nFAJtxbrQFD0XO4= Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=martindemello@gmail.com; spf=Pass smtp.mailfrom=martindemello@gmail.com; spf=None smtp.helo=postmaster@mail-vk0-f49.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of martindemello@gmail.com) identity=pra; client-ip=209.85.213.49; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="martindemello@gmail.com"; x-sender="martindemello@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of martindemello@gmail.com designates 209.85.213.49 as permitted sender) identity=mailfrom; client-ip=209.85.213.49; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="martindemello@gmail.com"; x-sender="martindemello@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-vk0-f49.google.com) identity=helo; client-ip=209.85.213.49; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="martindemello@gmail.com"; x-sender="postmaster@mail-vk0-f49.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BEAQDf1fFVmzHVVdFdg3dpBoMgvAKGAQKBSgc8EAEBAQEBAQEBEAEBAQEBBgsLCSEugh2CBgEBAQMBEhEEGQEbHQEDAQsGAwILDScDAgIhAQERAQUBCxEGEyKHdgEDCgibEI9DgTA+MYtBgWyCeYlqChknDVaEDQEBAQEBAQEDAQEBAQEBARUBBQ6GZYR7gk+CPQeCaYFDBY1sh2qFCoYDgW2BTEaRKoNQgiASI4EXOIIugh0eM4hJAQEB X-IPAS-Result: A0BEAQDf1fFVmzHVVdFdg3dpBoMgvAKGAQKBSgc8EAEBAQEBAQEBEAEBAQEBBgsLCSEugh2CBgEBAQMBEhEEGQEbHQEDAQsGAwILDScDAgIhAQERAQUBCxEGEyKHdgEDCgibEI9DgTA+MYtBgWyCeYlqChknDVaEDQEBAQEBAQEDAQEBAQEBARUBBQ6GZYR7gk+CPQeCaYFDBY1sh2qFCoYDgW2BTEaRKoNQgiASI4EXOIIugh0eM4hJAQEB X-IronPort-AV: E=Sophos;i="5.17,506,1437429600"; d="scan'208";a="176906088" Received: from mail-vk0-f49.google.com ([209.85.213.49]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 10 Sep 2015 21:14:23 +0200 Received: by vkfp126 with SMTP id p126so14714341vkf.3 for ; Thu, 10 Sep 2015 12:14:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=OEdUgENQep6EfRDnP4UnjC1T0OGlsxp2OqmuxJPALxE=; b=A+jFT9PAVCFNcZgdWKPG6lh0WHsw8jjvjIGgJ4UKLjNVROLo7xTFjh/zZdy+qqxMEK 0gD4uw5QOaTxTVdePcUrYz615Hkyz77Qhfi7YO0hUDXSP/YuR6UfaZakWm+O6pqFQl4q 6mTMQPpjTtv+rpspq/xKRDclF4rogFe87oApOV1WcosRFkZq300cvFleORK4PEJ1yJz4 V8kD2+Dvi80JRYjTSE6YZXX0gL2QvxpuGOf0AGl8oHfYfGKjpZj1qLBspBZJ7cTNFC17 JkYJZC0K3q0EDZKyAT/rtdRnHMsCzwt/MR3wSDbi17qnnfa7eblGJOdYCZtkfth9k7Nx sPDw== MIME-Version: 1.0 X-Received: by 10.31.132.131 with SMTP id g125mr2074390vkd.40.1441912461482; Thu, 10 Sep 2015 12:14:21 -0700 (PDT) Received: by 10.103.51.142 with HTTP; Thu, 10 Sep 2015 12:14:21 -0700 (PDT) In-Reply-To: <55F1C5EE.7010205@ens-lyon.org> References: <55F1C5EE.7010205@ens-lyon.org> Date: Thu, 10 Sep 2015 12:14:21 -0700 Message-ID: From: Martin DeMello To: Martin Jambon Cc: OCaml Mailing List , Edouard Evangelisti Content-Type: multipart/alternative; boundary=001a1143d7b6108aa7051f696822 Subject: Re: [Caml-list] using a restricted subset of a polymorphic variant --001a1143d7b6108aa7051f696822 Content-Type: text/plain; charset=UTF-8 Thanks all! I was trying to express the fact that heading could only be `Up or `Down but should act as a direction otherwise; Romain is right that I was expecting the coercion to happen automatically (too much time in other languages :)). Annotating val mutable heading : [`Up | `Down] = `Up and then coercing it when I pass it as a direction worked nicely. martin On Thu, Sep 10, 2015 at 11:03 AM, Martin Jambon wrote: > I'd like to add that this a good systematic way to avoid confusing error > messages when dealing with polymorphic variants (or objects). Just add type > annotations on variables and function parameters which are polymorphic > variants (or objects). > > Here only one extra annotation would be needed, on the heading value: > > val mutable heading : dir = `Up > method flip_heading = ... > method get_direction (x : dir) = ... > method get_heading = ... > > The error message becomes closer to the actual error: > > File "toto.ml", line 15, characters 15-61: > Warning 8: this pattern-matching is not exhaustive. > Here is an example of a value that is not matched: > (`West|`South|`North|`East) > > Martin > > On 09/10/2015 02:13 AM, Edouard Evangelisti wrote: > >> Dear Martin, >> >> In your example, heading is not of type dir. >> However, this would work : >> >> val mutable heading : dir = `Up >> >> Regards, >> Edouard >> >> >> 2015-09-10 10:07 GMT+01:00 Martin DeMello > >: >> >> I'm not sure exactly why this doesn't work: >> >> type dir = [`North | `South | `East | `West | `Up | `Down] >> >> let index = function >> | `North -> 1 >> | `South -> 2 >> | `East -> 3 >> | `West -> 4 >> | `Up -> 5 >> | `Down -> 6 >> >> class foo = object(self) >> val mutable heading = `Up >> >> method flip_heading = >> heading <- match heading with `Up -> `Down | `Down -> `Up >> >> method get_direction (x : dir) = index x >> >> method get_heading = self#get_direction heading >> end >> >> it fails with >> File "test.ml ", line 28, characters 42-49: >> Error: This expression has type [ `Down | `Up ] >> but an expression was expected of type dir >> The first variant type does not allow tag(s) >> `East, `North, `South, `West >> >> but why is that an error? I'd think that any function that accepts >> type dir should accept type [`Down | `Up] as well. Also, how do I >> get this to work? >> >> martin >> >> >> --001a1143d7b6108aa7051f696822 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Thanks all! I was trying to express the fact that heading = could only be `Up or `Down but should act as a direction otherwise; Romain = is right that I was expecting the coercion to happen automatically (too muc= h time in other languages :)). Annotating=C2=A0

val muta= ble heading : [`Up | `Down] =3D `Up

and then c= oercing it when I pass it as a direction worked nicely.

martin

On Thu, Sep 10, 2015 at 11:03 AM, Martin Jambon &l= t;martin.ja= mbon@ens-lyon.org> wrote:
I= 'd like to add that this a good systematic way to avoid confusing error= messages when dealing with polymorphic variants (or objects). Just add typ= e annotations on variables and function parameters which are polymorphic va= riants (or objects).

Here only one extra annotation would be needed, on the heading value:

=C2=A0 val mutable heading : dir =3D `Up
=C2=A0 method flip_heading =3D ...
=C2=A0 method get_direction (x : dir) =3D ...
=C2=A0 method get_heading =3D ...

The error message becomes closer to the actual error:

File "= toto.ml", line 15, characters 15-61:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
(`West|`South|`North|`East)

Martin

On 09/10/2015 02:13 AM, Edouard Evangelisti wrote:
Dear Martin,

In your example, heading is not of type dir.
However, this would work :

val mutable heading : dir =3D `Up

Regards,
Edouard


2015-09-10 10:07 GMT+01:00 Martin DeMello <martindemello@gmail.com
<mailto:mar= tindemello@gmail.com>>:

=C2=A0 =C2=A0 I'm not sure exactly why this doesn't work:

=C2=A0 =C2=A0 type dir =3D [`North | `South | `East | `West | `Up | `Down]<= br>
=C2=A0 =C2=A0 let index =3D function
=C2=A0 =C2=A0 =C2=A0 =C2=A0| `North -> 1
=C2=A0 =C2=A0 =C2=A0 =C2=A0| `South -> 2
=C2=A0 =C2=A0 =C2=A0 =C2=A0| `East -> 3
=C2=A0 =C2=A0 =C2=A0 =C2=A0| `West -> 4
=C2=A0 =C2=A0 =C2=A0 =C2=A0| `Up -> 5
=C2=A0 =C2=A0 =C2=A0 =C2=A0| `Down -> 6

=C2=A0 =C2=A0 class foo =3D object(self)
=C2=A0 =C2=A0 =C2=A0 =C2=A0val mutable heading =3D `Up

=C2=A0 =C2=A0 =C2=A0 =C2=A0method flip_heading =3D
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0heading <- match heading with `Up ->= ; `Down | `Down -> `Up

=C2=A0 =C2=A0 =C2=A0 =C2=A0method get_direction (x : dir) =3D index x

=C2=A0 =C2=A0 =C2=A0 =C2=A0method get_heading =3D self#get_direction headin= g
=C2=A0 =C2=A0 end

=C2=A0 =C2=A0 it fails with
=C2=A0 =C2=A0 File "test.ml <http://test.ml>", line 28, characters 42-49:=
=C2=A0 =C2=A0 Error: This expression has type [ `Down | `Up ]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 but an expression was expected of= type dir
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 The first variant type does not a= llow tag(s)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 `East, `North, `South, `West

=C2=A0 =C2=A0 but why is that an error? I'd think that any function tha= t accepts
=C2=A0 =C2=A0 type dir should accept type [`Down | `Up] as well. Also, how = do I
=C2=A0 =C2=A0 get this to work?

=C2=A0 =C2=A0 martin



--001a1143d7b6108aa7051f696822--