[Haskell-beginners] Type classes are not like interfaces,
tom.davie at gmail.com
Fri Jan 23 08:50:47 EST 2009
On 23 Jan 2009, at 14:37, Francesco Bochicchio wrote:
> 2009/1/23 Paul Visschers <mail at paulvisschers.net>
> It seems like you have some trouble with grasping the concept of
> polymorphism in this particular case.
> I think I get the polymorphism. What I don't get is why a
> specialized type cannot
> replace a more generic type, since the specialized type implements
> the interface
> defined in the generic type.
Suppose I declare this constant:
x :: Num a => a
x = 3 :: Integer
Now suppose I want to use that in a function. It's type signature
says that x is *any* numeric type i want it to be, so I'm going to add
it to another numeric value:
y :: Complex Float
y = x + (5.3 :+ 6.93)
Unfortunately, x *can't* take the form of any Numeric type – it has to
be an Integer, so I can't add it do Complex Floating point numbers.
The type system is telling you "while Integers may imelement the
numeric interface, the value 3 :: Integer is not a generic value – it
can't take the form of *any* numeric value, only a specific type of
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners