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

Francesco Bochicchio bieffe62 at gmail.com
Fri Jan 23 11:46:11 EST 2009


2009/1/23 Brent Yorgey <byorgey at seas.upenn.edu>

> >
> > 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).
>
> Writing
>
> 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?
>
>
Yes thanks.
>From other answers, I also got the essence of it, but now I know the exact
terminology: I met the terms
'existentially quantification' and 'universal quantification'  before, but
did not have any idea of their meaning.
I did not now than embarking in studying haskell I had to study phylosophy
too :-)

Joking apart, I believe something like your explanation should be reported
in tutorials helping to learn haskell, especially the
ones for people coming from other languages. As I said in my initial post
the analogy 'type class are like interfaces' is a good
starting point, but carried too far it is misleading.

Thanks again to all the posters that replied to my question. This seems a
lively forum: expect to see soon other questions from me :-).


Ciao
------
FB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090123/b196a1e6/attachment.htm


More information about the Beginners mailing list