[Haskell-beginners] Typeclasses and generic types
Christopher Howard
christopher.howard at frigidcode.com
Tue May 10 05:25:15 CEST 2011
I'm learning how to use typeclasses for the first time, and I tried
something like so:
[CODE]
data Point a = Point a a
getX (Point a _) = a
getY (Point _ a) = a
instance Eq Point where
(==) a b = getX a == getX b && getY a == getY b
[/CODE]
But when I try to load this in GHCI I get:
[CODE]
`Point' is not applied to enough type arguments
The first argument of `Eq' should have kind `*',
but `Point' has kind `* -> *'
In the instance declaration for `Eq Point'
[/CODE]
This seems to be saying that Eq requires a concrete argument, which I
suppose I can understand.
I can fix this by changing "data Point a = Point a a" into "data Point =
Point Int Int" or something like that, but that seems kind of lame: what
if I want to leave my Point type more generic? (Maybe sometimes I'll
need a Point Int, and other times I'll need a Point Float, or some other
weird combination I haven't thought of yet.)
Instead of changing the "data" declaration, can I change the "instance"
declaration somehow so that it just defines an instance for a particular
Point sub-type? I tried some variations like "instance Eq (Point Float)"
but I haven't been able to come up with anything yet that satisfies the
compiler.
--
frigidcode.com
theologia.indicium.us
More information about the Beginners
mailing list