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 BDF287EEB4 for ; Fri, 1 Feb 2013 21:28:02 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of agarwal1975@gmail.com) identity=pra; client-ip=209.85.223.175; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="agarwal1975@gmail.com"; x-sender="agarwal1975@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of agarwal1975@gmail.com designates 209.85.223.175 as permitted sender) identity=mailfrom; client-ip=209.85.223.175; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="agarwal1975@gmail.com"; x-sender="agarwal1975@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-ie0-f175.google.com) identity=helo; client-ip=209.85.223.175; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="agarwal1975@gmail.com"; x-sender="postmaster@mail-ie0-f175.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApsBAJkkDFHRVd+vmGdsb2JhbABFtg4BiRgIFg4BAQEBAQgJDQcUJ4JMGQEbHgMSCQddAREBBQEWDIgRAQMPDKFJgnCMNIJ7hGMKGScNWYh3AQUMkUIDiGaNMYEdjVIWKYQ8 X-IPAS-Result: ApsBAJkkDFHRVd+vmGdsb2JhbABFtg4BiRgIFg4BAQEBAQgJDQcUJ4JMGQEbHgMSCQddAREBBQEWDIgRAQMPDKFJgnCMNIJ7hGMKGScNWYh3AQUMkUIDiGaNMYEdjVIWKYQ8 X-IronPort-AV: E=Sophos;i="4.84,579,1355094000"; d="scan'208";a="1053738" Received: from mail-ie0-f175.google.com ([209.85.223.175]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 01 Feb 2013 21:28:02 +0100 Received: by mail-ie0-f175.google.com with SMTP id c12so3879025ieb.34 for ; Fri, 01 Feb 2013 12:28:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:from:date:message-id:subject:to :content-type; bh=L6v0cWEjzSw20/BtOQkkEiHlNO8QU0eZv0V/bMQd55g=; b=aekBG8lQ+Oy30q1uZKrgY+sElwt0pixxR52Vg6Al6qulCzejgOoxB0xvcPB5z6Pfue PS29DhERTOKhDA/xpISPFsV86kcoXlftCLcI27D6w1MKECUK/GSIIeSgQHdAjNKsOYKB gSctpM8zncsFNQOnLOooqQWWaqMa2tdi6ZZHf63RSCkulZJFdMdknUlLxElDHlNbCcS3 DbzcAT8vlwFRYJ8MfRrICO1D7ZH3jjNIF+OLgfa446hvqIHkdmOZw/2d8xp03IPkzdmO nKGruSwfOuytGKt4LqdkrkPaTcebwa+U5mYIzk9QxemqtZWMvJNbAaJMiri9RgIA6qsS ndLA== X-Received: by 10.50.151.225 with SMTP id ut1mr2246920igb.110.1359750481059; Fri, 01 Feb 2013 12:28:01 -0800 (PST) MIME-Version: 1.0 Received: by 10.64.47.229 with HTTP; Fri, 1 Feb 2013 12:27:40 -0800 (PST) From: Ashish Agarwal Date: Fri, 1 Feb 2013 15:27:40 -0500 Message-ID: To: Caml List Content-Type: multipart/alternative; boundary=e89a8f3b9fc9681fd704d4af9446 Subject: [Caml-list] deep coercion does not work for some stdlib types --e89a8f3b9fc9681fd704d4af9446 Content-Type: text/plain; charset=ISO-8859-1 Section 7.9.2 of the manual gives this example: http://caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc76 # module N : sig type t = private int val of_int: int -> t val to_int: t -> int end = struct type t = int let of_int n = assert (n >= 0); n let to_int n = n end;; module N : sig type t = private int val of_int : int -> t val to_int : t -> int end Deep coercion to a list of integers works as expected: # let l = List.map N.of_int [1;2;3];; val l : N.t list = [1; 2; 3] # (l :> int list);; - : int list = [1; 2; 3] But for arrays it doesn't work: # let a = Array.of_list l;; val a : N.t array = [|1; 2; 3|] # (a :> int array);; Error: Type N.t array is not a subtype of int array Is this because the array type does not have a variance annotation? If so, why doesn't it? I can get around this by mapping over the elements: # Array.map (fun (x : N.t) -> (x :> int)) a;; But am I right that coercions have no run-time cost, as where the mapping will? --e89a8f3b9fc9681fd704d4af9446 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Section 7.9.2 of the manual gives this example:

# module N : sig
=A0 =A0 =A0type t =3D privat= e int
=A0 =A0 =A0val of_int: int -> t
=A0 =A0 =A0val= to_int: t -> int
=A0 =A0end
=A0 =3D struct
=A0 =A0 =A0 =A0type t =3D int
=A0 =A0 =A0 =A0let of_int n =3D assert (n >=3D 0); n
=A0 = =A0 =A0 =A0let to_int n =3D n
=A0 =A0 end;;

<= div>module N : =A0sig type t =3D private int val of_int : int -> t val t= o_int : t -> int end

Deep coercion to a list of integers works as expe= cted:

# let l =3D List.map N.of_int [1;2= ;3];;
val l : N.t list =3D [1; 2; 3]

# (l :> int list);;
- : int list =3D [1; 2; 3]

But for arrays it doesn't work:

# let a =3D Array.of_list l;;
val a : N.t array =3D [|1; 2= ; 3|]

# (a :> int array);;
Error: Type N.t array= is not a subtype of int array

Is this becau= se the array type does not have a variance annotation? If so, why doesn'= ;t it?

I can get around this by mapping over the elements:

# Array.map (fun (x : N.t) -> (x :> int)) a;;

But am I right that coercions have no run-time cost= , as where the mapping will?

--e89a8f3b9fc9681fd704d4af9446--