[Haskell-cafe] Derivation of Eq given Ord
Malte Milatz
malte at gmx-topmail.de
Thu Aug 9 12:20:57 EDT 2007
Dougal Stanton, Thu, 9 Aug 2007 16:57:26 +0100:
> Is there a reason why automatic derivation of Ord without Eq doesn't
> do "the sensible thing" and just derive Eq anyway?
>
> > newtype Id a = Id { a :: String }
> > deriving (Read, Show, Eq, Ord)
> > newtype Ego a = Ego { b :: String }
> > deriving (Read, Show, Ord)
>
> Both will type check, but if you try any (in)equality operators on the
> second they'll be spat back at you.
According to the output of ghci's info command, deriving Ord without
deriving Eq is equivalent to
instance Eq Ego => Eq Ord where ...
That is, if there's an Eq instance declaration for Ego, then Ego will be
an instance of Ord, too. This may come in handy if you want to declare
the Eq instance yourself, possibly even in another module. The
compiler cannot know about instances you're up to declare in other
places, so it would probably not be convenient for the compiler to
derive Eq behind the scenes.
> It doesn't seem *much* of a hardship, but it wasn't what I expected.
> I'm not used to GHC accepting input and silently dropping stuff it
> doesn't like...
Well, obviously, you will still get a type error when trying to use the
instance without an Eq instance declaration, so it is not really a
matter of “silently dropping stuff”, I suppose.
Malte
More information about the Haskell-Cafe
mailing list