caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Lukasz Stafiniak <lukstafi@gmail.com>
Cc: Ashish Agarwal <agarwal1975@gmail.com>, Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] deep coercion does not work for some stdlib types
Date: Sat, 2 Feb 2013 10:33:25 +0900	[thread overview]
Message-ID: <0D592117-6CEC-4B95-BEBA-9ECE532C2CCB@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CAJMfKEWYhH8LRC_RFvai1qqA2k-=mx4w+jAohXovtvzi66gN0Q@mail.gmail.com>

On 2013/02/02, at 5:39, Lukasz Stafiniak <lukstafi@gmail.com> wrote:

> On Fri, Feb 1, 2013 at 9:27 PM, Ashish Agarwal <agarwal1975@gmail.com> wrote:
>> 
>> 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?
> 
> 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 = (a :> X.t array)", you can modify "a.(0) <- y"
> with a value "y" that is not "X.t".
> 
> 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 wrapping
your array in an object or record:

class [+'a] vector (arr : 'a array) = object
  val arr = arr
  method length = Array.length arr
  method get = Array.get arr
  method iter f = Array.iter f arr
end
type p = private int
let coerce x = (x : p vector :> int vector)

or

type +'a vector = {length: int; get: int -> 'a; iter: ('a -> unit) -> unit}
let vector arr = Array.({length=length arr; get=get arr; iter = fun f -> iter f arr})
let coerce x = (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

      parent reply	other threads:[~2013-02-02  1:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-01 20:27 Ashish Agarwal
2013-02-01 20:39 ` Lukasz Stafiniak
2013-02-01 20:44   ` Lukasz Stafiniak
2013-02-01 23:37   ` Ashish Agarwal
2013-02-02  1:33   ` Jacques Garrigue [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0D592117-6CEC-4B95-BEBA-9ECE532C2CCB@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=agarwal1975@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=lukstafi@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).