<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Conal Elliott &#187; implementation</title>
	<atom:link href="http://conal.net/blog/tag/implementation/feed/" rel="self" type="application/rss+xml" />
	<link>http://conal.net/blog</link>
	<description>Inspirations &#38; experiments, mainly about denotational programming in Haskell</description>
	<lastBuildDate>Fri, 30 Jul 2010 02:56:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simply efficient functional reactivity</title>
		<link>http://conal.net/blog/posts/simply-efficient-functional-reactivity/</link>
		<comments>http://conal.net/blog/posts/simply-efficient-functional-reactivity/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 22:27:43 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[continuous]]></category>
		<category><![CDATA[discrete]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[FRP]]></category>
		<category><![CDATA[functional reactive programming]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[future value]]></category>
		<category><![CDATA[icfp]]></category>
		<category><![CDATA[implementation]]></category>
		<category><![CDATA[joinMaybes]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[monoid]]></category>
		<category><![CDATA[multi-threading]]></category>
		<category><![CDATA[normal form]]></category>
		<category><![CDATA[paper]]></category>
		<category><![CDATA[reactive behavior]]></category>
		<category><![CDATA[reactive value]]></category>
		<category><![CDATA[semantics]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[type class]]></category>
		<category><![CDATA[type class morphism]]></category>
		<category><![CDATA[type composition]]></category>

		<guid isPermaLink="false">http://conal.net/blog/posts/simply-efficient-functional-reactivity/</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Simply efficient functional reactivity

Tags: applicative functor, continuous, discrete, event, FRP, functional reactive programming, functor, future value, icfp, implementation, joinMaybes, monad, monoid, type class morphism, multi-threading, normal form, paper, reactive behavior, reactive value, semantics, time, type class, type composition

URL: http://conal.net/blog/posts/simply-efficient-functional-reactivity/

-->

<!-- references -->

<!-- teaser -->

<p>I submitted a paper <em><a href="http://conal.net/papers/simply-reactive" title="Paper: &quot;Simply efficient functional reactivity&quot;">Simply efficient functional reactivity</a></em> to <a href="http://www.icfpconference.org/icfp2008" title="ICFP 2008 conference page">ICFP 2008</a>.</p>

<p><strong>Abstract:</strong></p>

<blockquote>
  <p>Functional reactive programming (FRP) has simple and powerful semantics, but has resisted efficient implementation.  In particular, most past implementations have used demand-driven sampling, which accommodates FRP&#8217;s continuous time semantics and fits well with the nature of functional programming.  Consequently, values are wastefully recomputed even when inputs don&#8217;t change, and reaction latency can be as high as the sampling period.</p>
  
  <p>This paper presents a way to implement FRP that combines data- and demand-driven evaluation, in which values are recomputed only when necessary, and reactions are nearly instantaneous.  The implementation is rooted in a new simple formulation of FRP and its semantics and so is easy to understand and reason about.</p>
  
  <p>On the road to efficiency and simplicity, we&#8217;ll meet some old friends (monoids, functors, applicative functors, monads, morphisms, and improving values) and make some new friends (functional future values, reactive normal form, and concurrent &#8220;unambiguous choice&#8221;).</p>
</blockquote>

<!--
**Edits**:

* 2008-02-09: just fiddling around
-->

<!-- without a comment or something here, the last item above becomes a paragraph -->
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/simply-efficient-functional-reactivity/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Future values via multi-threading</title>
		<link>http://conal.net/blog/posts/future-values-via-multi-threading/</link>
		<comments>http://conal.net/blog/posts/future-values-via-multi-threading/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 05:15:35 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[future value]]></category>
		<category><![CDATA[implementation]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[monoid]]></category>
		<category><![CDATA[multi-threading]]></category>

		<guid isPermaLink="false">http://conal.net/blog/posts/future-values-part-two-a-multi-threaded-implementation/</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title Future values via multi-threading

Tags: future values, multi-threading, implementation, functors, applicative functors, monads, monoids

-->

<!-- References -->

<h2>Future values</h2>

<p>A <a href="http://conal.net/blog/posts/future-values/" title="Blog post: &quot;Future values&quot;">previous post</a> described future values (or simply &#8220;futures&#8221;), which are values depend on information from the future, e.g., from the real world. There I gave a simple denotational semantics for future values as time/value pairs.  This post describes the multi-threaded implementation of futures in <a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">Reactive</a>&#8217;s <code>Data.Reactive</code> module.</p>

<p><span id="more-8"></span></p>

<h2>A simple representation</h2>

<p>Futures are represented as actions that deliver a value.  These actions are required to yield the same answer every time.   Having a special representation for the future that never arrives (<code>mempty</code>) will allow for some very important optimizations later.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Future a = Future <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a<span style="color: green;">&#41;</span> | Never</pre>
</div></p>

<p>A value can be &#8220;forced&#8221; from a future.  If the value isn&#8217;t yet available, forcing will block.</p>

<p><div>
<pre class="haskell">force :: Future a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a
force <span style="color: green;">&#40;</span>Future io<span style="color: green;">&#41;</span> = io
force Never       = hang  <span style="color: #5d478b; font-style: italic;">-- block forever</span></pre>
</div></p>

<h2>Threads and synchronization</h2>

<p>The current implementation of futures uses <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent.html">Concurrent Haskell</a>&#8217;s <code>forkIO</code>
(fork a thread) and <code>MVar</code>s (synchronized communication variables).</p>

<p>Except for trivial futures (<code>pure</code>/<code>return</code> and <code>mempty</code>), the action in a future will simply be reading an MVar.  Internal to the implementation:</p>

<p><div>
<pre class="haskell">newFuture :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span>Future a, a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
newFuture = <span style="color: #050; font-weight: bold;">do</span> v &lt;- newEmptyMVar
               <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span>Future <span style="color: green;">&#40;</span>readMVar v<span style="color: green;">&#41;</span>, putMVar v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The MVar is written to as the final step of a forked thread.  Importantly, it is to be written only once.  (It&#8217;s really an IVar.)</p>

<p><div>
<pre class="haskell">future :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a -&gt; Future a
future mka = unsafePerformIO $
             <span style="color: #050; font-weight: bold;">do</span> <span style="color: green;">&#40;</span>fut,sink<span style="color: green;">&#41;</span> &lt;- newFuture
                forkIO $ mka &gt;&gt;= sink
                <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> fut</pre>
</div></p>

<p>Note that the actual value is computed just once, and is accessed cheaply.</p>

<h2>Functor, Applicative, and Monad</h2>

<p>The <code>Functor</code>, <code>Applicative</code>, and <code>Monad</code> instances are defined easily in terms of <code>future</code> and the corresponding instances for <code>IO</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #833; font-weight: bold;">Functor</span></a> Future <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>Future get<span style="color: green;">&#41;</span> = future <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f get<span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> _ Never        = Never
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a> Future <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a                      = Future <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a<span style="color: green;">&#41;</span>
  Future getf &lt;*&gt; Future getx = future <span style="color: green;">&#40;</span>getf &lt;*&gt; getx<span style="color: green;">&#41;</span>
  _           &lt;*&gt; _           = Never
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="color: #833; font-weight: bold;">Monad</span></a> Future <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a>            = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>
  Future geta &gt;&gt;= h = future <span style="color: green;">&#40;</span>geta &gt;&gt;= force . h<span style="color: green;">&#41;</span>
  Never       &gt;&gt;= _ = Never</pre>
</div></p>

<h2>Monoid &#8212; racing</h2>

<p>The remaining class to implement is <code>Monoid</code>.  The <code>mempty</code> method is <code>Never</code>.  The <code>mappend</code> method is to select the earlier of two futures, which is implemented by having the two futures race to extrat a value:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span>Future a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>  = Never
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> = race
&nbsp;
race :: Future a -&gt; Future a -&gt; Future a</pre>
</div></p>

<p>Racing is easy in the case either is <code>Never</code>:</p>

<p><div>
<pre class="haskell">Never `race` b     = b
a     `race` Never = a</pre>
</div></p>

<p>Otherwise, spin a thread for each future.  The winner kills the loser.</p>

<p><div>
<pre class="haskell">a `race` b = unsafePerformIO $
             <span style="color: #050; font-weight: bold;">do</span> <span style="color: green;">&#40;</span>c,sink<span style="color: green;">&#41;</span> &lt;- newFuture
                <span style="color: #050; font-weight: bold;">let</span> run fut tid = forkIO $ <span style="color: #050; font-weight: bold;">do</span> x &lt;- force fut
                                              killThread tid
                                              sink x
                <span style="color: #050; font-weight: bold;">mdo</span> ta &lt;- run a tb
                    tb &lt;- run b ta
                    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
                <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> c</pre>
</div></p>

<h2>The problem</h2>

<p>This last piece of the implementation can fall short of the semantics.  For <code>a `mappend` b</code>, we might get <code>b</code> instead of <code>a</code> even if they&#8217;re available simultaneously.  It&#8217;s even possible to get the later of the two if they&#8217;re nearly simultaneous.</p>

<p><em>Edit (2008-02-02)</em>: although simultaneous <em>physical</em> events are extremely unlikely, futures are compositional, so it&#8217;s easy to construct two distinct but simultaneous futures in terms of on a common physical future.</p>

<p>What will it take to get deterministic semantics for <code>a `mappend` b</code>?  Here&#8217;s an idea: include an  explicit time in a future.  When one future happens with a time <code>t</code>, query whether the other one occurs by the same time.  What does it take to support this query operation?</p>

<h2>A simpler implementation</h2>

<p>In this implementation, futures (other than <code>Never</code>) hold actions that typically read an MVar that is written only once.  They could instead simply hold lazy values.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Future a = Future a | Never</pre>
</div></p>

<p>Forcing a future evaluates to weak head normal form (WHNF).</p>

<p><div>
<pre class="haskell">force :: Future a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a
force <span style="color: green;">&#40;</span>Future a<span style="color: green;">&#41;</span> = a `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:seq"><span style="font-weight: bold;">seq</span></a>` <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> a
force Never      = hang</pre>
</div></p>

<p>Making new future would work almost identically, just adding an <code>unsafePerformIO</code>.</p>

<p><div>
<pre class="haskell">newFuture :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span>Future a, a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
newFuture = 
  <span style="color: #050; font-weight: bold;">do</span> v &lt;- newEmptyMVar
     <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span>Future <span style="color: green;">&#40;</span>unsafePerformIO $ readMVar v<span style="color: green;">&#41;</span>, putMVar v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The <code>Functor</code>, <code>Applicative</code>, and <code>Monad</code> instances simplify considerably, using function application directly, instead of the corresponding methods from the <code>Functor</code>, <code>Applicative</code>, and <code>Monad</code> instances of <code>IO</code>.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #833; font-weight: bold;">Functor</span></a> Future <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>Future a<span style="color: green;">&#41;</span> = Future <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> _ Never      = Never
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a> Future <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a                = Future a
  Future f &lt;*&gt; Future x = Future <span style="color: green;">&#40;</span>f x<span style="color: green;">&#41;</span>
  _        &lt;*&gt; _        = Never
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="color: #833; font-weight: bold;">Monad</span></a> Future <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a>         = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>
  Future a &gt;&gt;= h = h a
  Never    &gt;&gt;= _ = Never</pre>
</div></p>

<p>The <code>Monoid</code> instance is completely unchanged.</p>

<p>How does this implementation compare with the one above?</p>

<ul>
<li>It&#8217;s simpler, as stated.</li>
<li>It&#8217;s a bit more efficient.  Each MVar is only read once.  Also, the <code>Functor</code>, <code>Applicative</code>, and <code>Monad</code> instances no longer create threads and MVars.</li>
<li>The type statically enforces that forcing a future always gives the same result.</li>
<li>Forcing a future reduces the value to WHNF and so is probably a little less lazy than the  implementation above.</li>
</ul>

<p>I&#8217;ve tried out this implementation and found that it intermittently crashes some examples that work fine in the <code>IO</code> version.  I have no idea why, and I&#8217;d be very interested in ideas about it or anything else in this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/future-values-via-multi-threading/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
