caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Didier Remy <remy@morgon.inria.fr>
To: Hendrik Tews <tews@tcs.inf.tu-dresden.de>
Cc: caml-list@inria.fr
Subject: Re: co(ntra)-variant subtyping
Date: Tue, 26 May 1998 11:24:35 +0200	[thread overview]
Message-ID: <19980526112435.51973@morgon.inria.fr> (raw)
In-Reply-To: <199805251146.NAA21664@ithif18.inf.tu-dresden.de>; from Hendrik Tews on Mon, May 25, 1998 at 01:46:19PM +0200

>    Objective Caml's subtyping algorithm does not subtype under type
>    constructors (but it does it under abbreviations), for the sake of
>    efficiency. That is, eventhough colored_point :> point, you don't have
>    colored_point option :> point option. This equation would be needed to
>    check the subtyping in method move of example 2.
>    
> I must say I am really surprised.  
> 
> How about object types? Do they support co(ntra)-variant
> subtype rules?

As Jacques said, subtyping sees through abbreviations.
Object types are abbreviations. 
They are covariant except when self-type occurs negatively, in
which case they are non-variant.

> I would suggest to include a section about the subtype relation
> in the ocaml documentation. 

We are aware that the documentation is too succinct. 
We will keep improving it. 

> Because IMHO anybody how knows
> something about formal models for oo languages would expect
> co(ntra)-variant subtype rules. 

I leave you this opinion.

Traditionally, people have heavily relied on subtyping polymorphism; they
run into serious difficulties because of the bad interaction between
recursion, late-binding and contra-variance. This lead to some mistakes
(some languages got it wrong). Moreover they still have difficulties with
binary methods.  For instance, even though Java is explicitly typed, it
fails to type binary methods: the type of self is weakened to the current
type of the class, which forces later useless and unsafe coercions (they may
fail, dynamically).

Instead, Objective Caml relies on parametric polymorphism.  This is easier to
explain, often more powerful (here, binary methods are not a problem) and
allows simple type inference. This is really what makes Objective Caml work.
Actually, Objective Caml does work with no subtyping at all, i.e.  polymorphic
message invocation, inheritance, binary methods, etc. all work without
subtyping.  Indeed, many examples do not use subtyping at all.

Still, Objective Caml allows subtyping because they are a few situations
when it is  convenient. Typically, when storing a collection of
objects of different subclasses of a common parent class into a bag.  Then,
only the operations of the parent class can be directly invoked on the
objects of the collection. (Actually, subtyping is independent from the
class hierarchy and objects do not need to be in an inheritance relation to
be put in the same collection.)

> In summary I think the lack of these subtype rules are a major
> restriction of the ocaml language. 

I am not convinced that the current restriction is such a burden. 
You are one of the first person to complain...

> Now it is not possible to mix
> class types and (algebraic) data types in an application. Instead
> a user has to decide beforehand what is more important for him:
> algebraic data types and parametric polymorphism or object types
> with code reuse and stepwise refinement.

Maybe you could detail your examples.

> Are there any plans to add the missing subtype rules to the type
> system of ocaml?

In fact, there is no real difficulty to allow subtyping through
user-declared type constructors.  However, when types are abstract (e.g. in
module interfaces) the user would need to declare the variances of the
constructors in their arguments.

We did not want to add yet another notion in the language, and therefore 
we prefered to make all non-primitive type constructors non variant.

   Didier





  reply	other threads:[~1998-05-27  9:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-05-20 15:00 Subtype problem Hendrik Tews
1998-05-22  2:21 ` Jacques GARRIGUE
1998-05-25 11:46   ` co(ntra)-variant subtyping Hendrik Tews
1998-05-26  9:24     ` Didier Remy [this message]
1998-06-04 15:17       ` Hendrik Tews
1998-06-04 18:29         ` Didier Remy
1998-06-05  5:45         ` Jacques GARRIGUE

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=19980526112435.51973@morgon.inria.fr \
    --to=remy@morgon.inria.fr \
    --cc=Didier.Remy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=tews@tcs.inf.tu-dresden.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).