title: Signature for Generic Mutable Vector function
body: <p>I'm trying to write a generic vector function that takes an immutable vector and returns an immutable vector, but operates on a (temporary) mutable vector. A simple example that demonstrates my problem is:</p>
  
  <pre><code>import Data.Vector.Generic as V
  import Control.Monad.ST
  import qualified Data.Vector.Mutable as M
  
  test :: (Vector v r) =&gt; v r -&gt; v r
  test v = runST $ do
    x &lt;- V.thaw v
    -- modify the mutable vector x, code elided
    let s = V.sum x
    M.write x 0 s
    -- continue to modify the mutable vector x, code elided
    V.unsafeFreeze x
  </code></pre>
  
  <p>This function does not compile because GHC cannot deduce <code>(Vector (Mutable v s) r)</code>. I realize this is a problem, but due to the higher-rank type of <code>runST</code>,</p>
  
  <pre><code>runST :: (forall s. ST s a) -&gt; a
  </code></pre>
  
  <p>I can't just add this constraint to <code>test</code>, nor could I figure out how to appropriately use explicit forall'd variables. How can I make <code>test</code> compile?</p>