Browse thread
[Caml-list] [Q]: Co(ntra)variance and subtyping?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Pixel <pixel@m...> |
| Subject: | Re: [Caml-list] [Q]: Co(ntra)variance and subtyping? |
"Andreas Rossberg" <AndreasRossberg@web.de> 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 = <fun> # (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