<?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; optimization</title>
	<atom:link href="http://conal.net/blog/tag/optimization/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>
	</channel>
</rss>
