[Haskell-cafe] a simpler way to declare typeclass instances
Alexey Muranov
alexey.muranov at gmail.com
Wed Oct 29 10:10:22 UTC 2014
On 25 oct. 2014, at 18:25, Adam Gundry <adam at well-typed.com> wrote:
> Hi Alexey,
>
> On 25/10/14 16:42, Alexey Muranov wrote:
>> 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)
>
> This case is obviously unambiguous, but what is the general rule? Is the
> presence of the Foo data constructor important? What if I wrote the
> following, which would be a perfectly good instance method:
>
> foo1 == foo2 = (x foo1 == x foo2) && (str foo1 == str foo2)
>
> In general, should the compiler be doing type inference to determine
> which instance a declaration belongs to? In principle, the language
> could permit instance methods to be detached from the instances
> themselves, but I don't think it should.
Maybe to specify the types without type inference, something like this could be used:
(foo1 :: Foo) == (foo2 :: Foo) = (x foo1 == x foo2) && (str foo1 == str foo2)
Alexey.
P.S. Maybe all i say is nonsense, I am not very familiar with the theory of types. I plan to read the Hindley's paper when i have time.
More information about the Haskell-Cafe
mailing list