<div>I&#39;d also like to point out that Chris did this with 165 lines of code--including comments and whitespace! If you drop the whitespace and comments, it&#39;s only 91 lines!</div><div><br></div>/jve<br>
<br><br><div class="gmail_quote">On Mon, Mar 9, 2009 at 4:54 PM, ChrisK <span dir="ltr">&lt;<a href="mailto:haskell@list.mightyreason.com">haskell@list.mightyreason.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello,<br>
<br>
  As a side effect of the discussion of the new C++ future/promise features at <a href="http://lambda-the-ultimate.org/node/3221" target="_blank">http://lambda-the-ultimate.org/node/3221</a> I have implemented a Haskell package called &quot;future&quot; at<br>

<br>
<a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/future" target="_blank">http://hackage.haskell.org/cgi-bin/hackage-scripts/package/future</a><br>
<br>
This ought to do what C++ standard futures/promises do, plus a bit more.  The main operation is<br>
<br>
&gt; forkPromise :: IO a -&gt; IO (Promise a)<br>
<br>
This sets the &quot;IO a&quot; operation running in a fresh thread.  The eventual result can be accessed in many ways (non-blocking, blocking, blocking with timeout).<br>
<br>
&gt; let one :: Int; one = 1<br>
&gt; p &lt;- forkPromise (return (one+one))<br>
&gt; x &lt;- get<br>
&gt; y &lt;- wait<br>
<br>
x is an Int with value 2.<br>
y is an (Either SomeException Int) with value (Right 2).<br>
<br>
The useful thing about futures, as opposed to various IVar packages, is handling  the case where the forked operation ends with an exception.  The exception becomes the return value of the promise.  The &quot;get&quot; operation rethrows it, the &quot;wait&quot; operation returns it as (Left ...).<br>

<br>
There is also an &quot;abort&quot; command to kill a promise.  The dead promise may then have an exceptions as its value.<br>
<br>
The &quot;plus a bit more&quot; than C++ is the nonblocking &quot;addTodo&quot; feature.  This takes a continuation function from the &quot;Either SomeException a&quot; to an IO operation. These continuation functions get queued and they are run immediately after the the forked operation completes.  Once completed any new &quot;addTodo&quot; continuations run immediately.<br>

<br>
These continuations allow you to race a list of action and take the first one done, or to collect the answers as they complete into a Chan.  Both of those options are demonstrated in Future.hs as racePromises and forkPromises.<br>

<br>
It should be safe to use &quot;unsafePerformIO . get&quot; or &quot;unsafePeformIO . wait&quot; to get lazy access to the result, which is itself immutable once set.<br>
<br>
Cheers,<br>
  Chris<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>