On Mon, Dec 8, 2008 at 2:04 AM, Martin Hofmann <span dir="ltr">&lt;<a href="mailto:martin.hofmann@uni-bamberg.de">martin.hofmann@uni-bamberg.de</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I am storing the TH data types &#39;Exp&#39; and &#39;Pat&#39; in Maps and Sets. As a<br>
first attempt to quickly get rid of typechecker&#39;s complaints I defined<br>
some naive instances of Ord for Exp and Pat.<br>
<br>
Now it took me about a week to realise, that &#39;instance Ord Pat&#39; causes<br>
ghc to loop. Apparently, there is a reason, why Pat and Exp are not<br>
instances of Ord. What is so bad about it?<br>
<br>
If Pat and Exp should not be instances of Ord, maybe a note in the<br>
source code or haddock would be helpful. On the other hand, what would<br>
argue against a lexicographic ordering (apart from the inefficient<br>
implementation of the following one)?<br>
<br>
Following some literate code to reproduce the &lt;&lt;loop&gt;&gt; (or stack<br>
overflow in GHCi), by commenting and uncommenting the appropriate lines:</blockquote><div><br>Try this:<br><br>data Foo = Foo deriving Eq <br><br>instance Ord Foo<br><br>Then try Foo &lt; Foo.<br><br>instance Ord Foo is not the same as &quot;deriving Ord&quot;; it declares an instance using all default definitions, which are self-referential.<br>
<br>It would be nice if typeclass authors could somehow declare the minimal complete definition, so we could get a warning in this case.<br><br>Luke<br>&nbsp;</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
<br>
<br>
&gt; {-# OPTIONS_GHC -fglasgow-exts -fth #-}<br>
&gt; module Test where<br>
&gt; import <a href="http://Language.Haskell.TH" target="_blank">Language.Haskell.TH</a><br>
&gt; import Data.Set<br>
<br>
<br>
-------------------<br>
&nbsp;naive Ord<br>
<br>
&gt; instance Ord Exp<br>
<br>
&gt; instance Ord Pat<br>
<br>
-------------------<br>
&nbsp;lexicographic Ord<br>
<br>
&nbsp;instance Ord Exp where<br>
 &nbsp; &nbsp; compare l r = compare (show l) (show r)<br>
<br>
&nbsp;instance Ord Pat where<br>
 &nbsp; &nbsp; compare l r = compare (show l) (show r)<br>
<br>
-------------------<br>
<br>
<br>
&gt; mkVP s = VarP $ mkName s<br>
&gt; mkVE s = VarE $ mkName s<br>
&gt; rule1 = (,) [mkVP &quot;x_14&quot;] (mkVE &quot;y_14&quot;)<br>
&gt; rule2 = (,) [InfixP (mkVP &quot;x1_15&quot;) &#39;(:) (mkVP &quot;x_16&quot;)] (InfixE (Just (mkVE &quot;y1_15&quot;)) (ConE &#39;(:)) (Just (mkVE &quot;ys_16&quot;)))<br>
<br>
&gt; stack_overflow = fromList [rule1,rule2]<br>
<br>
<br>
Thanks,<br>
<br>
Martin<br>
<br>
<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://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>