From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA24777; Sun, 18 Nov 2001 22:25:57 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA25036 for ; Sun, 18 Nov 2001 22:25:56 +0100 (MET) Received: from leia.mandrakesoft.com (office.mandrakesoft.com [195.68.114.34]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id fAILPu903866 for ; Sun, 18 Nov 2001 22:25:56 +0100 (MET) Received: by leia.mandrakesoft.com (Postfix, from userid 505) id D19D3578A; Sun, 18 Nov 2001 22:22:56 +0100 (CET) To: "Andreas Rossberg" Cc: "Clemens Hintze" , Subject: Re: [Caml-list] [Q]: Co(ntra)variance and subtyping? References: <20011116203745.A59514@qiao.in-berlin.de> <002001c17035$c722aa80$3363e195@pazo> From: Pixel Date: 18 Nov 2001 22:22:56 +0100 In-Reply-To: <002001c17035$c722aa80$3363e195@pazo> Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk "Andreas Rossberg" writes: > > - 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. well, you forgot polymorphic variants, which do use subsumption: # let switch = function `On -> `Off | x -> x;; val switch : ([> `Off | `On] as 'a) -> 'a = # (switch `On, switch `Whatever);; - : _[> `Off | `On] * _[> `Off | `On | `Whatever] = `Off, `Whatever > 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. necessary is a small overstatement ;p [...] > 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 ;-) if you want more about it, read the nice paper from Giuseppe Castagna: Covariance And Contravariance: conflict Without A Cause http://citeseer.nj.nec.com/castagna95covariance.html ------------------- 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