This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[Caml-list] [Q]: Co(ntra)variance and subtyping?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2001-11-19 (11:17) From: Pixel Subject: Re: [Caml-list] [Q]: Co(ntra)variance and subtyping?
Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp> writes:

[...]

> > 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
>
> This, again, is row polymorphism, not subsumption.
> The difference between (row/parametric) polymorphism and subsumption
> is that for the former you must explicitely leave some free variable
> in the type (the presence of which is proved by the "as 'a" or "_"),
> while the latter also works on ground types (without variables).

# let switch = function `On -> `Off | `Off -> `On;;
val switch : [< `On | `Off] -> [> `Off | `On] = <fun>
# `On;;
- : [> `On] = `On
# switch `On;;
- : _[> `Off | `On] = `Off

I still call this subsumption.

> Both polymorphic variants and objects uss both polymorphism and
> subtyping, but without implicit subsumption.

The subsumption is not implicit is the type of "switch", but it is implicit in
the use of the function. It is also implicit in the definition of "switch".
That's why for the programmer, it is subsumption.
-------------------
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