title: Haskell rank two polymorphism compile error
body: <p>Given the following definitions:</p>
  
  <pre><code>import Control.Monad.ST
  import Data.STRef
  
  fourty_two = do
    x &lt;- newSTRef (42::Int)
    readSTRef x
  </code></pre>
  
  <p>The following compiles under GHC:</p>
  
  <pre><code>main = (print . runST) fourty_two -- (1)
  </code></pre>
  
  <p>But this does not:</p>
  
  <pre><code>main = (print . runST) $ fourty_two -- (2)
  </code></pre>
  
  <p>But then as <em>bdonlan</em> points out in a comment, this does compile:</p>
  
  <pre><code>main = ((print . runST) $) fourty_two -- (3)
  </code></pre>
  
  <p>But, this does not compile</p>
  
  <pre><code>main = (($) (print . runST)) fourty_two -- (4)
  </code></pre>
  
  <p>Which seems to indicate that (3) only compiles due to special treatment of infix <code>$</code>, however, it still doesn't explain why (1) does compile.</p>
  
  <p><strong>Questions:</strong></p>
  
  <p>1) I've read the following two questions (<a href="https://stackoverflow.com/questions/9468963/runst-and-function-composition">first</a>, <a href="https://stackoverflow.com/questions/8343239/type-error-with-rank-2-types-and-function-composition">second</a>), and I've been led to believe <code>$</code> can only be instantiated with monomorphic types. But I would similarly assume <code>.</code> can only be instantiated with monomorphic types, and as a result would similarly fail. 
  Why does the first code succeed but the second code does not? (e.g. is there a special rule GHC has for the first case that it can't apply in the second?)</p>
  
  <p>2) Is there a current GHC extension that compiles the second code? (perhaps <a href="http://www.haskell.org/ghc/docs/6.10.1/html/users_guide/other-type-extensions.html#impredicative-polymorphism" rel="nofollow noreferrer">ImpredicativePolymorphism</a> did this at some point, but it seems deprecated, has anything replaced it?)</p>
  
  <p>3) Is there any way to define say <code>`my_dollar`</code> using GHC extensions to do what <code>$</code> does, but is also able to handle polymorphic types, so <code>(print . runST) `my_dollar` fourty_two</code> compiles?</p>
  
  <p><strong>Edit: Proposed Answer:</strong></p>
  
  <p>Also, the following fails to compile:</p>
  
  <pre><code>main = ((.) print runST) fourty_two -- (5)
  </code></pre>
  
  <p>This is the same as (1), except not using the infix version of <code>.</code>.</p>
  
  <p>As a result, it seems GHC has special rules for both <code>$</code> and <code>.</code>, but only their infix versions.</p>