noob “Duplicate instance declarations” (again)

谁说我不能喝 提交于 2020-01-11 06:16:05

问题


yes...sorry this has been asked before, but usually about something so specific and complex that it in incomprehensible

with a naïve OO head...we go....

class Animal a where

class Mammal m where

class Insect i where

instance (Mammal m) => Animal m

instance (Insect i) => Animal i

and ghc goes

Duplicate instance declarations:

  instance forall (k :: BOX) (m :: k). Mammal m => Animal m
  instance forall (k :: BOX) (i :: k). Insect i => Animal i

and you look it up...and there is a solution using witness types, that I could probably get to work BUT....I don't understand what the problem?

allegedly the compiler matches the right hand side and bla bla...?

I don't understand....I think I'm saying...if I have a type of typeclass Dog...then its also of typeclass Animal...so if I call methods foo etc then this is how to do it (in terms of a Dog)

I'm missing something


回答1:


Yup, you're missing something. Here's how you should have defined your class hierarchy:

class Animal a where
class Animal a => Mammal a where
class Animal a => Insect a where

This is how you express superclass relationships. Instance declarations are for actually making types instances of your classes.

instance Animal Ant where
instance Insect Ant where

What goes wrong in the original

You wrote

instance (Mammal m) => Animal m

instance (Insect i) => Animal i

Haskell requires that there be only one instance for each class and type. Thus is determined only from the part to the right of the =>. So it sees two declarations for

instance Animal a

and complains. You could have

instance Animal (Maybe a)

and also

instance Animal Int

But if you have

instance Animal a

then you can't have any other instance declarations for Animal.



来源:https://stackoverflow.com/questions/32375983/noob-duplicate-instance-declarations-again

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!