<?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; reactivity</title>
	<atom:link href="http://conal.net/blog/tag/reactivity/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>Blending continuity into reactive values</title>
		<link>http://conal.net/blog/posts/blending-continuity-into-reactive-values</link>
		<comments>http://conal.net/blog/posts/blending-continuity-into-reactive-values#comments</comments>
		<pubDate>Fri, 25 Jan 2008 23:13:46 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[continuous]]></category>
		<category><![CDATA[discrete]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[reactive behavior]]></category>
		<category><![CDATA[reactivity]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[type composition]]></category>

		<guid isPermaLink="false">http://conal.net/blog/posts/blending-continuity-into-reactive-values/</guid>
		<description><![CDATA[This post continues from &#8220;Reactive values from the future&#8221; and &#8220;Reactive normal form&#8221;. Fran/FRP reactive behaviors could change continuously, while reactive values change only discretely. The Reactive library keeps continuity orthogonal to reactivity. To combine continuity and reactivity, simply compose reactivity with a non-reactive type of functions of time. One could literally use Reactive (Time [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Blending continuity into reactive values

Tags: reactivity, discrete, continuous, reactive behaviors, optimization, type composition, time

-->

<!-- references -->

<!-- teaser -->

<p>This post continues from <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> and <a href="http://conal.net/blog/posts/reactive-normal-form/" title="Blog post: &quot;Reactive normal form&quot;">&#8220;Reactive normal form&#8221;</a>.</p>

<p>Fran/FRP reactive behaviors could change <em>continuously</em>, while reactive values change only <em>discretely</em>.  The <a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">Reactive</a> library keeps continuity orthogonal to reactivity.  To combine continuity and reactivity, simply compose reactivity with a non-reactive type of functions of time.</p>

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

<p>One could literally use <code>Reactive (Time -&gt; a)</code>, but Reactive also includes a more optimizable version as the module <a href="http://darcs.haskell.org/packages/reactive/doc/html/Data-Fun.html" title="Documentation for module Data.Fun in the Reactive library"><code>Data.Fun</code></a>.  The denotation of <code>Fun t a</code> is simply <code>t -&gt; a</code>, but it has a constructor (<code>K</code>) for constant-valued functions, which occur frequently.</p>

<pre><code>data Fun t a = K a | Fun (t -&gt; a)
</code></pre>

<p>As with the other types in Reactive, most of the operations on <code>Fun</code> values are in the form of instances of standard type classes, specifically <code>Monoid</code>, <code>Functor</code>, <code>Applicative</code>, <code>Monad</code>, and <code>Arrow</code>.  As an example of optimization,</p>

<pre><code>instance Functor (Fun t) where
  fmap f (K   a) = K   (f a)
  fmap f (Fun g) = Fun (f.g)
</code></pre>

<p><code>Data.Reactive</code> defines reactive behaviors very simply as a <a href="http://haskell.org/haskellwiki/TypeCompose" title="Wiki page for the TypeCompose library">type composition</a> of <code>Reactive</code> and <code>Fun Time</code>:</p>

<pre><code>type Time = Double

type ReactiveB = Reactive :. Fun Time
</code></pre>

<p>Wrapping the combination in a <a href="http://haskell.org/haskellwiki/TypeCompose" title="Wiki page for the TypeCompose library">type composition</a> (<code>g :. f</code>) gives <code>Functor</code> and <code>Applicative</code> instances for reactive behaviors that combine the corresponding instances for <code>Reactive</code> and <code>Fun Time</code>.</p>

<p>Combining reactive values and non-reactive functions in this way, we can create implementations that mix data-driven (push) and demand-driven (pull) evaluation.  The reactive value part is data-driven, resulting in a time function.  That time function can then be polled until a new time function is pushed for further polling.  As an important optimization, only poll when the time function is really time-varying (made with the <code>Fun</code> constructor).  If it&#8217;s constant (made with <code>K</code>), then output the value just once and wait until the next push.</p>

<p>I&#8217;m not sure whether this elegant composition of orthogonal notions is really a good idea in practice.  A benefit is the extra static typing and ability to use of the two types independently.  A drawback is that the operations on reactive values have to be rewrapped and perhaps tweaked for convenient use directly on reactive behaviors.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=11&amp;md5=dad114db31a6501c908baa7892572653"><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/blending-continuity-into-reactive-values/feed</wfw:commentRss>
		<slash:comments>7</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%2Fblending-continuity-into-reactive-values&amp;language=en_GB&amp;category=text&amp;title=Blending+continuity+into+reactive+values&amp;description=This+post+continues+from+%26%238220%3BReactive+values+from+the+future%26%238221%3B+and+%26%238220%3BReactive+normal+form%26%238221%3B.+Fran%2FFRP+reactive+behaviors+could+change+continuously%2C+while+reactive+values+change+only+discretely.+The+Reactive+library+keeps+continuity...&amp;tags=continuous%2Cdiscrete%2Coptimization%2Creactive+behavior%2Creactivity%2Ctime%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>
		<item>
		<title>Reactive values from the future</title>
		<link>http://conal.net/blog/posts/reactive-values-from-the-future</link>
		<comments>http://conal.net/blog/posts/reactive-values-from-the-future#comments</comments>
		<pubDate>Fri, 25 Jan 2008 21:56:36 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[future value]]></category>
		<category><![CDATA[reactive value]]></category>
		<category><![CDATA[reactivity]]></category>

		<guid isPermaLink="false">http://conal.net/blog/posts/reactive-values-from-the-future/</guid>
		<description><![CDATA[I&#8217;ve gotten interested lately in revisiting functional reactive programming (FRP). I was never entirely satisfied with the semantics or implementation in my original Fran formulation or in its successors. Over the last year, I&#8217;ve enjoyed getting more interface and functionality from standard type classes, and I&#8217;ve realized that quite a lot of FRP could be [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Reactive values from the future

Tags: reactivity, future values, reactive values, events

-->

<!-- references -->

<!-- teaser -->

<p>I&#8217;ve gotten interested lately in revisiting functional reactive programming (FRP).  I was never entirely satisfied with the semantics or implementation in my original <a href="http://conal.net/Fran" title="Fran -- functional reactive animation">Fran</a> formulation or in its successors.  Over the last year, I&#8217;ve enjoyed getting more interface and functionality from standard type classes, and I&#8217;ve realized that quite a lot of FRP could be both packaged and implemented by leveraging those classes.  This post describes that packaging, and in particular shows how the <code>Monad</code> interface makes some operations very easy to define.  I suspect that monadic functional reactivity is a very powerful structuring tool with lovely applications.</p>

<p>At <a href="http://www.informatik.uni-bonn.de/~ralf/icfp07.html" title="International Conference on Functional Programming, 2007">ICFP 07</a>, I had a conversation with <a href="http://www.deinprogramm.de/sperber/" title="Home page: Mike Sperber">Mike Sperber</a> about FRP and about his FRP-based <a href="http://www-pu.informatik.uni-tuebingen.de/lula" title="The Lula system for lighting design and control">Lula</a> system for stage lighting.  Mike used blocking threads in Lula, which I had never considered for FRP.  While playing with the idea, I realized that I could give a very elegant and efficient solution to caching, unlike my previous FRP implementations, including the recent <a href="http://haskell.org/haskellwiki/DataDriven" title="Wiki page on the DataDriven library">DataDriven</a> library.  From there, I stumbled on the idea of <em>reactive normal form</em> and separating out an aspect of reactivity into the notion of <em><a href="http://conal.net/blog/posts/future-values/" title="Blog post: &quot;Future values&quot;">future values</a></em>.  A third new (to me) idea is to factor out continuity from reactivity, so that <em>reactive behaviors</em> arise by composing orthogonal notions of <em>reactive values</em> and non-reactive functions of continuous time.</p>

<p>Two previous posts presented <em>future values</em>, first describing <a href="http://conal.net/blog/posts/future-values/" title="Blog post: &quot;Future values&quot;">interface and semantics</a> and then a <a href="http://conal.net/blog/posts/future-values-via-multi-threading/" title="Blog post: &quot;Future values via multi-threading&quot;">multi-threaded implementation</a>.  This post builds a simple foundation for FRP on top of future values, as part of a library <a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">Reactive</a>.</p>

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

<h2>Functional events and reactive values</h2>

<p>A functional <em>event</em> is (semantically) a stream of time-associated values, ordered with respect to time.  For example, the stream of keystrokes you&#8217;ll type.  While many systems have a primitive notion of events, functional events are <em>compositional</em>, i.e., they&#8217;re constructed by applying operations to one or more simpler events, e.g., the lower-case version of your keystroke stream, the substream of uppercase keys, or the temporally interleaved stream from your keystrokes <em>and</em> mine (data massaging, filtering, and merging).  As usual, compositionality leads to reuse.</p>

<p>A <em>reactive value</em> is a functional notion for a value that changes discretely over time, based on the occurrences of one or more (possibly composite) events.  Reactive values provide a a first class encapsulation of the infinite time-line of varying values.  As such, they replace many of the uses of imperative programming, particularly concurrent imperative programming, which is notoriously difficult to work with and reason about.</p>

<p>The semantics of a reactive value can be simply a function of time.  Since reactive values change <em>discretely</em> (though see below for continuous change), their meanings will be step functions (changing only at a discrete&#8211;but possibly infinite&#8211;set of domain values).  Since a step function is fully defined by an initial value and a discrete set of time-associated values (the changes), we can represent reactive values as a value plus an event that yields more values over time.</p>

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

<p>How then might we define <code>Event</code>?  It is a stream of a first occurrence and some more occurrences, just like <code>Reactive</code>.  But the first occurrence and the remainder event are knowable only in the future.  Therefore,</p>

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

<p>using the <code>Future</code> type described in <a href="http://conal.net/blog/posts/future-values/" title="Blog post: &quot;Future values&quot;">a previous post</a>.</p>

<h2>Composing reactivity</h2>

<h3>Standard classes</h3>

<p>Many of the means of constructing events and reactive values are packaged as methods on the standard type classes <code>Monoid</code>, <code>Functor</code>, <code>Applicative</code>, and <code>Monad</code>.  See the Reactive <a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">wiki page</a> and <a href="http://darcs.haskell.org/packages/reactive/doc/html" title="Library documentation for Reactive">documentation</a> for descriptions of these instances.  Here are a few examples:</p>

<ul>
<li>The event <code>e `mappend` e'</code> occurs whenever <code>e</code> or <code>e'</code> occurs.</li>
<li>The event <code>fmap f e</code> (also called <code>f &lt;$&gt; e</code>) occurs with value <code>f x</code> whenever <code>e</code> occurs with value <code>x</code>.</li>
<li>The reactive value <code>rf &lt;*&gt; rx</code> at time <code>t</code> is equal has value <code>f x</code>, where <code>rf</code> and <code>rx</code> have the values <code>f</code> and <code>x</code> respectively at time <code>t</code>.  The <code>(&lt;*&gt;)</code> and <code>pure</code> functions (the methods of <code>Applicative</code>) are the basis of applying an <em>n</em>-ary function to <em>n</em> reactive values, to form a reactive value.  Note the implicit and semantically simple concurrency.</li>
<li>If <code>ee</code> is an event-valued event (<code>ee :: Event (Event a)</code>), then <code>join ee</code> is an event whose occurrences are all of the occurrences of all of the events in <code>ee</code>.</li>
</ul>

<h3>Switching</h3>

<p>The main tools for introducing reactivity are <code>stepper</code> and <code>switcher</code>.  (These combinators were <a href="http://conal.net/papers/dsl97" title="Paper: &quot;Modeling Interactive 3D and Multimedia Animation with an Embedded Language&quot;">added to Fran</a> after the <a href="http://conal.net/papers/icfp97/">original paper</a>, which had only single-shot events and therefore encouraged an explictly recursive programming style.)  The <code>stepper</code> function exactly corresponds to the <code>Stepper</code> constructor in our our new representation of <code>Reactive</code> above.</p>

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

<p>For a more general reactive combinator, <code>switcher</code> takes an initial <em>reactive value</em> and an event that generates reactive values to switch to.</p>

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

<p>Pleasantly, it is very easy to define this general form of switching in terms of the simpler <code>stepper</code>.  To see how this definition works, note that the arguments of <code>switcher</code> are also suitable as arguments to <code>stepper</code>, but the result will be of type <code>Reactive (Reactive a)</code> instead of <code>Reactive a</code>.  Fortunately, <code>Reactive</code> is a monad, so <code>join</code> turns reactive-reactive values into reactive values.</p>

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

<h3>Other building blocks</h3>

<p>Many other event and reactive value combinators are described in the Reactive <a href="http://darcs.haskell.org/packages/reactive/doc/html" title="Library documentation for Reactive">library docs</a>.  Here are just a few:</p>

<ul>
<li>Cumulative application of functions from a function-valued event.</li>
<li>A <code>scanl</code> for events and one for values.</li>
<li>Filtering of <code>Maybe</code>-valued events, and filtering of an event by a boolean reactive value.</li>
<li>Sampling of a reactive value at each occurrence of an event.</li>
</ul>

<h2>Coming attractions</h2>

<p>Future posts will (a) present the implementation of events and reactive values via a <a href="http://conal.net/blog/posts/reactive-normal-form/" title="Blog post: &quot;Reactive normal form&quot;">reactive normal form</a>, and (b) say how to <a href="http://conal.net/blog/posts/blending-continuity-into-reactive-values/" title="Blog post: &quot;Blending continuity into reactive values&quot;">blend temporal continuity</a> with our discrete notion of reactive values.</p>

<p>I see FRP as being a viable alternative to imperative programming, especially with concurrency.  Like the imperative programming model, FRP is about change.  Unlike concurrent imperative programming, FRP has formally tractable&#8211;even simple&#8211;semantics.  Future blog posts will give examples of how to reduce dependence on side-effects, widening the pure, and hence semantically tractable and composable, core of programs.</p>

<hr />

<p><em>Edit 2008-02-07:</em> added missing type parameter in <code>Event</code> definition.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=9&amp;md5=1f22bc6a562564cb485179f7d0c9b8fe"><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-values-from-the-future/feed</wfw:commentRss>
		<slash:comments>7</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-values-from-the-future&amp;language=en_GB&amp;category=text&amp;title=Reactive+values+from+the+future&amp;description=I%26%238217%3Bve+gotten+interested+lately+in+revisiting+functional+reactive+programming+%28FRP%29.+I+was+never+entirely+satisfied+with+the+semantics+or+implementation+in+my+original+Fran+formulation+or+in+its+successors.+Over...&amp;tags=events%2Cfuture+value%2Creactive+value%2Creactivity%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
