title: Referential transparency with polymorphism in Haskell
body: <p>Say I have a function:</p>
  
  <pre><code>f :: Int -&gt; (Rational, Integer)
  f b = ((toRational b)+1,(toInteger b)+1)
  </code></pre>
  
  <p>I want to abstract away the (+1) like so:</p>
  
  <pre><code>f :: Int -&gt; (Rational, Integer)
  f b = (h (toRational b)
        ,h (toInteger b))
      where h = (+1)
  </code></pre>
  
  <p>This wont work obviously, but if I specify the type signature it will work:</p>
  
  <pre><code>f :: Int -&gt; (Rational, Integer)
  f b = (h (toRational b)
        ,h (toInteger b))
      where h :: Num a =&gt; a -&gt; a
            h = (+1)
  </code></pre>
  
  <p>Say I now want to further abstract the function by passing h as a parameter:</p>
  
  <pre><code>f :: Num a =&gt; Int -&gt; (a -&gt; a) -&gt; (Rational, Integer)
  f b g = (h (toRational b)
          ,h (toInteger b))
      where h :: Num a =&gt; a -&gt; a
            h = g
  </code></pre>
  
  <p>I get an error that the inner a is not the same a as the outer one.</p>
  
  <p>Does anyone know how to write this function correctly?
  I want to pass a polymorphic function <code>g</code> to <code>f</code> and use it polymorphically.</p>
  
  <p>I have encountered this situation multiple times now in very different projects, and I could not find a good solution.</p>