[Haskell-beginners] Type classes are not like interfaces, after all
Francesco Bochicchio
bieffe62 at gmail.com
Fri Jan 23 05:34:49 EST 2009
Hi all.
It's about a month I'm trying to learn haskell in my spare time ( and, I
should add, with my spare neuron :-).
I made progress, but more slowly than I expected :-(. I started hasking
question on comp.lang.haskell
(I like newsgroups more than mailing lists), but then realized that this may
be a better places for my newbie
questions. So, here comes the first ...
As many beginners coming from OOP, i made the mental equation 'type class
== interface'.
It worked well ... unitil yesterday.
Then I discovered that this piece of code (1) is illegal in askell (ghc
gives the 'rigid type variable' error)
Num n => a :: n
a = 3 :: Integer
I also discovered that this (2) instead is legal:
Num n => a :: n
a = 3
because it is implicitely translated into (3):
Num n => a :: n
a = fromInteger 3
But I don't understand the difference between (1) and (3). I could not find
the implementation of
fromInteger, but I suspect that it goes like this:
instance Num Integer where
fromInteger x -> x
To me, it looks like (1) and (3) are equal, but that the compiler needs a
sort of 'formal reassurance' that
the value yielded by 'a' is of Num type. But since Integer is an instance of
Num, I expected that
(1) was already giving this reassurance. At least, it works this way with
interfaces in Java and with class pointers in C++.
But obviously there is something I don't get here ...
Anybody here can explain?
Ciao
-----
FB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090123/fea27376/attachment.htm
More information about the Beginners
mailing list