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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 875F77F1AA for ; Thu, 10 Sep 2015 11:13:23 +0200 (CEST) IronPort-PHdr: 9a23:0OauwRwgnR1Ka0zXCy+O+j09IxM/srCxBDY+r6Qd0OsQIJqq85mqBkHD//Il1AaPBtWHrasYw8Pt8IneGkU4qa6bt34DdJEeHzQksu4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2WVTerzWI4CIIHV2nbEwudrizQ9Kapv/0/t7x0qWbWx9Piju5bOE6BzSNhiKViPMrh5B/IL060BrDrygAUe1XwWR1OQDbxE6ktY+YtaRu+CVIuv8n69UIEeCjJ/x5HvRkC2EDOnop5cujmRTmQA+L4TNIV2wMjhtODwXA4RX+Wb/+tyL7sqx23yzMeYX9RLUwHDCj9LtDWQb1hSJBNjgj8WiRhNYjorhcpUeMqxtiypTYKKyLPfxxc6fcZs8bDT5MUsFMXjZND521aYQCC+saIedwoIz0pl9Iphy7U1r/TNjzwyNF0yellZYx1P4sRESfhFQt Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=edouardevangelisti@gmail.com; spf=Pass smtp.mailfrom=edouardevangelisti@gmail.com; spf=None smtp.helo=postmaster@mail-qk0-f169.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of edouardevangelisti@gmail.com) identity=pra; client-ip=209.85.220.169; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="edouardevangelisti@gmail.com"; x-sender="edouardevangelisti@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of edouardevangelisti@gmail.com designates 209.85.220.169 as permitted sender) identity=mailfrom; client-ip=209.85.220.169; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="edouardevangelisti@gmail.com"; x-sender="edouardevangelisti@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qk0-f169.google.com) identity=helo; client-ip=209.85.220.169; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="edouardevangelisti@gmail.com"; x-sender="postmaster@mail-qk0-f169.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0CfAQDFSPFVnKncVdFdg0I1aQaDILwAhgECgTgHPBABAQEBAQEBARABAQEBAQYNCQkhLoIdggcBAQMBEhEEGQEbHgMBCwYDAgs3AgIhAQERAQUBHAYBEiKHdgEDCgiZZY9DgTA+MYtBgWyCeYlzChknDVaEFgEBAQcBAQEBGAEFDoZlhHuCT4JEgmmBQwWVVoUKhgOBbYFMRpEqg1CCIBIjgRcRJ4IugX49M4hJAQEB X-IPAS-Result: A0CfAQDFSPFVnKncVdFdg0I1aQaDILwAhgECgTgHPBABAQEBAQEBARABAQEBAQYNCQkhLoIdggcBAQMBEhEEGQEbHgMBCwYDAgs3AgIhAQERAQUBHAYBEiKHdgEDCgiZZY9DgTA+MYtBgWyCeYlzChknDVaEFgEBAQcBAQEBGAEFDoZlhHuCT4JEgmmBQwWVVoUKhgOBbYFMRpEqg1CCIBIjgRcRJ4IugX49M4hJAQEB X-IronPort-AV: E=Sophos;i="5.17,503,1437429600"; d="scan'208";a="145329295" Received: from mail-qk0-f169.google.com ([209.85.220.169]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 10 Sep 2015 11:13:22 +0200 Received: by qkdw123 with SMTP id w123so15580801qkd.0 for ; Thu, 10 Sep 2015 02:13:19 -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 :content-type; bh=FeNeSIvUH35fxpVkscTE6CKkEchsTxyir1M/HBOdeCA=; b=Tsp6Y0LZVG7om9ZpDYGMYdBHjA200GFHdb2zAU4U+y+YDSHQ9RD7AclpPtrZRUfTbF DjEEIbjnKveiTkY83joKgWlgI40dylmDPz3s8L1VO70Omo6QmBsAy9jW4LV0KOUucLCf 4IkLSS43uzwEEh94rzyFWkIObdFCgOMAWvsJMwZ0bwdd0CnTkW3iCad58TD6+nhJZ4bH beFLdgf1aGoZ8IeuU0JWIXIOVLFgccX+SQTAqLJm3Kiusj3WHQ+HTNWjHUDl7kU+cfh2 bZXKgFN++s265rQ1LjUIxdt5woEBjfqpoPikxnN/+7ONThf6XHyo7ns9IxShNV0BtqNP d34g== MIME-Version: 1.0 X-Received: by 10.55.42.65 with SMTP id q62mr51906675qkh.12.1441876399138; Thu, 10 Sep 2015 02:13:19 -0700 (PDT) Received: by 10.140.38.39 with HTTP; Thu, 10 Sep 2015 02:13:19 -0700 (PDT) In-Reply-To: References: Date: Thu, 10 Sep 2015 10:13:19 +0100 Message-ID: From: Edouard Evangelisti To: Martin DeMello , caml-list@yquem.inria.fr Content-Type: multipart/alternative; boundary=001a114943b094aeef051f610276 Subject: Re: [Caml-list] using a restricted subset of a polymorphic variant --001a114943b094aeef051f610276 Content-Type: text/plain; charset=UTF-8 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 > > --001a114943b094aeef051f610276 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Dear Martin,
In your exampl= e, heading is not of= type dir.
However, this would work = :

val mutable headin= g : dir =3D `Up

Regard= s,
Edouard

=

2015-09-10 10:07 = GMT+01:00 Martin DeMello <martindemello@gmail.com>:
I'm not sure exac= tly why this doesn't work:

type dir =3D [`North | `South | `East | `West | `Up | `Dow= n]

let index =3D function
<= div>=C2=A0 | `North -> 1
=C2=A0 | `South -> 2
=C2=A0 | `East -> 3
=C2=A0 | `West -> 4<= /div>
=C2=A0 | `Up -> 5
=C2=A0 | `Down -> 6<= /div>

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

=C2=A0 method flip_heading =3D
=C2=A0 =C2=A0 heading <- match he= ading with `Up -> `Down | `Down -> `Up

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

=C2=A0 method get_heading =3D self#get_direction heading
end
it fails with
File "test.ml"= ;, line 28, characters 42-49:
Error: This expression has type [ `Down | `Up ]
=C2=A0 =C2=A0 =C2=A0 =C2=A0but an express= ion was expected of type dir
=C2=A0 =C2=A0 =C2=A0 =C2=A0The first variant type does not allow tag= (s)
=C2=A0 =C2=A0 =C2= =A0 =C2=A0`East, `North, `South, `West

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

<= div>martin


--001a114943b094aeef051f610276--