Ok, my turn now. Let&#39;s think about algorithm that takes equivalence relation EQ, ordering relation ORD on abstraction classes generated by this equivalence ( -&gt; equivalence classes ) and divides given input elements XS into appropriate classes and then prints them out according to given ordering ORD. If we pose the restriction (let&#39;s call it (*)), that input XS should have at most one element from every abstraction class, we get sorting in a way that you desire. Hovewer, if we don&#39;t pose that restriction the algorithm still makes perfect sense and is given by standard library sortBy. <br>
<br>I see no reason for restriction (*).<br><br><br><br>For efficiency reasons there could be additional class StrictEq. If the type is in that class then we can assume (*) and use more space-efficient algorithm.<br><br>Now, the problem with<br>
<br>treeSort = concatMap (reverse . snd) . Map.toAscList<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;. Map.fromListWith (++) . map (\x -&gt; (x,[x]))<br>
<br>is that i can&#39;t tell any compact way of implementing treeSortBy in nice manner. This is because Data.Map does not allow us to provide our own comparison test instead of compare.<br><br><br><div class="gmail_quote">
On Mon, Mar 10, 2008 at 6<span class="__mozilla-findbar-search" style="padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;">:</span>10 PM, Adrian Hey &lt;<a href="mailto:ahey@iee.org">ahey@iee.org</a>&gt; wrote<span class="__mozilla-findbar-search" style="padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;">:</span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">Neil Mitchell wrote<span class="__mozilla-findbar-search" style="padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;">:</span><br>

&gt; Hi<br>
&gt;<br>
&gt;&gt; &nbsp;We&#39;re talking about *semantic correctness*, not efficiency. If you<br>
&gt;&gt; &nbsp;want to fine tune your code like this you shouldn&#39;t be relying<br>
&gt;&gt; &nbsp;on overloaded general purpose function implementations. E.G. the<br>
&gt;&gt; &nbsp;COrdering type used by AVL lib is one way to do this. This lets<br>
&gt;&gt; &nbsp;a combining comparison chose which of two &quot;equal&quot; values is used<br>
&gt;&gt; &nbsp;(and other things).<br>
&gt;<br>
&gt; I would have thought<span class="__mozilla-findbar-search" style="padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;">:</span><br>
&gt;<br>
&gt; sort x == sortBy compare x<br>
&gt;<br>
&gt; was a reasonable property to have.<br>
<br>
</div>Certainly, but this is part of (but not the complete) specification<br>
for sortBy, not sort. But given sane Ord/Eq instances and sortBy<br>
implementation, then this is indeed also one of many possible<br>
correct implementatations of sort.<br>
<div class="Ih2E3d"><br>
&gt; But you are proposing that sort<br>
&gt; should make assumptions on the compare function,<br>
<br>
</div>Not just sort, but any function with an Ord constraint is entited<br>
to assume sane behavior wrt to compare. Without this the Ord class<br>
just becomes quite useless, other than saving a few keystrokes for<br>
folk who be bothered to pass any old compare function as explicit arg.<br>
Surely type classes are supposed to be more than that?<br>
<div class="Ih2E3d"><br>
&gt; which you can&#39;t even state in Haskell,<br>
<br>
</div>There are plenty of formal statements about things that can&#39;t be<br>
written in Haskell. That doesn&#39;t mean they aren&#39;t true or should not<br>
be respected or relied upon. It just means Haskell is an imperfect<br>
language for expressing such things.<br>
<br>
&gt; but sortBy should not.<br>
<br>
sortBy should not &quot;assume&quot; anything about the function of type<br>
x -&gt; x -&gt; Ordering. Rather, what sortBy actually does with that<br>
function should be specified.<br>
<div class="Ih2E3d"><br>
&gt; I also know of a data type<span class="__mozilla-findbar-search" style="padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;">:</span><br>
&gt;<br>
&gt; data Set xs = Set [xs]<br>
&gt;<br>
&gt; where the Set constructor is all nicely hidden. If I define Set &quot;ab&quot;<br>
&gt; to be equal to Set &quot;ba&quot;, should the compiler burst into flames?<br>
<br>
??<br>
<br>
 &nbsp;If we<br>
&gt; _require_ all Eq definitions to follow our very narrowly defined<br>
&gt; notion of equality, then the question comes up why we permit people to<br>
&gt; write Eq at all - why doesn&#39;t the compiler just do it for us, if there<br>
&gt; is only one right answer.<br>
<br>
</div>You provided one example yourself..<br>
<div class="Ih2E3d"><br>
data Foo = Foo Int (Int -&gt; Int)<br>
<br>
</div>It&#39;s perfectly possible for Foo to be an abstract type exported from<br>
a module that preserves the invariant that the same function is always<br>
associated with a given Int (value).<br>
<br>
If this is the case there&#39;s no reason why Foo should not be an instance<br>
of Ord or Eq.<br>
<br>
If this isn&#39;t the case then Foo should certainly not be an instance or<br>
either class IMO.<br>
<br>
If this was intended to be the case but in fact isn&#39;t the case, then<br>
that&#39;s a bug.<br>
<br>
Regards<br>
--<br>
<font color="#888888">Adrian Hey<br>
</font><div><div></div><div class="Wj3C7c"><br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http<span class="__mozilla-findbar-search" style="padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;">:</span>//www.haskell.org/mailman/listinfo/haskell-cafe</a><br>

</div></div></blockquote></div><br>