[Haskell-beginners] Type classes are not like interfaces, after all

Brent Yorgey byorgey at seas.upenn.edu
Fri Jan 23 09:59:49 EST 2009

> AbstractInterface a = new ConcreteClass();

In Java, if a variable has type AbstractInterface, it is
*existentially* quantified: it means, this variable references a value
of *some* type, and all you know about it is that it is an instance of
AbstractInterface.  Whatever code sets the value of the variable gets
to choose its concrete type; any code that uses the variable cannot
choose what type it should be, but can only use AbstractInterface
methods on it (since that's all that is known).

However, a Haskell variable with type

  var :: AbstractInterface a => a

is *universally* quantified: it means, this variable has a polymorphic
value, which can be used as a value of any type which is an instance
of AbstractInterface.  Here, it is the code which *uses* var that gets
to choose its type (and indeed, different choices can be made for
different uses); the definition of var cannot choose, and must work
for any type which is an instance of AbstractInterface (hence it must
only use methods of the AbstractInterface type class).


a :: Num n => n
a = 3 :: Integer

is trying to define a universally quantified value as if it were
existentially quantified.

Now, it *is* possible to have existentially quantification in Haskell;
I can show you how if you like, but I think I'll stop here for now.

Does this help?


More information about the Beginners mailing list