简体   繁体   English

Haskell继承:它的遗传是什么?

[英]Haskell inheritance: What's inherity about it?

Here http://en.wikibooks.org/wiki/Haskell/Classes_and_types in section Class inheritance, I read "A class can inherit from several other classes: just put all the ancestor classes in the parentheses before the =>." 这里是类继承中的http://en.wikibooks.org/wiki/Haskell/Classes_and_types ,我读到“一个类可以继承其他几个类:只需将所有祖先类放在=>之前的括号中。”

I am puzzled when "(...)=>" is described as "inheritance". 当“(...)=>”被描述为“继承”时,我很困惑。 So far as I can see, it's simply a class constraint. 据我所知,它只是一个类约束。 It merely says that this newly defined class (in the example: Real) applies to types which are already members (have instances for) the listed classes (Num and Ord). 它只是说这个新定义的类(在示例中为Real)适用于已经列出的类(Num和Ord)的成员(具有实例)的类型。

In short, the "(...)=>" seems to me to act like a filter for qualities required of the types for which instances of this class may be created, and does not act to augment either the class or its instances. 简而言之,“(...)=>”似乎对于可以创建此类实例的类型所需的质量的过滤器起作用,并且不用于增强类或其实例。

Am I missing something? 我错过了什么吗? Is there some sense in which the "(...)=>" actually passes something along from "parent" to "child"? 是否有某种意义上“(...)=>”实际上从“父母”传递给“孩子”?

In practice, this means that all members of the subclass necessarily provide all methods of the superclass. 实际上,这意味着子类的所有成员都必须提供超类的所有方法。

So, as in the linked example, we can write a method that requires Eq , but only give it an Ord constraint, and the Eq methods are implied for us. 因此,如在链接示例中,我们可以编写一个需要Eq的方法,但只给它一个Ord约束,并且我们隐含了Eq方法。

(Note that inheritance is probably a terrible term for this, because it carries a lot of associations that don't make sense in our context. Nonetheless, I figured I might as well explain it.) (请注意,继承可能是一个可怕的术语,因为它带有许多在我们的上下文中没有意义的关联。但是,我想我也可以解释它。)

Later reply, @gwideman, I think your original understanding was correct. 后来回复,@ gwideman,我认为你原来的理解是正确的。

In short, the "(...)=>" seems to me to act like a filter for qualities required of the types for which instances of this class may be created, and does not act to augment either the class or its instances 简而言之,“(...)=>”似乎对于可以创建此类实例的类型所需的质量的过滤器起作用,并且不会用于增强类或其实例

That wiki page's "Class inheritance" is wrong. 那个wiki页面的“类继承”是错误的。 here is my reason. 这是我的理由。 in the page, it says. 在页面中,它说。

Here, it means that for a type to be an instance of Ord it must also be an instance of Eq, and hence needs to implement the == and /= operations 这里,这意味着对于一个类型是Ord的实例,它也必须是Eq的一个实例,因此需要实现==和/ =操作

if you run ghci, and type :info Ord ,it shows following information: 如果你运行ghci,并输入:info Ord ,它会显示以下信息:

class Eq a => Ord a where
  compare :: a -> a -> Ordering
  (<) :: a -> a -> Bool
  (<=) :: a -> a -> Bool
  (>) :: a -> a -> Bool
  (>=) :: a -> a -> Bool
  max :: a -> a -> a
  min :: a -> a -> a
  {-# MINIMAL compare | (<=) #-}

https://downloads.haskell.org/~ghc/7.8.1/docs/html/users_guide/pragmas.html , for explanation of "MINIMAL" https://downloads.haskell.org/~ghc/7.8.1/docs/html/users_guide/pragmas.html ,解释“MINIMAL”

look at " MINIMAL " , it says, an instance of Ord only needs to implement compare or (<=) , which means, you do not needs to "implement the == and /= operations". 看看“ MINIMAL ”,它说,Ord的一个实例只需要实现比较 (<=),这意味着,你不需要“实现==和/ =操作”。 only that polymorphic 'a' needs to implement the == Or /= (check Eq's MINIMAL pragmas) 只有那个多态'a'需要实现==或/ =(检查Eq的MINIMAL编译指示)

(...)=> is type class constraint, not Java like interface inheritance. (...)=>是类型类约束,而不是类似接口继承的Java。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM