caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Jeff Meister <nanaki@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Use-site variance in OCaml
Date: Sat, 8 Jun 2013 16:05:40 +0900	[thread overview]
Message-ID: <69F3B820-75C0-4EA1-B1AF-B70D276E4AEE@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CAHaHOqRcumt57p6w=-hBH3yzsQ5tJksrZRis72hDgNDsYASWXA@mail.gmail.com>

On 2013/06/08, at 14:37, Jeff Meister <nanaki@gmail.com> wrote:

> I don't often use subtyping in OCaml, but I was recently reading a paper about a mixture of declaration-site and use-site variance annotations in a Java-like language (http://www.cs.cornell.edu/~ross/publications/mixedsite/mixed-site-tate-2013.pdf), and it occurred to me that OCaml might be able to express the same concepts.
> 
> If I have a type parameter that appears in both covariant and contravariant positions, then it is invariant, and OCaml will not allow me to annotate the parameter with + or -. For example, this class of mutable references:
> 
> class ['a] rw (init : 'a) = object
>   val mutable v = init
>   method get = v
>   method set x = v <- x
> end
> 
> This code will not compile with [+'a] or [-'a]. However, the class can be (manually) decomposed into separate covariant and contravariant parts:
> 
> class [+'a] ro (init : 'a) = object val mutable v = init method get = v end
> class [-'a] wo (init : 'a) = object val mutable v = init method set x = v <- x end
> class ['a] rw init = object inherit ['a] ro init inherit! ['a] wo init end

[…]

> So, it seems that the OCaml type checker knows which methods of my first definition of 'a rw use 'a in a covariant or contravariant position. Would it be possible to implement use-site variance in OCaml based on this?
> 
> I'm imagining an expression like (new rw 0 :> +'a rw) that would give an object of type 'a ro, without the programmer having to declare any such type, or decompose his class manually. OCaml would automatically select only those methods where 'a appears only in covariant positions. Similarly, -'a rw would produce an object of type 'a wo.
> 
> Is this feasible, or is the situation more complicated than I've described?

This is technically doable.
Maybe the most painful part would be to extend the syntax to allow variance annotations inside type expressions.
Also, OCaml and Java differ in that OCaml allows binary methods, which being contravariant in the type of self
introduce some ambiguity in the meaning of "+":
   class type ['a] cell = object ('self) inherit ['a] rw method eq : 'self -> bool end
In that case,  +'a cell could either keep only method get, or only eq, but keeping both would be invariant.

And as always the question is rather how useful it would be in practice.

Also, an intermediate form seems possible too: rather than doing this on the fly inside coercions,
one could use it in declarations:

  class type ['a] ro = [+'a] rw

or

  class type ['a] c = object
     inherit [+'a] rw
     method set x = {< v = x >}
  end

The change in syntax is much more modest: we just allow variance annotations in the bracket, and we are sure that rw must be a class type.

Jacques Garrigue

  reply	other threads:[~2013-06-08  7:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-08  5:37 Jeff Meister
2013-06-08  7:05 ` Jacques Garrigue [this message]
2013-06-11 20:58   ` Jeff Meister
2013-06-12  2:36     ` Jacques Garrigue

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=69F3B820-75C0-4EA1-B1AF-B70D276E4AEE@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=nanaki@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).