[Haskell-beginners] Type classes are not like interfaces, after
all
Jan Jakubuv
jakubuv at gmail.com
Fri Jan 23 10:41:18 EST 2009
2009/1/23 Francesco Bochicchio <bieffe62 at gmail.com>:
>
>
> 2009/1/23 Jan Jakubuv <jakubuv at gmail.com>
>>
>> hi,
>>
>> 2009/1/23 Francesco Bochicchio <bieffe62 at gmail.com>:
>> >
>> The problem whith your implementation of 'a'
>>
>> a = 3 :: Integer
>>
>> is that it provides too specific result. Its type signature says that
>> its result has to be of the type n for *any* instance of the class
>> Num. But your result is simply Integer that is just *one* specific
>> instance of Num. In other words it has to be possible to specialize
>> ("retype") 'a' to any other instance of Num, which is no longer
>> possible because (3 :: Integer) is already specialized.
>
>
> Uhm. Now I think I start to get it ...
> You are saying that if a value is a Num, it shall be possible to convert it
> in _any_ of the num instances?
>
Well, in this case of the above constant yes. The main point here is
that when you want to implement a function of a type say (Num a => a
-> a) then the implementation has to work for *all* instances of the
class Num. Usually you can use only "abstract" functions defined in a
class declaration to write such functions.
Try to start with some function that mentions the quantified type in
one of its arguments. They are easier to understand. Constants like
(Num a => a) and functions like (Num a => Bool -> a) are rare (also
they have a special name I can not recall ;-).
Also note that you can take:
a :: (Num t) => t
a = 3
and then specialize it:
spec = (a :: Integer)
Sincerely,
jan.
More information about the Beginners
mailing list