caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Andreas Rossberg" <AndreasRossberg@web.de>
To: "Clemens Hintze" <c.hintze@gmx.net>, <caml-list@inria.fr>
Subject: Re: [Caml-list] [Q]: Co(ntra)variance and subtyping?
Date: Sun, 18 Nov 2001 14:34:41 +0100	[thread overview]
Message-ID: <002001c17035$c722aa80$3363e195@pazo> (raw)
In-Reply-To: <20011116203745.A59514@qiao.in-berlin.de>

> - What does subtyping exactly mean in OCaml resp. functional
>   programming?

OCaml has two separate notions of subtyping:

- in the core language, to support objects
- in the module language, for signature matching

While subtyping for module types is rather standard from a superficial point
of view, OCaml's treatment of subtyping in the core language is somewhat
special because it does not provide so-called subsumption. Subsumption means
that wherever a value of type t is expected you may freely provide a value
of some subtype t'. This is not the case in OCaml: you have to explicitly
coerce the value to the required supertype first. This is necessary to make
type inference feasible.

Note however that OCaml's object system rarely requires the use of
subtyping, because it mainly relies on row polymorphism, which is more or
less superior (although it admittedly complicates things to have both).

> - What means covariance and contravariance of types and subtypes?

Variance is a property of type constructor arguments. Take the "list" type
constructor for example: it is said to be covariant in its argument because
it comes with the following subtyping rule:

    t' < t     =>     t' list < t list

where "<" stands for "is subtype of". This means that you can use a t' list
as a t list - the list type "varies" in the same direction as its argument,
thus the term "covariance".

The most important type constructor is "->", which has the following
subtyping rule:

    t' < t and u' < u    =>    (t -> u') < (t' -> u)

Note the order of t and t' here: it goes in the opposite direction!
Functions are covariant in their result type, but contravariant in their
argument type. This may seem a bit funny at first (and in fact there are
major programming languages that still insist on getting it plainly wrong -
you already know one ;-) but it is the only sound rule. Intuitively it means
that a function type is a subtype of another function type if it produces a
more specific result while putting less requirements on its argument.

In general, you have covariance for type components that are "produced" or
"readable", and contravariance for components that are "consumed" or
"writable". Another example of contravariance would be a polymorphic output
channel.

Of course, you cannot have both co- and contravariance for the same
argument. So if some type component is both readable and writable then it
has to be invariant. Consider arrays for example: there is no subtyping
relation between any two array types t array and u array, no matter what
relation t and u are in (with the only exception of t=u, then of course you
trivially have `subtyping' in either direction). In fact, mutable types
always have to be invariant (another thing that many popular OO languages do
wrong - some at least do runtime checks for compensation, some just crash).

> OTOH, these terms seem to be very important in dealing with OCaml or
> functional programming.

I hope my explanations clarified that these terms are mainly important if
you do OO in the presence of polymorphic types. Functional programming in
its purer forms usually does not use nor require subtyping, so need not
bother with them ;-)

Cheers,

    - Andreas


-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


  parent reply	other threads:[~2001-11-18 13:33 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-16 19:37 Clemens Hintze
2001-11-17 14:18 ` Mark Wotton
2001-11-17 14:55   ` Mark Wotton
2001-11-17 17:50   ` [Caml-list] " Clemens Hintze
2001-11-17 23:17     ` Mark Wotton
2001-11-18  9:16       ` Clemens Hintze
2001-11-18 13:18         ` Alain Frisch
2001-11-19  9:54           ` Remi VANICAT
     [not found]       ` <9t7v4d$gij$1@qrnik.zagroda>
2001-11-18 11:57         ` Marcin 'Qrczak' Kowalczyk
2001-11-18 13:34 ` Andreas Rossberg [this message]
2001-11-18 21:22   ` [Caml-list] " Pixel
2001-11-19  0:33     ` Jacques Garrigue
2001-11-18 22:35       ` David Gurr
2001-11-19  7:24         ` [Caml-list] " Clemens Hintze
2001-11-19 12:03           ` Markus Mottl
2001-11-19  8:29         ` [Caml-list] " Xavier Leroy
2001-11-19 11:03       ` Alain Frisch
2001-11-20  9:58         ` Didier Remy
2001-11-19 11:14       ` Pixel
2001-11-18 22:30   ` [Caml-list] Re: variance, subtyping and monads... oh, my! james woodyatt
2001-11-19  8:11     ` Francois Pottier
2001-11-19  9:02       ` james woodyatt
2001-11-19  9:58         ` Markus Mottl
2001-11-19 20:47           ` james woodyatt
2001-11-19 12:56       ` Frank Atanassow
2001-11-19 10:39     ` Andreas Rossberg
2001-11-19 12:21       ` Markus Mottl
2001-11-19 13:43         ` [Caml-list] Kylix and OCaml Christophe Raffalli
2001-11-20  2:05           ` Vitaly Lugovsky
2001-11-20  8:51             ` Christophe Raffalli
2001-11-22  1:42               ` Vitaly Lugovsky
2001-11-20 10:00             ` Benjamin Monate
2001-11-20 10:24               ` [Caml-list] [Bug in an interface between C++ and OCAML due to some pointer encapsulation] Sylvain Kerjean
2001-11-20 12:14             ` [Caml-list] Kylix and OCaml Maxence Guesdon

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='002001c17035$c722aa80$3363e195@pazo' \
    --to=andreasrossberg@web.de \
    --cc=c.hintze@gmx.net \
    --cc=caml-list@inria.fr \
    --cc=rossberg@ps.uni-sb.de \
    /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).