<div>This monad&nbsp;seems to be basically the same as&nbsp;Prompt; see <a href="http://www.haskell.org/pipermail/haskell-cafe/2007-November/034830.html">http://www.haskell.org/pipermail/haskell-cafe/2007-November/034830.html</a>, the only difference I see is that&nbsp;Prompt allows the return value&#39;s type to be&nbsp;based on the&nbsp;request&nbsp;instead of forcing everything to be wrapped in&nbsp;a single result type.
</div>
<div>&nbsp;</div>
<div>You implemented the monad operations exactly the same as Prompt, and your bind operator suffers from the same quadratic behavior problem that was pointed out in that thread.</div>
<div>&nbsp;</div>
<div>As was pointed out there, what you are doing is turning the potential side effects of your computations into a term algebra, which allows you to write different &quot;interpretation&quot; functions to use when running the calculation (the reqf passed to runSC).&nbsp; As far as I can tell, this pattern is general enough to implement any computation, so it&#39;s not surprising that you found it possible to use it to implement parallel computation.
</div>
<div>&nbsp;</div>
<div>As an example, here&#39;s the State monad&nbsp;implemented in terms of SCalc:</div>
<div>&nbsp;</div>
<div>&gt; data StateReq s = Get | Put s</div>
<div>&gt; get :: SCalc (StateReq s) s s</div>
<div>&gt; get = SCStage Get return</div>
<div>&gt; put :: s -&gt;&nbsp;SCalc (StateReq s) s ()</div>
<div>&gt; put s = SCStage (Put s) (const $ return ())</div>
<div>&gt;</div>
<div>&gt; runState :: SCalc (StateReq a) s b -&gt; s -&gt; (a, s)</div>
<div>&gt; runState (SCResult v)&nbsp;s = (v, s)</div>
<div>&gt; runState (SCStage Get cont) s = runState (cont s) s</div>
<div>&gt; runState (SCStage (Put s) cont)&nbsp;_ = runState (cont s) s<br>&nbsp;</div>
<div>I think it&#39;s a useful pattern and I definitely am getting a lot of use out of Prompt in my code.&nbsp; But I&#39;m trying to figure out the best way to&nbsp;eliminate the quadratic behavior of (&gt;&gt;=) that is exhibited by, for example:
</div>
<div>&nbsp;</div>
<div>foldl1 (&gt;&gt;=) $ take 100 $ repeat $ (\x -&gt; put (x+1) &gt;&gt;= get) $ 0</div>
<div>&nbsp;</div>
<div>The only way I&#39;ve found so far is to wrap Prompt inside of ContT which solves the problem in much the same way that difference lists (newtype DList a = [a] -&gt; [a]) solve the problem of quadratic time append for lists.
</div>
<div>&nbsp;</div>
<div>&nbsp; -- ryan</div>