<div dir="ltr">I see. I did know about the specificity rule, but somehow it escaped me that the extra <font face="courier new, monospace">y := x</font><font face="arial, sans-serif"> substitution required for the second instance to match means that the first instance <i>is</i> more specific.</font><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">Thanks!</font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">PS - why do overlapping instances give you hives? Did you mean incoherent instances?<br></font><br>On Wednesday, February 24, 2016 at 7:07:54 PM UTC-8, David Feuer wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><p dir="ltr">Oddly, this message was rejected by the system. Trying again.</p>
<div class="gmail_quote">---------- Forwarded message ----------<br>From: "David Feuer" <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="af668JluFwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">david...@gmail.com</a>><br>Date: Feb 24, 2016 10:04 PM<br>Subject: Re: [Haskell-cafe] Overlapping instances<br>To: "Mitchell Rosen" <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="af668JluFwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">mitchel...@gmail.com</a>><br>Cc: "Haskell-cafe" <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="af668JluFwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">haskel...@googlegroups.com</a><wbr>><br><br type="attribution"><p dir="ltr">It's possible that GHC will backtrack with overlapping enabled (I can't remember, and overlapping instances give me hives) but it doesn't have here. The first instance is "more specific" than the second because the type variable x appears twice.</p>
<div class="gmail_quote">On Feb 24, 2016 9:49 PM, "Mitchell Rosen" <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="af668JluFwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">mitchel...@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Consider this "list membership" typeclass and two overlapping instances:<div><br></div><div><font size="2" face="courier new, monospace">class Elem (x :: k) (xs :: [k])</font></div><div><font size="2" face="courier new, monospace"><br></font></div><div><font size="2" face="courier new, monospace">instance {-# OVERLAPS #-} Elem x (x ': xs)<br>instance {-# OVERLAPS #-} Elem x xs => Elem x (y ': xs)</font><br><br>The inductive style is satisfying, but I'm struggling to understand exactly how GHC can pick one instance over the other.</div><div><br></div><div>How is a constraint like <font face="courier new, monospace">Elem Int [Int]</font><font face="arial, sans-serif"> solved? Certainly the </font><font face="courier new, monospace">Elem x (x ': xs)</font><font face="arial, sans-serif"> instance matches, with </font><font face="courier new, monospace">x = Int</font><font face="arial, sans-serif"> and </font><font face="courier new, monospace">xs = []</font><font face="arial, sans-serif">. But, the second instance is also equally valid with </font><font face="courier new, monospace">x = Int, y = Int, xs = []</font><font face="arial, sans-serif">. Even though <i>if </i>the second instance is chosen, the context cannot be satisfied (no instance for </font><font face="courier new, monospace">Elem x []</font><font face="arial, sans-serif">), it's my understanding that GHC will not backtrack once it picks an instance. And because both instances look valid to me, I don't understand why this code does not require </font><font face="courier new, monospace">IncoherentInstances</font><font face="arial, sans-serif">.</font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">Thanks.</font></div></div><br>______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
<a href="javascript:" target="_blank" gdf-obfuscated-mailto="af668JluFwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Haskel...@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="nofollow" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fmail.haskell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fhaskell-cafe\46sa\75D\46sntz\0751\46usg\75AFQjCNH7sFgl7KfuDcDlaGGG3ip3kRaoIA';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fmail.haskell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fhaskell-cafe\46sa\75D\46sntz\0751\46usg\75AFQjCNH7sFgl7KfuDcDlaGGG3ip3kRaoIA';return true;">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
<br></blockquote></div>
</div>
</blockquote></div></div>