title: How to assign a value from the IO monad to a RankNType qualified constructor
body: <p><strong>(UPDATED)</strong></p>
  
  <p>I have made an interface using a <a href="https://stackoverflow.com/a/23767740/163177">Free Monad</a> to a generic data store. I want to place the specific interpreter (:: DataStore a -> IO a) chosen by the user at run time into a state monad along with some other information.  I cannot seem to put anything into this field in the data structure.</p>
  
  <p>How do I put a value into a field defined as a higher rank type?</p>
  
  <p>Below is a minimum example:</p>
  
  <pre><code>{-# LANGUAGE RankNTypes, DeriveFunctor #-}
  
  data ProgramState = PS { -- line 3
      [...]
    , storageInterface :: (forall a. DataStore a -&gt; IO a)
    }
  
  data DataStoreF next = 
       Create    Asset                           ( String -&gt; next)
    |  Read      String                          ( Asset  -&gt; next)
    |  Update    Asset                           ( Bool   -&gt; next)
    |  UpdateAll [Asset]                         ( Bool   -&gt; next)
    |  [...]
    deriving Functor
  
  type DataStore = Free DataStoreF
  
  runMemory :: (IORef (Map String Asset)) -&gt; DataStore a -&gt; IO a
  runMemory ms (Pure a) = return a
  runMemory ms (Free Create asset next) = [...]
  runMemory ms (Free Read   str   next) = [...]
  [...]
  
  pickStorageInterface :: IO (DataStore a -&gt; IO a)
  pickStorageInterface = do
    opts &lt;- parseOptions
    case (storage opts) of
       MemoryStorage -&gt; 
         ms &lt;- readAssetsFromDisk 
         return $ runMemory ms
       SomeOtherStorage -&gt; [...]
  
  restOfProgram :: StateT ProgramState IO
  restOfProgram = [...]
  
  main = do
    si &lt;- pickStorageInterface
    let programState = PS { storageInterface = si} -- line 21
    evalState restOfProgram programState
  </code></pre>
  
  <p>When I try to do this GHC complains that:</p>
  
  <pre><code>Main.hs: &lt;&lt; Line 21 &gt;&gt;
  Couldn't match type `a0' with `a'
    because type variable `a' would escape its scope
  This (rigid, skolem) type variable is bound by
    a type expected by the context: DataStore a -&gt; IO a
    at Main.hs &lt;&lt;line 3&gt;&gt;
  Expected type: DataStore a -&gt; IO a
    Actual type: DataStore a0 -&gt; IO a0
  In the `storageInterface' field of a record
    [...]
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>My original minimal example was to minimal.  Some further experimentation shows that the problem arises when I need to load the interface in an the IO monad so I can read the command line options.  I've updated the example to include that issue.  Knowing this I may be able to code around it.</p>
  
  <p>Interesting GHCI tells me that the results of a function of type <code>IO (DataStore a -&gt; IO a)</code> is <code>DataStore GHC.Prim.Any -&gt; IO GHC.Prim.Any</code> which is not what I expected.</p>