<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="mailto:david.feuer@gmail.com">david.feuer@gmail.com</a>><br>Date: Feb 24, 2016 10:04 PM<br>Subject: Re: [Haskell-cafe] Overlapping instances<br>To: "Mitchell Rosen" <<a href="mailto:mitchellwrosen@gmail.com">mitchellwrosen@gmail.com</a>><br>Cc: "Haskell-cafe" <<a href="mailto:haskell-cafe@googlegroups.com">haskell-cafe@googlegroups.com</a>><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="mailto:mitchellwrosen@gmail.com" target="_blank">mitchellwrosen@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>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div>
</div>