<div dir="ltr">Be careful with that. I seem to recall a StackOverflow post where a really confusing bug was eventually tracked down to a declaration of<br><br>instance Num (Double,Double)<br><br>somewhere in an imported package.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 23, 2015 at 1:03 PM, Michael Orlitzky <span dir="ltr"><<a href="mailto:michael@orlitzky.com" target="_blank">michael@orlitzky.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 02/23/2015 03:46 PM, Stuart Hungerford wrote:<br>
><br>
> instance (AddSemigroup a, AddSemigroup b) => AddSemigroup (a, b)<br>
><br>
> GHC says it "Could not deduce (Num (a, b))" in this situation, which<br>
> seems fair enough so I tried:<br>
><br>
> instance (AddSemigroup a, Num a, AddSemigroup b, Num b) => AddSemigroup (a, b)<br>
<br>
</span>Since you've defined the "AddSemigroup plus" to be the "Num plus", when<br>
you attempt to make (a,b) an instance of AddSemigroup, GHC tries to use<br>
the "Num plus" on them; i.e.<br>
<br>
  (x1,y1) + (x2,y2) = ???<br>
<br>
Since (a,b) isn't an instance of Num, it doesn't know what to do here.<br>
It's obvious that you want,<br>
<br>
  (x1,y1) + (x2,y2) = (x1+x2, y1+y2)<br>
<br>
(which is fine, since both 'a' and 'b' are instance of Num), but you<br>
could just as well declare,<br>
<br>
  (x1,y1) + (x2,y2) = (x1*x2, y1*y2)<br>
<br>
The compiler doesn't know, so declaring 'a' and 'b' as instances of Num<br>
isn't enough. You really have to tell it how to add the pair.<br>
<span class=""><br>
<br>
><br>
> With the same result.  Separate instances seem to work though:<br>
><br>
> instance (Num a, Num b) => Num (a, b)<br>
><br>
> instance (AddSemigroup a, AddSemigroup b) => AddSemigroup (a, b)<br>
><br>
<br>
</span>Now it knows how to add (x1,y1) and (x2,y2), since (a,b) is an instance<br>
of Num. The compiler won't infer the pair instance from the individual<br>
ones though.<br>
<div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br></div>