title: Haskell deduced too strict type
body: <p><em>Disclaimer: I’m just starting to learn Haskell and I’m not sure “strict” is the right word here.</em></p>
  
  <p>I was trying to narrow down my problem but I couldn’t really find the issue, so here is my code that <strong>doesn’t compile</strong>:</p>
  
  <pre><code>module Json where
  import Data.List (intersperse)
  
  data JNode = 
      JObject [(String, JNode)]
    | JArray  [JNode]
    | JString String
    | JNumber Double
    | JBool   Bool
    | JNull
  
  instance Show JNode where
    show = show_node 0 where
      glue = foldl (++) ""
      show_tabs n              = glue $ take n $ repeat "  "
      show_list n              = glue . intersperse ",\n" . map (show_pair (n + 1))
      show_sect n l r xs       = glue ["\n", tabs, l, "\n", show_list n xs, "\n", tabs, r] where tabs = show_tabs n
      -- show_pair :: (Show a) =&gt; Int -&gt; (a, JNode) -&gt; String -- works when uncommented
      show_pair n (name,  val) = glue [show_tabs n, show name, " : ", show_node n val]
      show_node n (JObject xs) = show_sect n "{" "}" xs
      show_node n (JArray  xs) = show_sect n "[" "]" $ zip [0..] xs
      show_node n (JString x ) = show x
      show_node n (JNumber x ) = show x
      show_node n (JBool   x ) = show x
      show_node n (JNull     ) = "null"
  </code></pre>
  
  <p>The error is:</p>
  
  <pre class="lang-none prettyprint-override"><code>Prelude&gt; :l scripts\json.hs
  [1 of 1] Compiling Json             ( scripts\json.hs, interpreted )
  
  scripts\json.hs:21:59:
      No instance for (Enum String)
        arising from the arithmetic sequence `0 .. '
      In the first argument of `zip', namely `([0 .. ])'
      In the second argument of `($)', namely `zip ([0 .. ]) xs'
      In the expression: show_sect n "[" "]" $ zip ([0 .. ]) xs
  
  scripts\json.hs:21:60:
      No instance for (Num String) arising from the literal `0'
      In the expression: 0
      In the first argument of `zip', namely `[0 .. ]'
      In the second argument of `($)', namely `zip [0 .. ] xs'
  Failed, modules loaded: none.
  </code></pre>
  
  <p><strong>Take a look at the line of code with the comment.</strong> Apparently, when there is no type declaration, it requires me to pass <code>String</code> instead of just <code>Show a</code>. Funny enough, it still requires <code>name</code> to be a <code>String</code> when I don’t even use it, e.g. when replacing <code>show_pair</code> implementation with this:</p>
  
  <pre><code>show_pair n (name,  val) = show_node n val
  </code></pre>
  
  <p>Can someone explain to me why it works the way it does?</p>
  
  <hr>
  
  <p><strong>Simplified version</strong> of my code with the same issue in case anyone was going to improve the answer:</p>
  
  <pre><code>data TFoo = 
      FooStr (String, TFoo)
    | FooNum (Int,    TFoo)
  
  -- show_pair :: (a, TFoo) -&gt; String
  show_pair (_,    val) = show_node val
  show_node (FooStr  x) = show_pair x
  show_node (FooNum  x) = show_pair x
  </code></pre>