Haskell Typeclass Type Constraints and Deduction
I've been running through the "Learn You a Haskell" book, and I'm trying
to wrap my head around Haskell Type Classes. As practice, I'm trying to
create a simple vector type class. The following snippet of code has been
giving me some grief (resulting in my first post to StackOverflow):
data Vec2 a = Vec2 (a,a) deriving (Show, Eq, Read)
class Vector a where
(<*) :: (Num b) => a -> b -> a
instance (Num a) => Vector (Vec2 a) where
Vec2 (x,y) <* a = Vec2 (a*x, a*y)
I get the following error message:
Could not deduce (a~b) from the context (Num a) or from (Num b) bound by
the type signature for
<* :: Num b => Vec2 a -> b -> Vec2 a
It seems like the Num being specified in the typeclass should supply the
type of a, and Num a spefication in the instance should supply the type of
x and y, so why is it complaining? What misconception do I have about this
code?
No comments:
Post a Comment