title: Haskell instance declaration affecting interpretation of class
body: <p>The code below gives me this error unless the instance declaration is removed. Additionally, I have no idea what to Google, as the error isn't related to what seems to be causing the problem?</p>
  
  <pre><code>Test.hs|20 col 31 error| Could not deduce (Integral a) arising from a use of `gendivmod'
  || from the context (Euclidean a)
  ||   bound by the type signature for
  ||              gcdBezouts :: Euclidean a =&gt; a -&gt; a -&gt; (a, a, a)
  ||   at /home/jragonfyre/src/haskell/mathlib/Test.hs:17:15-50
  || Possible fix:
  ||   add (Integral a) to the context of
  ||     the type signature for
  ||       gcdBezouts :: Euclidean a =&gt; a -&gt; a -&gt; (a, a, a)
  || In the expression: gendivmod x y
  || In a pattern binding: (q, r) = gendivmod x y
  || In the expression:
  ||   let
  ||     (q, r) = gendivmod x y
  ||     (n, m, d) = gcdBezouts y r
  ||   in (m, n - m * q, d)
  </code></pre>
  
  <p>This is a MWE to reproduce the error on my machine.</p>
  
  <pre><code>{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}
  
  class (Num a, Eq a) =&gt; Euclidean a where
    genmod :: a -&gt; a -&gt; a
    genmod a = snd . gendivmod a
    gendiv :: a -&gt; a -&gt; a
    gendiv a = fst . gendivmod a
    gendivmod :: a -&gt; a -&gt; (a,a)
    gendivmod a b = (gendiv a b, genmod a b)
    isUnitEu :: a -&gt; Bool
    isUnitEu = (==1) . abs
  
  instance (Integral a) =&gt; Euclidean a where
    gendivmod = divMod
  
  gcdBezouts :: (Euclidean a) =&gt; a -&gt; a -&gt; (a, a, a)
  gcdBezouts 0 x = (0, 1, x)
  gcdBezouts x 0 = (1, 0, x)
  gcdBezouts x y = let (q, r) = gendivmod x y
                       (n, m, d) = gcdBezouts y r
                   in (m, n-m*q, d)
  </code></pre>
  
  <p>As for the motivation for not using a newtype, and going with the specific instance declaration, I have a lot of existing code that was written for integers that I wanted to generalize to work with polynomials as well, and I didn't want to have to rewrite it into an ugly mess with wrapping and unwrapping the newtype.</p>
  
  <p>If there is another solution that will accomplish what I'd like, that would also be appreciated.</p>