Alex Ferguson wrote:
| But alas, not in Haskell; this would be possible, though, if the
| rules against circular instance declarations and overlapping
| instances were relaxed, though. The question is though, does this
| happen sufficiently often (a subtype instance completely determining
| its supertype's methods) to be worth bending the language sufficiently
| to admit it?
I would suggest you take a look at Simon, Erik and Mark's paper [1].
My answer is: yes, there are a lot of examples where this is useful.
Simon's answer is: yes, but allowing this breaks other properties which I
find more desirable.
Examples for use of defaults & overlapping instances:
-- Ord => Eq
instance Ord a => Eq a where
a == b = a <= b && b <= a
-- Monads with zeros:
class Monad m => ErrorMonad m where
raise :: String -> m a
handle :: (String -> m a) -> m a -> m a
instance ErrorMonad m => MonadZero m where
zero = raise "zero"
Examples of other uses of overlapping instances:
-- Text
class Text a where
show :: a -> String
instance Text a => Text [a] where
show = brackets . commas . map show
instance Text [Char] where
show s = "\"" ++ s ++ "\""
The undesirable property Simon doesn't agree with is that overlapping
instances don't interfere with separate compilation.
Regards,
Koen.
References:
[1] Peyton Jones, Meijer and Jones, "Type classes: an exploration of the
design space", http://www.cse.ogi.edu/~simonpj/multi.ps.gz, to
appear on the Haskell Workshop 1997, Amsterdam.
--
| Koen Claessen, [EMAIL PROTECTED] |
| http://www.cse.ogi.edu/~kcclaess/ |
|------------------------------------------------------|
| Visiting student at OGI, Portland, Oregon, USA. |