title: Polymorphism: a constant versus a function
body: <p>I'm new to Haskell and come across a slightly puzzling example for me in the <em>Haskell Programming from First Principles</em> book. At the end of Chapter 6 it suddenly occurred to me that the following doesn't work:</p>
  
  <pre><code>constant :: (Num a) =&gt; a
  constant = 1.0
  </code></pre>
  
  <p>However, the following works fine:</p>
  
  <pre><code>f :: (Num a) =&gt; a -&gt; a
  f x = 3*x
  </code></pre>
  
  <p>I can input any numerical value for <code>x</code> into the function <code>f</code> and nothing will break. It's not constrained to taking integers. This makes sense to me intuitively. But the example with the constant is totally confusing to me.</p>
  
  <p>Over on a reddit <a href="https://www.reddit.com/r/HaskellBook/comments/50kzh7/ch6_match_the_types/" rel="nofollow">thread</a> for the book it was explained (paraphrasing) that the reason why the constant example doesn't work is that the type declaration forces the value of <code>constant</code> to only be things which aren't more specific than <code>Num</code>. So trying to assign a value to it which is from a subclass of <code>Num</code> like <code>Fractional</code> isn't kosher.</p>
  
  <p>If that explanation is correct, then am I wrong in thinking that these two examples seem completely opposites of each other? In one case, the type declaration forces the value to be as general as possible. In the other case, the accepted values for the function can be <em>anything</em> that implements <code>Num</code>.</p>
  
  <p>Can anyone set me straight on this?</p>