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

Paul Visschers mail at paulvisschers.net
Fri Jan 23 06:07:38 EST 2009


Hello,

It seems like you have some trouble with grasping the concept of
polymorphism in this particular case.

The function reverse has the following type:
reverse :: [a] -> [a]
This means that this function will work on a list containing any type,
and it will return that same type.

A sort function would have a type like this:
sort :: (Ord a) => [a] -> [a]
This function also doesn't care what type the list is filled with, but
does require this type to be an instance of the Ord class (so it can be
ordered).

Now an example with the Num class:
(+) :: (Num a) => a -> a -> a
This function works on any type which is an instance of Num, for example
Int and Integer.

The type of fromInteger is:
fromInteger :: (Num a) => Integer -> a
You are correct that literal numbers are internally rewritten, so 3
becomes fromInteger 3, giving the following type:
fromInteger 3 :: (Num a) => a
This means that fromInteger 3 is of type a, for any a that is an
instance of Num.

In your example (which seems to be in a bad syntax), you're saying that
a must be of type:
a :: (Num n) => n
So this is the same type as fromInteger 3. So just saying that
a = 3
or
a = fromInteger 3
will work here. The function a is polymorphic, just like sort or (+)
are. They (have to) work for any type that implements the corresponding
type class.

The problem comes from the extra type annotation:
a = 3 :: Integer
Which says that instead of being of any numeric type, a is of type
Integer. This is less general, since you can't use it when you need an
Int or a Double for example.

I hope I explained it clearly. If not please reply.

Paul

Francesco Bochicchio wrote:
> 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
>  
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



More information about the Beginners mailing list