default class methods in sub-classes

Ross Paterson ross@soi.city.ac.uk
Thu, 6 Feb 2003 11:20:52 +0000


On Wed, Feb 05, 2003 at 09:16:58PM -0800, Dylan Alex Simon wrote:
> Apparently I can't do this (in ghc or hugs -- haven't tried others):
> 
> class Space a where
>   distance :: a -> a -> Int
>   subtract :: a -> a -> a
> 
> class (Space a) => NormSpace a where
>   norm :: a -> Int
>   distance a b = norm (subtract a b)
> 
> That is, I can't make (or redefine) a default for the superclass method
> 'distance' in a subclass.  I agree that the Haskell'98 definition doesn't
> claim that this should be allowed, but it seems like something that would be
> useful.  Are there reasons not to allow this (or that I shouldn't want to do
> this at all)?

No, it's not legal, but it would indeed be useful, even in the Prelude,
e.g. defining (==) in Ord, or fmap in Monad.  It seems a reasonable idea
of subclass, but might be hard to do across module boundaries.