Revision as of 14:09, 17 April 2009

Context aliases, also known as class aliases, are a long-requested feature of Haskell. This feature would allow class hierarchies to be restructured without breaking compatibility to a certain degree. Also, it would make fine-grained class hierarchies usable.

Notice that BoundedBelowJoinable doesn't have the alias keyword. Is this a syntax error or is it allowed? It is allowed because BoundedBelowJoinable is not just an alias for (BoundedBelow a, SemiLatticeJoin a). It also declares a new method called joins.

So why is this usefull?

Users can declare instances for BoundedBelow and SemiLatticeJoin and get joins for free or they can declare an instance for BoundedBelowJoinable and define an optimized joins for their type.

Lets look at another example why this ability, to give a class alias new methods, is useful. Again I take an example from the original proposal but I slightly change it:

The current Num class in the Prelude is (more or less) this

class Num a where
(+) :: a -> a -> a
(*) :: a -> a -> a
(-) :: a -> a -> a
negate :: a -> a
fromInteger :: Integer -> a

Ideally we would want to split it up using classes from the monoids package:

class Monoid a where
mempty :: a
mappend :: a -> a -> a
class Monoid a => Group a where
gnegate :: a -> a
minus :: a -> a -> a
gsubtract :: a -> a -> a