[Haskell-cafe] partial inheritance

Christopher Done chrisdone at googlemail.com
Sun Jul 17 11:54:49 CEST 2011


On 17 July 2011 11:36, Patrick Browne <patrick.browne at dit.ie> wrote:
> Hi,
> Is it possible to model partial inheritance using Haskell type classes?
> For example, if the class Bird has a flying method can we represent
> Penguins as a sub-class of Bird without a flying method?

Possibly with duck typing[1], but that's not vanilla Haskell type
classes. FWIW I think you'd have to have:

class (Flying a,Wings a,Feathers a,Beak a) => Bird a
class (Wings a,Feathers a,Beak a) => Penguin a

But I think that's nice anyway. Your functions, IMHO, should ask for
only the properties of the object it needs, so:

nomOnCorn :: Beak a => a -> Corn -> a
nomOnCorn = ...

Rather than

nomOnCorn :: Bird a => a -> Corn -> a
nomOnCorn = ...

As there's no need to restrict yourself by having a Bird class, IMHO.
I don't think the subclassing/hierarchy concept scales as well as a
graph.

[1]: http://chrisdone.com/posts/2010-11-22-duck-typing-in-haskell.html



More information about the Haskell-Cafe mailing list