Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Variance constraint
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] Variance constraint
> I found the doc quite elusive on the subtle + and - notations
> for variance and contravariance constraints of abstract types.
> Can someone points out a good reference with examples that
> adresses this issue ? Maybe Jacques Garrigue ?

Sorry, there is nothing available currently.
You can find annotations in a few libraries: the Map module in the
standard library, the Raw module in LablGL, or the Gdk and Gtk modules
in LablGTK.
Basically, you normally only use annotations on abstract types (where
no definition is provided) to describe the expected behaviour of the
type with respect to subtyping.

For instance, an immutable container type (like lists) will have a
covariant type:
    type (+'a) container
meaning that if s is a subtype of t then s container is a subtype of t
container. On the other hand an acceptor will have a contravariant
type:
    type (-'a) acceptor
meaning that if s is a subtype of t then t acceptor is a subtype s
acceptor.
A type with no annotation is kept nonvariant, but there is no way to
express that a type is both covariant of contravariant (i.e. that both
subtypes and supertypes are ok): I don't really see any use for it...

For concrete or abbreviation types, variance is automatically
inferred, and variance annotations are only used to check that the
type really has the expected variance. In particular they do not
effectively restrict a type's variance.
Example
  type (+'a) key = int
will be accepted, but key will actually be both covariant and
contraviant in 'a (which does not appear in the body).

Hope this helps. Should I add this to the manual ?

     Jacques Garrigue
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr