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 853B37EEB4 for ; Sat, 2 Feb 2013 02:33:15 +0100 (CET) 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: Ap4EAFBsDFGFBoIF/2dsb2JhbABFv0pzgh4BAQQBJxwBNQIDCwsOChwSITYGE4d/AwkFryWEQQKFFg1MiQMHjBmEVGGIZ4tcgVeLPoUSgws X-IPAS-Result: Ap4EAFBsDFGFBoIF/2dsb2JhbABFv0pzgh4BAQQBJxwBNQIDCwsOChwSITYGE4d/AwkFryWEQQKFFg1MiQMHjBmEVGGIZ4tcgVeLPoUSgws X-IronPort-AV: E=Sophos;i="4.84,588,1355094000"; d="scan'208";a="820880" 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; 02 Feb 2013 02:30:31 +0100 Received: from mailhost.math.nagoya-u.ac.jp (localhost [127.0.0.1]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTP id 44E056382; Sat, 2 Feb 2013 10:33:09 +0900 (JST) Received: from mailhost.math.nagoya-u.ac.jp (localhost [127.0.0.1]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTP id 0C3D4252E; Sat, 2 Feb 2013 10:33:09 +0900 (JST) DomainKey-Signature: h=Received:Content-Type:Mime-Version:Subject:From:In-Reply-To:Date:Cc:Content-Transfer-Encoding:Message-Id:References:To:X-Mailer; b=; c=nofws; d=math.nagoya-u.ac.jp; q=; 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 A7BA1251F; Sat, 2 Feb 2013 10:33:08 +0900 (JST) Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) From: Jacques Garrigue In-Reply-To: Date: Sat, 2 Feb 2013 10:33:25 +0900 Cc: Ashish Agarwal , Caml List Content-Transfer-Encoding: quoted-printable Message-Id: <0D592117-6CEC-4B95-BEBA-9ECE532C2CCB@math.nagoya-u.ac.jp> References: To: Lukasz Stafiniak X-Mailer: Apple Mail (2.1499) Subject: Re: [Caml-list] deep coercion does not work for some stdlib types On 2013/02/02, at 5:39, Lukasz Stafiniak wrote: > On Fri, Feb 1, 2013 at 9:27 PM, Ashish Agarwal wr= ote: >>=20 >> But for arrays it doesn't work: >>=20 >> # let a =3D Array.of_list l;; >> val a : N.t array =3D [|1; 2; 3|] >>=20 >> # (a :> int array);; >> Error: Type N.t array is not a subtype of int array >>=20 >> Is this because the array type does not have a variance annotation? If s= o, >> why doesn't it? >=20 > You must have heard about the "mistake" of Go having covariant arrays. > In OCaml, arrays are invariant because they are mutable. After you > have coerced "let b =3D (a :> X.t array)", you can modify "a.(0) <- y" > with a value "y" that is not "X.t". >=20 > It would be great to have a separate type "tuple", supported by > Pervasives, that differs from "array" only in that it is immutable and > covariant. Well, if you do not care too much about performance, you can do it by wrapp= ing your array in an object or record: class [+'a] vector (arr : 'a array) =3D object val arr =3D arr method length =3D Array.length arr method get =3D Array.get arr method iter f =3D Array.iter f arr end type p =3D private int let coerce x =3D (x : p vector :> int vector) or type +'a vector =3D {length: int; get: int -> 'a; iter: ('a -> unit) -> uni= t} let vector arr =3D Array.({length=3Dlength arr; get=3Dget arr; iter =3D fun= f -> iter f arr}) let coerce x =3D (x : p vector :> int vector) Note that the object based approach is slightly weaker because, while vector is covariant, it is not strongly covariant, as needed for the relaxed value restriction. This can be fixed by making it abstract. Jacques Garrigue=