Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics and Constrained Polymorphism versus Subtyping

In this PDF presentation on Haskell Type Classes, slide #54 has this question:

Open Question:

In a language with generics and constrained polymorphism, do you need subtyping too?

My questions are:

  1. How do generics and constrained polymorphism make subtyping unnecessary?

  2. If generics and constrained polymorphism make subtyping unnecessary, why does Scala have subtyping?

like image 633
missingfaktor Avatar asked Apr 25 '10 04:04

missingfaktor


People also ask

Is subtyping polymorphism?

Subtyping is therefore a form of type polymorphism. In object-oriented programming the term 'polymorphism' is commonly used to refer solely to this subtype polymorphism, while the techniques of parametric polymorphism would be considered generic programming.

Is generic programming polymorphism?

Using parametric polymorphism, a function or a data type can be written generically so that it can handle values identically without depending on their type. Such functions and data types are called generic functions and generic datatypes respectively and form the basis of generic programming.

What is the purpose of subtype polymorphism?

Subtyping describes type relationships, and subtype polymorphism enables operations defined for supertypes to be safely substituted with subtypes.

What is subtyping in OOP?

Subtyping is a method for substitution and code reuse used in object-oriented programming languages to prevent unnecessary copying of largely similar code and promote code readability and prevent bugs.


2 Answers

How do generics and constrained polymorphism make subtyping unnecessary?

It is not known that they do. If you put the slide in context, I think that the argument the speaker was trying to make goes something like this:

  • In the old days, subtyping provided an important kind of polymorphism.

  • Also in the old days, in another country, type abstraction and type parameters provided an important kind of polymorphism. This kind is known in its native land as parametric polymorphism, but in foreign lands it is called generics.

  • Modern generics admit of constraints, sometimes called "bounded polymorphism", which can achieve many of the same things as subtype polymorphism.

  • Subtyping carries with it substantial baggage—in particular, you have to worry about covariance and contravariance. Languages wind up with uncomfortable restrictions, heavyweight notations, and sometimes outright safety violations (e.g., Eiffel).

The open question: perhaps constrained parametric polymorphic solves enough of the same problems that in the happy future, we can get rid of subtype polymorphism entirely, and with it the nasty question of when subtyping is covariant, contravariant, and invariant.

like image 76
Norman Ramsey Avatar answered Oct 08 '22 11:10

Norman Ramsey


Well if that is indeed an open question, then by definition we don't know the answer to #1. The design spaces are pretty different, and it isn't obvious to me how you might directly encode subtyping into constrained polymorphism. The encoding is direct when the arguments are polymorphic. For example, a Haskell function with type

foo :: (Num a) => a -> Bool

is equivalent to, say:

Bool foo(Num x)

in an OO language. However it is not clear how to encode:

// I will return some Num, but I'm not going to tell you what kind exactly
Num bar(Bool x) 

into constrained polymorphism, nor is it clear how to encode:

-- I can return any kind of Num, *you* tell *me* what kind
bar :: (Num a) => Bool -> a 

into subtyping.

My best guess for #2 is that Scala has to talk to Java, and Java talks about subtyping. And because Scala has every type system feature known to man because it thinks it has to in order to be cool. :-P

like image 41
luqui Avatar answered Oct 08 '22 12:10

luqui