[Haskell-cafe] a simpler way to declare typeclass instances

Alexey Muranov alexey.muranov at gmail.com
Sat Oct 25 15:42:43 UTC 2014


Hello,

i am trying to understand how typeclasses work.  I know that they can be used as follows (from the manual):

    data Foo = Foo {x :: Integer, str :: String}

    instance Eq Foo where
       (Foo x1 str1) == (Foo x2 str2) = (x1 == x2) && (str1 == str2)

I am wondering, why is the following seemingly unambiguous syntax not allowed too?

    data Foo = Foo { x :: Integer, str :: String }

    instance Eq Foo

    (Foo x1 str1) == (Foo x2 str2) = (x1 == x2) && (str1 == str2)

If it was allowed, it seems that it could also be applied to records:

    class HasName r where
        name :: r -> String

    data Bird   = Bird { name :: String, wingNumber :: Integer }
    data Person = Person { name :: String, likesBirds :: Bool }

    instance HasName Bird
    instance HasName Person

Alexey.


More information about the Haskell-Cafe mailing list