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 B98E27F1AA for ; Thu, 10 Sep 2015 11:19:56 +0200 (CEST) IronPort-PHdr: 9a23:I88wWxGs5bXs2gJRYjnsxp1GYnF86YWxBRYc798ds5kLTJ75r8uwAkXT6L1XgUPTWs2DsrQf27aQ6vyocFdDyKjCmUhKSIZLWR4BhJdetC0bK+nBN3fGKuX3ZTcxBsVIWQwt1Xi6NU9IBJS2PAWK8TWM5DIfUi/yKRBybrysXNWC04Lui6ibwN76XUZhvHKFe7R8LRG7/036l/I9ps9cEJs30QbDuXBSeu5blitCLFOXmAvgtI/rpMYwuwwZgf8q9tZBXKPmZOx4COUAVHV1e1wysejirwXCS0Oj614RVmER2k5NChLZ7Rf2U5L8ti/9nuV40Siee8bxSOZndy6l6vJJQRTxlSocfxEw6nvWhdF9xPZUqQi7phNizqbRaZ2Vcvx3caTMdJYHA2NKGMRJAX8SSrigZpcCWrJSdd1TqJPw8h5X9UOz Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=garrigue@math.nagoya-u.ac.jp; spf=None smtp.mailfrom=garrigue@math.nagoya-u.ac.jp; spf=None smtp.helo=postmaster@mailhost.math.nagoya-u.ac.jp Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of garrigue@math.nagoya-u.ac.jp) identity=pra; client-ip=133.6.130.5; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="garrigue@math.nagoya-u.ac.jp"; x-sender="garrigue@math.nagoya-u.ac.jp"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of garrigue@math.nagoya-u.ac.jp) identity=mailfrom; client-ip=133.6.130.5; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="garrigue@math.nagoya-u.ac.jp"; x-sender="garrigue@math.nagoya-u.ac.jp"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mailhost.math.nagoya-u.ac.jp) identity=helo; client-ip=133.6.130.5; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="garrigue@math.nagoya-u.ac.jp"; x-sender="postmaster@mailhost.math.nagoya-u.ac.jp"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0CbAAAZSvFVlwWCBoVdg3dpvyaGAQKBehEBAQEBAQEBARABAQEBAQgWB0+CHYIHAQEEJxMGAwE1AQEOCxgcElcGiEC2GYVcAosJhCUBAQEBAQEBAwEBAQEBAQEBEwEGiQKCbIRZMweDGIEUjXCHa4UKh3CCEphmN4Q7YohJAQEB X-IPAS-Result: A0CbAAAZSvFVlwWCBoVdg3dpvyaGAQKBehEBAQEBAQEBARABAQEBAQgWB0+CHYIHAQEEJxMGAwE1AQEOCxgcElcGiEC2GYVcAosJhCUBAQEBAQEBAwEBAQEBAQEBEwEGiQKCbIRZMweDGIEUjXCHa4UKh3CCEphmN4Q7YohJAQEB X-IronPort-AV: E=Sophos;i="5.17,503,1437429600"; d="scan'208";a="145330660" Received: from rabbit.math.nagoya-u.ac.jp (HELO mailhost.math.nagoya-u.ac.jp) ([133.6.130.5]) by mail3-smtp-sop.national.inria.fr with ESMTP; 10 Sep 2015 11:19:54 +0200 Received: from mailhost.math.nagoya-u.ac.jp (localhost [127.0.0.1]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTP id 91F506427; Thu, 10 Sep 2015 18:19:51 +0900 (JST) Received: from mailhost.math.nagoya-u.ac.jp (rabbit-172 [172.16.254.254]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTP id EAA8E4114; Thu, 10 Sep 2015 18:19:50 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=math.nagoya-u.ac.jp; h= subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; s=alpha; bh=3xP+w4KD7RFk8BP5WYCwkbXgeDU=; b=oOnNCEUhUDe01Q2OPGsy1Hv9tqq+ rSpSa9F4WtPDOKQcfhNuUBJ2wznaxJpKViKqBPJdCJuiNQCFyclbbpHuA95Iyp67 eHiMCnZL5URGXtyVRJvL/EzTfGuZj/Ec37DKKj6TxeuCEcVCtfwFix3LuirwKny/ 75CCAd4bJZiLecQ= DomainKey-Signature: a=rsa-sha1; h=Received:Subject:Mime-Version:Content-Type:From:In-Reply-To:Date:Cc:Content-Transfer-Encoding:Message-Id:References:To:X-Mailer; b=ziiFyZeQy1LC8VbQ2HuNrd07DuJdRdyBx3WDcR/dTLkR3/0ENuYOyTm25wHxJVmfWmtz9xQv37EZmmhSiDGOB0ck124v4UeTncbpKyxzPRC2F3vi7Xd+ru4g00HffM4mnBupw3AZmiQLtDe/1KeEtOyn5XQhypcs0+1dBg0KuZ0=; c=nofws; d=math.nagoya-u.ac.jp; q=dns; s=alpha Received: from tet.garrigue.jp (58x158x128x157.ap58.ftth.ucom.ne.jp [58.158.128.157]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTPSA id 6D6368A72; Thu, 10 Sep 2015 18:19:29 +0900 (JST) Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) Content-Type: text/plain; charset=us-ascii From: Jacques Garrigue In-Reply-To: Date: Thu, 10 Sep 2015 18:19:49 +0900 Cc: OCaML List Mailing Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Martin DeMello X-Mailer: Apple Mail (2.2104) Subject: Re: [Caml-list] using a restricted subset of a polymorphic variant On 2015/09/10 18:07, Martin DeMello wrote: >=20 > I'm not sure exactly why this doesn't work: >=20 > type dir =3D [`North | `South | `East | `West | `Up | `Down] >=20 > let index =3D function > | `North -> 1 > | `South -> 2 > | `East -> 3 > | `West -> 4 > | `Up -> 5 > | `Down -> 6 >=20 > class foo =3D object(self) > val mutable heading =3D `Up >=20 > method flip_heading =3D > heading <- match heading with `Up -> `Down | `Down -> `Up >=20 > method get_direction (x : dir) =3D index x >=20 > method get_heading =3D self#get_direction heading > end >=20 > 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 >=20 > but why is that an error? I'd think that any function that accepts type d= ir should accept type [`Down | `Up] as well. Also, how do I get this to wor= k? The problem is that OCaml type inference uses unification rather than subty= ping. So [`Down | `Up] and dir are incompatible. All the answers up to now seem to have assumed that get_direction should on= ly accept [`Up | `Down]. This indeed solves the problem, but may not be what you intended. If you still want get_direction to acception all directions, then you can e= ither modify get_direction or get_heading (but no need to change both): method get_direction : 'a. ([< dir] as 'a) -> int =3D index with this polymorphic method annotation, you can now apply get_direction to= heading method get_heading =3D self#get_direction (heading :> dir) this one uses subtyping to convert heading to dir. Jacques Garrigue