Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell: do standard libraries assume Eq and Ord are compatible?
    primarykey
    data
    text
    <p>This is a followup question to <a href="https://stackoverflow.com/questions/17114899/inconsistent-eq-and-ord-instances">Inconsistent Eq and Ord instances?</a>.</p> <p>The question there is essentially: when declaring <code>Eq</code> and <code>Ord</code> instances for a type, must one ensure that <code>compare x y</code> returns <code>EQ</code> if and only if <code>x == y</code> returns <code>True</code>? Is it dangerous to create instances that break this assumption? It seems like a natural law one might assume, but it doesn’t appear to be explicitly stated in the Prelude, unlike e.g. the monad or functor laws.</p> <p>The basic response was: it is a bit dangerous to do this, since libraries may assume that this law holds.</p> <p>My question, now, is: <strong>do any of the standard libraries (in particular, <code>Set</code> or <code>Map</code>) make this assumption? Is it dangerous to have a type with incompatible <code>Eq</code> and <code>Ord</code>, so long as I am only relying on the standard libraries supplied with GHC?</strong> (If big-list questions were still acceptable, I would be asking: which commonly used libraries assume this law?)</p> <p><strong>Edit.</strong> My use-case is similar to that of the original question. I have a type with a custom instance of <code>Eq</code>, that I use quite a bit. The only reason I want <code>Ord</code> is so that I can use it as the domain of a <code>Map</code>; I don’t care about the specific order, and will never use it explicitly in code. So if I can use the derived instance of <code>Ord</code>, then my life will be easier and my code clearer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload