noob“重复实例声明" (再次) [英] noob "Duplicate instance declarations" (again)

查看:117
本文介绍了noob“重复实例声明" (再次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的...很抱歉,以前有人问过这个问题,但是通常是因为如此具体和复杂,以至于无法理解

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

一个天真的OO头...我们走....

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

动物a类

哺乳动物类m在哪里

将昆虫分类到哪里

实例(哺乳动物m)=>动物m

instance (Mammal m) => Animal m

instance(Insect i)=>动物我

instance (Insect i) => Animal i

ghc走了

重复的实例声明:

  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...?

我不明白....我想我是说...如果我有一个类型类Dog的类型...那么它的类型也就是Animal ...所以如果我调用foo等方法的话是怎么做的(就狗而言)

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)

我想念一些东西

推荐答案

是的,您缺少了一些内容.这是您应该如何定义类层次结构的方法:

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

原件出了什么问题

你写了

instance (Mammal m) => Animal m

instance (Insect i) => Animal i

Haskell要求每个类和类型只有一个实例.因此仅从=>的右侧确定.所以它看到两个声明

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

并抱怨.你可能有

instance Animal (Maybe a)

还有

instance Animal Int

但是如果你有

instance Animal a

那么您将没有Animal的其他任何实例声明.

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

这篇关于noob“重复实例声明" (再次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆