Derivation of Eq given Ord

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.
*Main> let e1 = Ego "" *Main> let e2 = Ego "" *Main> e1 < e2
<interactive>:1:0: No instance for (Eq (Ego a)) arising from use of `<' at <interactive>:1:0-6 Possible fix: add an instance declaration for (Eq (Ego a)) In the expression: e1 < e2 In the definition of `it': it = e1 < e2
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... Cheers, D.

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

I would say that qualifies as a bug because it relays an error from compile
time to run time.
Andreas
----- Original Message -----
From: "Dougal Stanton"
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.
*Main> let e1 = Ego "" *Main> let e2 = Ego "" *Main> e1 < e2
<interactive>:1:0: No instance for (Eq (Ego a)) arising from use of `<' at <interactive>:1:0-6 Possible fix: add an instance declaration for (Eq (Ego a)) In the expression: e1 < e2 In the definition of `it': it = e1 < e2
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...
Cheers,
D. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Andreas Marth wrote:
I would say that qualifies as a bug because it relays an error from compile time to run time.
It doesn't relay anything to run time - ghci has to _compile_ the expressions you give it too. If you _compile something_ successfully, you will know that _it_ will not fail in the stated way (which is all you need to know for a normal program with 'main'). However it does relay an error from deriving module to using [module/ghci], which may not be right. HOWEVER, I can't reproduce it - ghc (6.6.1) always tells me No instance for (Eq Foo) arising from the superclasses of an instance declaration whether data or newtype, deriving Ord, no Eq instance, and no using operations at all, and Hugs does too (in different words). Isaac

I have to admit that I didn't test it before my first response.
But now I did and can verify that it in deed "it does relay an error from
deriving module to using [module/ghci]".
So if I don't do any comparisons on Ego everything succeeds.
If there is any comparision then ghc (6.6 in my case) returns the
appropriate error.
Still a bit strange for me, but now atleast I know it. :-)
Andreas
----- Original Message -----
From: "Isaac Dupree"
Andreas Marth wrote:
I would say that qualifies as a bug because it relays an error from compile time to run time.
It doesn't relay anything to run time - ghci has to _compile_ the expressions you give it too. If you _compile something_ successfully, you will know that _it_ will not fail in the stated way (which is all you need to know for a normal program with 'main').
However it does relay an error from deriving module to using [module/ghci], which may not be right. HOWEVER, I can't reproduce it - ghc (6.6.1) always tells me No instance for (Eq Foo) arising from the superclasses of an instance declaration whether data or newtype, deriving Ord, no Eq instance, and no using operations at all, and Hugs does too (in different words).
Isaac

It may not be a bug, because you do get an error, but it's certainly an infelicity because the error comes out much too late. I'll Trac this and fix in due course. Thanks for raising it. Simon | -----Original Message----- | From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Dougal | Stanton | Sent: 09 August 2007 16:57 | To: haskell-cafe | Subject: [Haskell-cafe] Derivation of Eq given Ord | | 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. | | > *Main> let e1 = Ego "" | > *Main> let e2 = Ego "" | > *Main> e1 < e2 | > | > <interactive>:1:0: | > No instance for (Eq (Ego a)) | > arising from use of `<' at <interactive>:1:0-6 | > Possible fix: add an instance declaration for (Eq (Ego a)) | > In the expression: e1 < e2 | > In the definition of `it': it = e1 < e2 | | 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... | | Cheers, | | D. | _______________________________________________ | Haskell-Cafe mailing list | Haskell-Cafe@haskell.org | http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Andreas Marth
-
Dougal Stanton
-
Isaac Dupree
-
Malte Milatz
-
Simon Peyton-Jones