title: Parallel mapM on Repa arrays
body: <p>In my recent <a href="https://github.com/bgamari/mixture-model" rel="noreferrer">work</a> with <code>Gibbs sampling</code>, I've been making great use of the <a href="http://hackage.haskell.org/packages/archive/rvar/0.2.0.1/doc/html/Data-RVar.html" rel="noreferrer"><code>RVar</code></a> which, in my view, provides a near ideal interface to random number generation.  Sadly, I've been unable to make use of Repa due to the inability to use monadic actions in maps.</p>
  
  <p>While clearly monadic maps can't be parallelized in general, it seems to me that <code>RVar</code> may be at least one example of a monad where effects can be safely parallelized (at least in principle; I'm not terribly familiar with the inner workings of <code>RVar</code>). Namely, I want to write something like the following,</p>
  
  <pre><code>drawClass :: Sample -&gt; RVar Class
  drawClass = ...
  
  drawClasses :: Array U DIM1 Sample -&gt; RVar (Array U DIM1 Class)
  drawClasses samples = A.mapM drawClass samples
  </code></pre>
  
  <p>where <code>A.mapM</code> would look something like,</p>
  
  <pre><code>mapM :: ParallelMonad m =&gt; (a -&gt; m b) -&gt; Array r sh a -&gt; m (Array r sh b)
  </code></pre>
  
  <p>While clearly how this would work depends crucially on the implementation of <code>RVar</code> and its underlying <code>RandomSource</code>, in principle one would think that this would involve drawing a new random seed for each thread spawned and proceeding as usual.</p>
  
  <p>Intuitively, it seems that this same idea might generalize to some other monads.</p>
  
  <p>So, my question is: Could one construct a class <code>ParallelMonad</code> of monads for which effects can be safely parallelized (presumably inhabited by, at the least, <code>RVar</code>)? </p>
  
  <p>What might it look like? What other monads might inhabit this class? Have others considered the possibility of how this might work in Repa?</p>
  
  <p>Finally, if this notion of parallel monadic actions can't be generalized, does anyone see any nice way to make this work in the specific case of <code>RVar</code> (where it would be very useful)? Giving up <code>RVar</code> for parallelism is a very difficult trade-off.</p>