[Haskell-beginners] Typeclasses and generic types

Arlen Cuss celtic at sairyx.org
Tue May 10 05:35:26 CEST 2011


Hi Christopher,

Try this instead:

instance Eq (Point a) where
    ..

Eq needs to take a type of kind `*', meaning the type itself doesn't
have any type variables left. `Point' by itself is a type 'constructor',
if you will, that needs another type to complete it (thus you can
imagine it's like a 'type function' of kind `* -> *', meaning you need
to give one type (`*') to yield a type `*').

You can just use `a' (or whatever) to represent any type filled in
there, however.

HTH!

Arlen

On Mon, 2011-05-09 at 19:25 -0800, Christopher Howard wrote:
> 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.
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110510/0849ad43/attachment.pgp>


More information about the Beginners mailing list