[Haskell-cafe] [Newbie] Quest for inheritance

Gracjan Polak gracjan at acchsh.com
Mon Jun 6 08:28:01 EDT 2005


Cédric Paternotte wrote:
> Hi Gracjan,
> 
> 
> This is smart. So I understand the point of this part is to forward
> the "function call" to the parent (through get_super). All you have to
> do is to define these forwards in each inheriting data.

Yes. I think this is the whole point of inheritance :)

> 
> Does it also mean that, in each inheriting data, you have to define
> these forwards to all your parents (meaning not only to the one just
> above, but all of them) ? In other words if I was to define a data DD
> which inherits from DB (and thus also from DA), will I have to define
> forwards for both get_a and get_b ? If not, how would you declare it ?
>

This is exactly what I described as "private inheritance". If you have 
(Inherits DA DB) and (Inherits DB DC) this does not mean that you have 
automatically (Inherits DA DC). Why? This would require instance:

instance (Inherits a b,Inherits b c) => (Inherits a c) where ...

but this summons known problem: multiple inheritance. How to chose b? 
Imagine such situation:

data DA; data DB; data DC; data DD

instance Inherits DA DB where ...
instance Inherits DA DC where ...
instance Inherits DB DD where ...
instance Inherits DC DD where ...

DD inherits DA *twice*. So b in above instance declaration would not be 
determined uniquely.

> 
>>As you see there is much more writting as in Java. But this gives better
>>control over inheritance and subsumption because everything must be
>>stated explicitly. Multiple inheritance is allowed :) Also it is
>>"private inheritance" (as in C++) by default.
> 
> 
> I think I like this way of dealing with inheritance. There's a bit
> more typing indeed and it's kind of limited but it has the advantage
> of being relativily simple to put in action.

I agree with typing, but compared to Java this is actually not limited 
but more powerful, because it gives greater control over inheritance.

Most important aspect to me is that inheritance can be specified *after* 
data declaration. Imagine you have some strange library that has DA and 
DB, that are obviosly in generalization-specialization hierarchy, but 
some jerk forgot to inherit one from another. In Java you are toast, in 
Haskell you can specify inheritance relation in your code :)


> What I really like with this is that you can come up with new data
> types inheriting DA without having to change anything in the
> declaration of DA.
> 
> I guess you'd just better avoid having too many levels of hierarchy as
> it tends to get more and more verbose ;)

If you stick to single inheritance there is other way to simulate OO in 
Haskell. Look for "phantom types". Whole wxHaskell (for example) is 
based on this concept.

> 
> Cédric

-- 
Gracjan


More information about the Haskell-Cafe mailing list