[Haskell-beginners] Understanding inheritance

Kyle Murphy orclev at gmail.com
Mon Dec 2 14:21:02 UTC 2013


I think you're headed down a path that's only going to cause you confusion.
You shouldn't think in OO terms like sub and super classes. Each class is
its own thing, they aren't sub and super classes in the OO sense. A class
my have a constraint applied to it such that any type that is an instance
of that class, must also be an instance of one or more other classes. That
constraint is what allows you to use functions that belong to other classes
inside of default function definitions. Semantically this behaves much like
inheritance in an OO language, however functionally it's very different.
On Dec 1, 2013 7:08 PM, "Patrick Browne" <patrick.browne at dit.ie> wrote:

> Hi,
> In the example below does the concept of inheritance only apply to classes
> and not instances?
> By inheritance I mean that a method in a sub-class can use a method from
> the super-class but not vice versa.
> Would the inclusion of instance contexts such as 'instance (Eq a, Show b)
> => MyClass a b' apply such inheritance semantics between LHS => RHS of
> instances?
> My current misunderstandings are in the comments.
> Thanks,
> Pat
> -- Any instance of SUPER must define the super method. The super method
> definition may use any function in scope on the RHS of equations.
> -- If the default method is defined in the class it may use any method in
> scope except those from the sub-class
> class SUPER a where
>  super :: a -> a
> -- Any instance of SUB must define sub. The sub method definition may use
> super or any function in scope on the RHS of equations
> class SUPER a => SUB a where
>   sub :: a -> a
>
> -- This instance defines super and can use sub because sub it is defined
> and is in scope
> instance SUPER Int  where
>  super n  = 1 + (sub n)
>
> -- This instance defines sub
> instance SUB Int  where
>  sub n = 6 +  n
>
> -- The Char instances differs from the Int instances in that the SUB uses
> super.
> instance SUPER Char where
>  super n  = 's'
>
> -- The use of super here because it is defined and is in scope.
> instance SUB Char where
>  sub n = super n
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20131202/7f70a701/attachment.html>


More information about the Beginners mailing list