Optimight
Optimight

Reputation: 3061

How to create Sub Type Class of Type Class in haskell?

Can we create Sub Type of Type Class in haskell? Up to how many level sub-typing of Type Class can go?

Upvotes: 1

Views: 892

Answers (1)

Vladimir Matveev
Vladimir Matveev

Reputation: 127721

Yes, it is possible to create some kind of subclass in Haskell. It looks basically like this:

class Parent a where
    ...

class Parent a => Child a where
    ...

Then any instance of Child is also required to be an instance of Parent.

See, for example, Applicative class.

Also I don't think there is a restriction on 'level' of subclassing since (I guess so) subclassing can be though as sequential union of corresponding instances' class dictionaries which contain their respective implementations of polymorphic functions, and seemingly there are no boundaries for this dictionary growth.

Upvotes: 4

Related Questions