title: Calculate the rank of a type signature of Haskell?
body: <p>I know the id has rank 1 and rank3 has rank 3. However, I am not sure whether there are some formula to calculate the ranks. For the order of a function we have:</p>
  
  <pre><code>order(c) = 0 
  order(A -&gt; B) = max(order(A)+1, order(B))
  
  rank1   :: forall a. a -&gt; a
  rank2   :: (forall a. a -&gt; a) -&gt; (b -&gt; b)
  rank3   :: ((forall a. a -&gt; a) -&gt; (Bool, Char)) -&gt; (Char, Bool)
  </code></pre>
  
  <p>It seems that the rank is dependent on the "depth" of "forall" in the type signature, but this is not clear enough for me to see it. I have the following questions:</p>
  
  <ol>
  <li>What is the rank of <code>undefined :: forall a. a</code>? It is 0 or 1?</li>
  <li>What is the rank of <code>f :: ((a -&gt; a) -&gt; (forall a. a -&gt; a)) -&gt; (forall b. b -&gt; b)</code>?  2?</li>
  <li>Is there a formula to calculate the number of rank explicitly?</li>
  <li>If there is no formula, can someone just tell me how to count the rank explicitly?</li>
  </ol>