title: Understanding type variables that only appear in the return type
body: <p>I'm having some trouble understanding how to understand and use type variables that only appear in the return type of a function.</p>
  
  <p>I'm trying to use <a href="https://hackage.haskell.org/package/diagrams-cairo">diagrams-cairo</a> to compare two diagrams, pixel by pixel. The <a href="http://projects.haskell.org/diagrams/haddock/Diagrams-Backend-Cairo-List.html">renderToList</a> function has the type:</p>
  
  <pre><code>renderToList :: (Ord a, Floating a) =&gt; Int -&gt; Int -&gt; Diagram Cairo R2 -&gt; IO [[AlphaColour a]]
  </code></pre>
  
  <p>Returning a list of lists of <code>AlphaColour a</code>. Bearing in mind that <code>a</code> is <code>(Ord a, Floating a)</code>, I figured I could use mathematical and comparison operations on these <code>AlphaColour a</code> values:</p>
  
  <pre><code>import Diagrams.Prelude
  import Diagrams.Backend.Cairo
  import Diagrams.Backend.Cairo.List
  import Data.Colour
  import Data.Colour.SRGB
  import Data.Foldable (fold)
  import Data.Monoid
  
  cmp :: Diagram Cairo R2 -&gt; Diagram Cairo R2 -&gt; Diagram Cairo R2 -&gt; IO Bool
  cmp base img1 img2 = do
                  baseAlphaColours &lt;- renderToList 400 400 base
                  img1AlphaColours &lt;- renderToList 400 400 img1
                  img2AlphaColours &lt;- renderToList 400 400 img2
                  return $ (imgDiff baseAlphaColours img1AlphaColours) &lt; (imgDiff baseAlphaColours img2AlphaColours)
  
  imgDiff :: (Ord a, Monoid a, Floating a) =&gt; [[AlphaColour a]] -&gt; [[AlphaColour a]] -&gt; a
  imgDiff img1 img2 = fold $ zipWith diffPix (concat img1) (concat img2)
  
  diffPix :: (Ord a, Floating a) =&gt; AlphaColour a -&gt; AlphaColour a -&gt; a
  diffPix a1 a2 = (diffRed * diffRed) - (diffGreen * diffGreen) - (diffBlue * diffBlue)
              where red a = channelRed $ toSRGB (a `over` black)
                    green a = channelGreen $ toSRGB (a `over` black)
                    blue a = channelBlue $ toSRGB (a `over` black)
                    diffRed = (red a1) - (red a2)
                    diffGreen = (green a1) - (green a2)
                    diffBlue = (blue a1) - (blue a2)
  </code></pre>
  
  <p>However I'm getting the ominous compile error</p>
  
  <pre><code>Ambiguous type variable `a0' in the constraints:
    (Floating a0)
      arising from a use of `renderToList' at newcompare.hs:11:37-48
    (Ord a0)
      arising from a use of `renderToList' at newcompare.hs:11:37-48
    (Monoid a0)
      arising from a use of `imgDiff' at newcompare.hs:14:27-33
  Probable fix: add a type signature that fixes these type variable(s)
  In a stmt of a 'do' block:
    baseAlphaColours &lt;- renderToList 400 400 base
  In the expression:
    do { baseAlphaColours &lt;- renderToList 400 400 base;
         img1AlphaColours &lt;- renderToList 400 400 img1;
         img2AlphaColours &lt;- renderToList 400 400 img2;
         return
         $ (imgDiff baseAlphaColours img1AlphaColours)
           &lt; (imgDiff baseAlphaColours img2AlphaColours) }
  In an equation for `cmp':
      cmp base img1 img2
        = do { baseAlphaColours &lt;- renderToList 400 400 base;
               img1AlphaColours &lt;- renderToList 400 400 img1;
               img2AlphaColours &lt;- renderToList 400 400 img2;
               .... }
  </code></pre>
  
  <p>Which I understand as the compiler wanting to know the full type of the <code>renderToList</code> calls.</p>
  
  <p>But what I don't understand is:</p>
  
  <ul>
  <li>Why does the compiler need to know the full type? I think I'm only using operations available to <code>Ord</code> and <code>Floating</code> instances.</li>
  <li>If I do need to provide a type, where exactly in the code would I define this type.</li>
  <li>How can I even know what the full concrete type returned from <code>renderToList</code> is?</li>
  </ul>
  
  <p>I feel I'm missing something fundamental with the way this code is written, any help would be greatly appreciated.</p>