4

A similar question has already answered here - Parametric polymorphism vs Ad-hoc polymorphism. The difference in this question is with the term bounded. It seems that there are two distinct flavors of ad-hoc polymorphism -

  1. For example, in C++ you can write

    int foo(int x) { return x; }
    int foo(string x) { return x.size(); }
    

    In this case, it doesn't make sense to speak of the type of foo as an overload set (in the sense that it is not a user-definable type, or something for which you can create an alias), but it does make sense to speak of the types of the individual overloads themselves.

  2. In Haskell, you can write

    class Foo a where
      foo :: a -> Int
    instance Foo Int where
      foo x = x
    instance Foo String where
      foo xs = length xs
    

    Here, it makes sense to speak about the type of foo by itself as a proper type Foo a => a -> Int which can be used like an ordinary user defined type.

Is it appropriate to say:

  1. C++'s flavor of ad-hoc polymorphism via function overloading cannot be classified as bounded parametric polymorphism.

  2. Haskell's flavor of ad-hoc polymorphism via type classes can be classified as bounded parametric polymorphism (as a different example, Pierce's Types and Programming Languages talks about System-F<: in a similar context).

Is bounded parametric polymorphism a strict "subset" of ad-hoc polymorphism, in the sense that any type system that can be said to have the first can also be said to have the second?

typesanitizer
  • 2,505
  • 1
  • 20
  • 44

0 Answers0