[Haskell-beginners] Type classes are not like interfaces, after
all
Jan Jakubuv
jakubuv at gmail.com
Fri Jan 23 06:15:08 EST 2009
hi,
2009/1/23 Francesco Bochicchio <bieffe62 at gmail.com>:
>
> 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 guess you mean:
a :: Num n => n
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.
> I also discovered that this (2) instead is legal:
>
> Num n => a :: n
> a = 3
>
It's fine because 3 has the type (Num t) => t::
Prelude> :t 3
3 :: (Num t) => t
> because it is implicitely translated into (3):
>
> Num n => a :: n
> a = fromInteger 3
>
also fine:
Prelude> :t fromInteger
fromInteger :: (Num a) => Integer -> a
Sincerely,
jan.
More information about the Beginners
mailing list