<?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; normal form</title>
	<atom:link href="http://conal.net/blog/tag/normal-form/feed" rel="self" type="application/rss+xml" />
	<link>http://conal.net/blog</link>
	<description>Inspirations &#38; experiments, mainly about denotative/functional programming in Haskell</description>
	<lastBuildDate>Thu, 25 Jul 2019 18:15:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.17</generator>
	<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2F&amp;language=en_US&amp;category=text&amp;title=Conal+Elliott&amp;description=Inspirations+%26amp%3B+experiments%2C+mainly+about+denotative%2Ffunctional+programming+in+Haskell&amp;tags=blog" type="text/html" />
	<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><![CDATA[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[I submitted a paper Simply efficient functional reactivity to ICFP 2008. Abstract: 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 [&#8230;]]]></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 -->
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=22&amp;md5=dee648d38383131c59963d5b9b4c4b93"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/simply-efficient-functional-reactivity/feed</wfw:commentRss>
		<slash:comments>33</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fsimply-efficient-functional-reactivity&amp;language=en_GB&amp;category=text&amp;title=Simply+efficient+functional+reactivity&amp;description=I+submitted+a+paper+Simply+efficient+functional+reactivity+to+ICFP+2008.+Abstract%3A+Functional+reactive+programming+%28FRP%29+has+simple+and+powerful+semantics%2C+but+has+resisted+efficient+implementation.+In+particular%2C+most+past...&amp;tags=applicative+functor%2Ccontinuous%2Cdiscrete%2Cevents%2CFRP%2Cfunctional+reactive+programming%2Cfunctor%2Cfuture+value%2Cicfp%2Cimplementation%2CjoinMaybes%2Cmonad%2Cmonoid%2Cmulti-threading%2Cnormal+form%2Cpaper%2Creactive+behavior%2Creactive+value%2Csemantics%2Ctime%2Ctype+class%2Ctype+class+morphism%2Ctype+composition%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Reactive normal form</title>
		<link>http://conal.net/blog/posts/reactive-normal-form</link>
		<comments>http://conal.net/blog/posts/reactive-normal-form#comments</comments>
		<pubDate>Fri, 25 Jan 2008 23:00:59 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[future value]]></category>
		<category><![CDATA[normal form]]></category>
		<category><![CDATA[reactive value]]></category>
		<category><![CDATA[reactivity]]></category>

		<guid isPermaLink="false">http://conal.net/blog/posts/reactive-normal-form/</guid>
		<description><![CDATA[The post &#8220;Reactive values from the future&#8221; presented a simple formulation of functional events and reactive values, built on a functional notion of future values. In the current post, I&#8217;ll describe some of the implementation of events and reactive values*. Reactive values Switching As mentioned previously, the stepper function corresponds directly to the representation of [&#8230;]]]></description>
				<content:encoded><![CDATA[<!--

Title: Reactive normal
tags: reactivity, future values, normal form, reactive values, events

-->

<!-- references -->

<!-- teaser -->

<p>The post <a href="http://conal.net/blog/posts/reactive-values-from-the-future/" title="Blog post: &quot;Reactive values from the future&quot;">&#8220;Reactive values from the future&#8221;</a> presented a simple formulation of functional <em>events</em> and <em>reactive values</em>, built on a functional notion of <em><a href="http://conal.net/blog/posts/future-values/" title="Blog post: &quot;Future values&quot;">future values</a></em>.  In the current post, I&#8217;ll describe some of the implementation of events and reactive values*.</p>

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

<h2>Reactive values</h2>

<h3>Switching</h3>

<p>As mentioned <a href="http://conal.net/blog/posts/reactive-values-from-the-future/" title="Blog post: &quot;Reactive values from the future&quot;">previously</a>, the <code>stepper</code> function corresponds directly to the representation of <code>Reactive</code>.</p>

<pre><code>data Reactive a = Stepper a (Event a)

stepper :: a -&gt; Event a -&gt; Reactive a
stepper = Stepper
</code></pre>

<p>The representation of all other functions must therefore produce their result in terms of <code>stepper</code>, which thereby serves as a kind of <em>reactive normal form</em> (RNF).  The implementation can be understood as a set of rewrite rules that convert arbitrary reactive expressions into RNF.  (These rewrite rules are all provably correct based on a simple semantics of events and reactive values.)</p>

<p>The more general switching form can be expressed in terms of <code>switcher</code> and (monadic) <code>join</code>:</p>

<pre><code>switcher :: Reactive a -&gt; Event (Reactive a) -&gt; Reactive a
r `switcher` er = join (r `stepper` er)
</code></pre>

<p>We&#8217;ll see below how to reduce <code>join</code> to a <code>stepper</code>.</p>

<h3>Functor</h3>

<p>As a simple example, <code>fmap f</code> applies a function <code>f</code> to a reactive value pointwise, which is equivalent to applying <code>f</code> to the initial value and to each occurrence value.</p>

<pre><code>instance Functor Reactive where
  fmap f (a `Stepper` e) = f a `stepper` fmap f e
</code></pre>

<p>The <code>Event</code> functor is also easily defined.  <a href="http://conal.net/blog/posts/reactive-values-from-the-future/" title="Blog post: &quot;Reactive values from the future&quot;">Recall</a> that an event is a future reactive value:</p>

<pre><code>newtype Event = Future (Reactive a)
</code></pre>

<p>We want to map a given function over that reactive value, which we can do by combining <code>fmap</code> on <code>Future</code> with <code>fmap</code> on <code>Reactive</code>.</p>

<pre><code>instance Functor Event where
  fmap f (Event fut) = Event ((fmap.fmap) f fut)
</code></pre>

<h3>Applicative</h3>

<p>The <code>Functor</code> definitions for <code>Reactive</code> and <code>Event</code> are straightforward, because the <code>Stepper</code> structure is easily preserved.  A more challenging case is <code>Applicative</code>.</p>

<pre><code>instance Applicative Reactive where ... 
</code></pre>

<p>First the easy part.  A pure value <code>a</code> becomes reactive by using <code>a</code> as the initial value and <code>mempty</code> as the (never-occuring) change event:</p>

<pre><code>  pure a = a `stepper` mempty
</code></pre>

<p>Consider next applying a reactive function to a reactive argument:</p>

<pre><code>  rf@(f `Stepper` Event futf) &lt;*&gt; rx@(x `Stepper` Event futx) =
    f x `stepper` Event fut
   where ...
</code></pre>

<p>The initial value is <code>f x</code>, and the change event occurs each time <em>either</em> the function or the argument changes.  If the function changes first, we will (in the future) apply a new reactive function to an old argument:</p>

<pre><code>           fmap ( rf' -&gt; rf' &lt;*&gt; rx) futf
</code></pre>

<p>Similarly, if the argument changes first, we will apply an old reactive function and a new argument:</p>

<pre><code>           fmap ( rx' -&gt; rf &lt;*&gt; rx') futx
</code></pre>

<p>Combining these two futures as alternatives:</p>

<pre><code>...
     fut = fmap ( rf' -&gt; rf' &lt;*&gt; rx ) futf `mappend`
           fmap ( rx' -&gt; rf  &lt;*&gt; rx') futx
</code></pre>

<p>A wonderful thing about this <code>(&lt;*&gt;)</code> definition for Reactive is that it automatically caches the previous value of the function or argument when the argument or function changes.  This caching property is especially handy in nested applications of <code>(&lt;*&gt;)</code>, which can arise either explicitly or through <code>liftA2</code>, <code>liftA3</code>, etc.  Consider <code>u == liftA2 f r s</code> or, equivalently, <code>u == pure f &lt;*&gt; r &lt;*&gt; s</code>, where <code>r</code> and <code>s</code> are reactive values, with initial values <code>r0</code> and <code>s0</code>, respectively.  (The <code>(&lt;*&gt;)</code> operator is associates to the left.)  The initial value <code>u0</code> of <code>u</code> is <code>f r0 s0</code>.  If <code>r</code> changes from <code>r0</code> to <code>r1</code>, then the new value of <code>pure f &lt;*&gt; r</code> will be <code>f r1</code>, which then gets applied to <code>s0</code>, i.e., <code>u1 == f r1 s0</code>.  However, if instead <code>s</code> changes from <code>s0</code> to <code>s1</code>, then <code>u1 == f r0 s1</code>.  In this latter case, the old value <code>f r0</code> of <code>pure f &lt;*&gt; r</code> is passed on without having to be recomputed.  The savings is significant for functions that do some work based partial applications.</p>

<h3>Monad</h3>

<p>Above we saw a simple definition of <code>switcher</code>:</p>

<pre><code>r `switcher` er = join (r `stepper` er)
</code></pre>

<p>How does <code>join</code> work for <code>Reactive</code>?  Rather than using the standard definition of <code>join</code> in terms of <code>(&gt;&gt;=)</code>, let&#8217;s do the reverse:</p>

<pre><code>r &gt;&gt;= h = joinR (fmap h r)
</code></pre>

<p>where <code>joinR</code> is <code>join</code> for reactive values:</p>

<pre><code>joinR :: Reactive (Reactive a) -&gt; Reactive a
</code></pre>

<p>Defining <code>joinR</code> is similar to <code>(&lt;*&gt;)</code> above.  For a reactive-reactive value <code>rr</code>, either <code>rr</code> or the reactive value that is the initial value of <code>rr</code> changes first.</p>

<pre><code>joinR ((a `Stepper` Event fut) `Stepper` e'@(Event fut')) =
  a `stepper` Event fut''
 where
   -- If fut  arrives first, switch and continue waiting for e'.
   -- If fut' arrives first, abandon fut and keep switching with new
   -- reactive values from fut'.
   fut'' = fmap (`switcher` e') fut `mappend` fmap join fut'
</code></pre>

<h2>Dynamic optimization</h2>

<p>Operations on events and reactive values dynamically re-optimize themselves.  For instance, the reactive value <code>fmap f r</code> is known to quiesce (stop changing) when <code>r</code> quiesces, which is knowable because futures have a special representation for the future that never comes.  (Really, only easy common cases are optimized.  An example of a hard-to-detect never-occuring event would be filtering out first the odd numbers and then the even numbers from an <code>Int</code>-valued event.)  Similarly, the reactive value <code>liftA2 f r s</code> quiesces once <em>both</em> <code>r</code> and <code>s</code> quiesce.  Once reactive values quiesce, no more computational resource is devoted to them.</p>

<h2>Events</h2>

<p>The definitions above are mainly for reactive values.  What about functional events?</p>

<h3>Monoid</h3>

<p>Events form a monoid: <code>mempty</code> is the never-occurring event, which is represented by the never-occuring future, while <code>e `mempty` e'</code> is an event consisting of all occurrences from <code>e</code> and <code>e'</code>.</p>

<pre><code>instance Monoid (Event a) where
  mempty  = Event mempty
  Event fut `mappend` Event fut' = Event (fut `merge` fut')
</code></pre>

<p>This <code>merge</code> operation combines two <code>Future</code> streams into one.</p>

<pre><code>merge :: Future (Reactive a) -&gt; Future (Reactive a) -&gt; Future (Reactive a)
Never `merge` fut   = fut
fut   `merge` Never = fut
u     `merge` v     =
  (onFut (`merge` v) &lt;$&gt; u) `mappend` (onFut (u `merge`) &lt;$&gt; v)
 where
   onFut f (a `Stepper` Event t') = a `stepper` Event (f t')
</code></pre>

<p>The <code>mappend</code> used in the definition of <code>merge</code> is from <code>Future</code>.  It picks the earlier future and abandons the other.  Thus the first of the two events to occur will contribute an occurrence value (<code>a</code>), leaving a residual event, which is combined with the not-yet-occurring event.</p>

<h3>Applicative and Monad</h3>

<p>The <code>Applicative</code> and <code>Monad</code> instances for events are like those for lists, and unlike those for reactive values (given above).  For instance, the occurrences of <code>ef &lt;*&gt; ex</code> consist of <code>f x</code> for all occurrences <code>f</code> of <code>ef</code> and occurrences <code>x</code> of <code>ex</code>.  See the <a href="http://darcs.haskell.org/packages/reactive/doc/html/src/Data-Reactive.html">source code</a> for details.</p>

<h2>Conclusion</h2>

<p>I&#8217;m happy with the simplicity of this new functional formulation of events and reactive values.  While most previous FRP implementations used polling, Reactive is change-driven and so has the can be much more efficient.  It also uses a dynamically self-optimizing representation.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=10&amp;md5=dfc295c91e045cfb95ed14f766d2b737"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/reactive-normal-form/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Freactive-normal-form&amp;language=en_GB&amp;category=text&amp;title=Reactive+normal+form&amp;description=The+post+%26%238220%3BReactive+values+from+the+future%26%238221%3B+presented+a+simple+formulation+of+functional+events+and+reactive+values%2C+built+on+a+functional+notion+of+future+values.+In+the+current+post%2C+I%26%238217%3Bll...&amp;tags=events%2Cfuture+value%2Cnormal+form%2Creactive+value%2Creactivity%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
