English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2001-03-20 (14:01)
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 (-'a) acceptor
meaning that if s is a subtype of t then t acceptor is a subtype s
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.
  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