Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 =>."

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

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

like image 399
gwideman Avatar asked Feb 07 '13 02:02

gwideman


2 Answers

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.

(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.)

like image 101
sclv Avatar answered Sep 24 '22 21:09

sclv


Later reply, @gwideman, I think your original understanding was correct.

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

if you run ghci, and type :info Ord,it shows following information:

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"

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". only that polymorphic 'a' needs to implement the == Or /= (check Eq's MINIMAL pragmas)

(...)=> is type class constraint, not Java like interface inheritance.

like image 23
Software Wisdom Avatar answered Sep 25 '22 21:09

Software Wisdom