Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The definition of <code>Ord</code> itself in the <a href="http://www.haskell.org/onlinereport/standard-prelude.html">standard prelude</a> requires there already be an <code>Eq</code> instance:</p> <pre><code>class (Eq a) =&gt; Ord a where ... </code></pre> <p>So it would be just as wrong to violate</p> <pre><code> x == y = compare x y == EQ x /= y = compare x y /= EQ </code></pre> <p>As it would be to violate (from the default definitions for these operators in Ord).</p> <pre><code> x &lt;= y = compare x y /= GT x &lt; y = compare x y == LT x &gt;= y = compare x y /= LT x &gt; y = compare x y == GT </code></pre> <p><strong>Edit: Use in libraries</strong></p> <p>I would be quite surprised if standard libraries didn't make use of <code>Ord</code>'s <code>==</code> and <code>/=</code> operators. The specific purpose operators (<code>==</code>, <code>/=</code>, <code>&lt;=</code>, <code>&lt;</code>, <code>&gt;=</code>, <code>&gt;</code>) are frequently more convenient than <code>compare</code>, so I'd expect to see them used in code for <code>map</code>s or <code>filter</code>s.</p> <p>You can <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/containers-0.5.0.0/src/Data-Map-Base.html">see <code>==</code> being used in guards on keys in <code>Data.Map</code> in fromAscListWithKey</a>. This specific function only calls out for the <code>Eq</code> class, but if the key is also an <code>Ord</code> instance, <code>Ord</code>'s <code>compare</code> will be used for other functions of the resulting <code>Map</code>, which is an assumption that <code>Eq</code>'s <code>==</code> is the same as <code>Ord</code>'s <code>compare</code> and testing for <code>EQ</code>.</p> <p>As a library programmer, I wouldn't be surprised if any of the special purpose operators outperformed <code>compare</code> for the specific purpose. After all, that's why they are part of the <code>Eq</code> and <code>Ord</code> classes instead of being defined as polymorphic for all <code>Eq</code> or <code>Ord</code> instances. I might make a point of using them even when <code>compare</code> is more convenient. If I did, I'd probably define something like:</p> <pre><code>compareOp :: (Ord a) =&gt; Ordering -&gt; Bool -&gt; a -&gt; a -&gt; Bool compareOp EQ True = (==) compareOp EQ False = (/=) compareOp LT True = (&lt;) compareOp LT False = (&gt;=) compareOp GT True = (&gt;) compareOp GT False = (&lt;=) </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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