<?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; dance</title>
	<atom:link href="http://conal.net/blog/tag/dance/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>Functional reactive partner dancing</title>
		<link>http://conal.net/blog/posts/functional-reactive-partner-dancing</link>
		<comments>http://conal.net/blog/posts/functional-reactive-partner-dancing#comments</comments>
		<pubDate>Tue, 12 Feb 2008 06:16:10 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[arrow]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[dance]]></category>
		<category><![CDATA[FRP]]></category>
		<category><![CDATA[functional reactive programming]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[lead/follow]]></category>
		<category><![CDATA[mutant]]></category>

		<guid isPermaLink="false">http://conal.net/blog/posts/functional-reactive-partner-dancing/</guid>
		<description><![CDATA[This note continues an exploration of arrow-friendly formulations of functional reactive programming. I refine the previous representations into an interactive dance with dynamically interchanging roles of follow and lead. These two roles correspond to the events and reactive values in the (non-arrow) library Reactive described in a few previous posts. The post ends with some [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Functional reactive partner dancing

Tags: functional reactive programming, FRP, bot, mutant, dance, lead/follow, arrow, functor, applicative functor

URL: http://conal.net/blog/posts/functional-reactive-partner-dancing/

-->

<!-- references -->

<!-- teaser -->

<p><a href=http://www.theage.com.au/news/Technology/Robots-offer-a-hand-on-the-ballroom-floor/2005/06/09/1118123948570.html><img class=illustration src="http://conal.net/images/misc/dancing_robot.jpg" title= "Takahiro Takeda dances with the Partner Ballroom Dance Robot. Photo: AFP." align="right"></a>
This note continues an exploration of arrow-friendly formulations of functional reactive programming.  I refine the previous representations into an <em>interactive dance</em> with dynamically interchanging roles of <em>follow</em> and <em>lead</em>.  These two roles correspond to the events and reactive values in the (non-arrow) library <a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">Reactive</a> described in a few previous posts.  The post ends with some examples.</p>

<p>The code described (with documentation and examples) here may be found in the new, experimental library <strong><a href="http://haskell.org/haskellwiki/Bot" title="Wiki page for the Bot library">Bot</a></strong> (which also covers <a href="http://conal.net/blog/posts/functional-reactive-chatter-bots/" title="Blog post: &quot;Functional reactive chatter-bots&quot;">mutant-bots and chatter-bots</a>).</p>

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

<h2>Dancing with Mealy machines</h2>

<p>As mentioned in a <a href="http://conal.net/blog/posts/functional-reactive-chatter-bots/" title="Blog post: &quot;Functional reactive chatter-bots&quot;">previous post</a>, Mealy-style automata (&#8220;bots&#8221;) can be given the following type:</p>

<pre><code>a -&gt; (b, a -&gt; (b, a -&gt; (b, ...)))
</code></pre>

<p>where <code>a</code> and <code>b</code> are the types of inputs and outputs, respectively.  The essence of Mealy machines is the alternation of <em>consuming</em> and <em>producing</em> values.  The type ensures that exactly one output is delivered per input.  In recursive form,</p>

<pre><code>newtype a `MutantBot` b = Mutant (a -&gt; (b, a `MutantBot` b))
</code></pre>

<p>which is a special case of Ross Paterson&#8217;s <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/arrows" title="Haskell package: arrows"><code>Automaton</code> arrow transformer</a>.</p>

<p>These bots do nothing until prompted by an external partner.  What kind of thing is that partner?  Unlike our bots, it must have the ability to take initiative, i.e., to produce a value without itself being prompted.  So we might say that the partner is <em>leading</em>, while the bot is <em>following</em> (or <em>acting</em> and <em>reacting</em>, or <em>generative</em> and <em>receptive</em>).</p>

<p>Once the partner has provided a value, the bot can then provide a reponse.  After the bot&#8217;s reponse, there will be another value from the partner.  Once this dance has begun, the roles of <em>lead</em> and <em>follow</em> are no longer static but rather trade back and forth fluidly.</p>

<p>We&#8217;ve seen the type of the bot:</p>

<pre><code>a -&gt; (b, a -&gt; (b, a -&gt; (b, ...)))
</code></pre>

<p>What is the type of the partner who can dance Lead to the bot&#8217;s Follow?  Initially, while the bot waits for a value (follows), the partner provides one (leads).  After that first step, the roles reverse.</p>

<pre><code>(a, b -&gt; (a, b -&gt; (a, b -&gt; ...)))
</code></pre>

<p>This Lead type matches the range of the Follow type, but with <code>a</code> and <code>b</code> swapped.  We can therefore type our bot friend as <code>a `Follow` b</code> and its partner as <code>b `Lead` a</code>, where</p>

<pre><code>type a `Follow` b =  a -&gt; Lead a b
type a `Lead`   b = (b, Follow a b)
</code></pre>

<p>This formulation makes it clear that the bot and the partner are simply different phases of the same kind of process (though with swapped type parameters).  While the bot is <code>a `Follow` b</code>, the partner is <code>b `Lead` a</code>; and while the bot is <code>a `Lead` b</code>, the partner is <code>b `Follow` a</code>.</p>

<p>Haskell won&#8217;t accept these mutually recursive types without at least one <code>newtype</code> or <code>data</code> wrapper.  We&#8217;ll use two.</p>

<pre><code>newtype a `Follow` b = Follow (a -&gt; Lead a b)
newtype a `Lead`   b =   Lead (b, Follow a b)
</code></pre>

<p><em>Aside:</em> I studied partner dancing pretty intensively a few years ago, during my mid-life sabbatical.  One of my later teachers rocked my conception of dancing when he said that the roles of &#8220;lead&#8221; and &#8220;follow&#8221; are not rigid, but alternate fluidly between the parters.  For instance the man (typically) initiates a movement (leads), which the woman (typically) responds to (follows).  The roles then smoothly reverse: the man then follows her as she leads him through completion of the movement, when the roles reverse again.</p>

<h2>Class instances</h2>

<p>Partially applied, both of these types are functors and applicative functors.</p>

<pre><code>instance Functor (Follow a) where
  fmap f (Follow h) = Follow (fmap f . h)

instance Applicative (Follow a) where
  pure b = Follow (const (pure b))
  Follow h &lt;*&gt; Follow k = Follow $  a -&gt; h a &lt;*&gt; k a

instance Functor (Lead a) where
  fmap f (Lead (b, g)) = Lead (f b, fmap f g)

instance Applicative (Lead a) where
  pure b = Lead (b, pure b)
  Lead (f,pf) &lt;*&gt; Lead (a,pa) = Lead (f a, pf &lt;*&gt; pa)
</code></pre>

<p>Since I&#8217;ve been playing with <code>Functor</code> and <code>Applicative</code> and type compositions a lot lately, some patterns in this code jump out at me.  It&#8217;s almost possible to get these four instances automatically, from very simple reformulations of <code>Follow</code> and <code>Lead</code>.  I&#8217;ll address those reformulations in another post.</p>

<p>An <code>Arrow</code> instance for <code>Follow</code> can be <a href="http://darcs.haskell.org/packages/bot/doc/html/src/Data-Bot-LeadFollow.html">adapted easily</a> from the <code>Arrow</code> instance for <code>Automaton</code>.  I don&#8217;t think it&#8217;s possible to define an <code>Arrow</code> instance for <code>Lead</code>, however.  Is it an instance of some other generic notion?</p>

<p>The type parameter order for <code>Follow</code> and <code>Lead</code> is always received before generated.  That order fits well with the type classes <code>Functor</code>, <code>Applicative</code>, and <code>Arrow</code>, while the reversed parameter order would mesh with contra-variant functors.  However, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html" title="Library documentation: `Control.Arrow`">Control.Arrow</a> provides its own contravariant map (and related goodies):</p>

<pre><code>-- | Precomposition with a pure function.
(^&gt;&gt;) :: Arrow (~&gt;) =&gt; (b -&gt; c) -&gt; c ~&gt; d -&gt; b ~&gt; d
f ^&gt;&gt; a = arr f &gt;&gt;&gt; a
</code></pre>

<p>By the way, I prefer infix operators over the traditional form (e.g., <code>c ~&gt; d</code> vs <code>a c d</code>).  Keep in mind that &#8220;<code>-&gt;</code>&#8221; binds less strongly than other infix operators.</p>

<h2>Events and varying values</h2>

<p>One of the puzzles I&#8217;ve had about arrow-based reactivity is how to distinguish between events and reactive values.  The lead/follow dance provides an answer.  Here are the definitions, from <a href="http://conal.net/blog/posts/reactive-values-from-the-future/" title="Blog post: &quot;Reactive values from the future&quot;">Reactive values from the future</a>.</p>

<pre><code>newtype Event b = Future (Reactive b)

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

<p>An event starts out in waiting mode&#8211;a sort of <em>pregnant</em> phase&#8211;until it gives birth to a reactive value.  A reactive value initially has a value and then acts like an event.  These two definitions are very like our <code>Follow</code> and <code>Lead</code>.  Where <code>Event</code> and <code>Reactive</code> use futures, the arrow-based formulations have explicit inputs (of type <code>a</code>).</p>

<p>Now that I have this follow/lead perspective, I regret my choice of the name &#8220;<code>Reactive</code>&#8221; for varying values.  Now I see that events are initially <em>reactive</em> (receptive), while varying values are initially <em>active</em> (generative).</p>

<h2>Getting chatty</h2>

<p>I&#8217;ve found it useful to allow any number of outputs in reaction to a single input.  In particular, filtering eliminates outputs, and <code>mappend</code> can combine outputs.  This flexibility motivated <a href="http://conal.net/blog/posts/functional-reactive-chatter-bots/" title="Blog post: &quot;Functional reactive chatter-bots&quot;">chatter-bots</a>.  The implementation is simple: use a list-valued output type, rewrapping as another abstraction.</p>

<pre><code>newtype a :&gt;- b = Leads   (Lead   a [b])
newtype a :-&gt; b = Follows (Follow a [b])
</code></pre>

<p>These types are also functors and applicative functors (when partially applied).  The instances are boilerplate for any composition of applicative functors.  The <code>Arrow</code> instance is a straightforward adaption from the <code>ChatterBot</code> instance given in <a href="http://conal.net/blog/posts/functional-reactive-chatter-bots/" title="Blog post: &quot;Functional reactive chatter-bots&quot;">Functional reactive chatter-bots</a>.</p>

<p>The <code>Lead</code> and <code>Follow</code> types are very similar to <a href="http://www.md.chalmers.se/Cs/Research/Functional/Fudgets/" title="Fudgets home page">Fudgets</a>-style stream processors, which provide output flexibility in a different way.  I will compare these two approaches in an upcoming post.</p>

<h2>Examples</h2>

<p>The examples from <a href="http://conal.net/blog/posts/accumulation-for-functional-reactive-chatter-bots/" title="Blog post: &quot;Accumulation for functional reactive chatter-bots&quot;">Accumulation for functional reactive chatter-bots</a> all work in the lead/follow setting, with changes to the function names.  Those examples were follows, but are more useful as leads, which we can now do as well, thanks to lead versions of the accumulating combinators.</p>

<p>As another example, let&#8217;s maintain the product of two independently-changing numbers.  In <a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">Reactive</a>, we&#8217;d take two reactive (varying) values and return one:</p>

<pre><code>Reactive Int -&gt; Reactive Int -&gt; Reactive Int
</code></pre>

<p>We&#8217;ll have to re-arrange this interface to fit into the <code>Arrow</code>-style interface.  <a href="http://conal.net/blog/posts/reactive-values-from-the-future/" title="Blog post: &quot;Reactive values from the future&quot;">As described earlier</a>, a reactive value is simply a value and an event (analogous to a lead and a follow).  A bit of currying, argument flipping, and uncurrying gives the type</p>

<pre><code>(Int,Int) -&gt; (Event Int, Event Int) -&gt; Reactive Int
</code></pre>

<p>Next use the Event/pairing isomorphism:</p>

<pre><code>(Int,Int) -&gt; Event (Either Int Int) -&gt; Reactive Int
</code></pre>

<p>which converts to a Lead:</p>

<pre><code>prod :: (Int,Int) -&gt; Either Int Int :&gt;- Int
</code></pre>

<p>As first step, let&#8217;s decode the <code>Either</code>-valued input into a pair-modifying function</p>

<pre><code>updPair :: Either c d -&gt; (c,d) -&gt; (c,d)
updPair (Left  c') (_,d) = (c',d)
updPair (Right d') (c,_) = (c,d')
</code></pre>

<p>Or, the functionally svelte</p>

<pre><code>updPair = (first.const) `either` (second.const)
</code></pre>

<p>Using <code>updPair</code>, we can convert the <code>Either</code>-encoded pair edits to editing functions, and then cumulatively apply those functions.  (The <code>accumF</code> function is like <a href="http://conal.net/blog/posts/accumulation-for-functional-reactive-chatter-bots/" title="Blog post: &quot;Accumulation for functional reactive chatter-bots&quot;"><code>accumC</code> on chatter-bots</a>.)</p>

<pre><code>editPairF :: (c,d) -&gt; Either c d :-&gt; (c,d)
editPairF cd = updPair ^&gt;&gt; accumF cd
</code></pre>

<p>We have all we need to make a Lead version of pair editing as well:</p>

<pre><code>editPairL :: (c,d) -&gt; Either c d :&gt;- (c,d)
editPairL cd = leads [cd] (editPairF cd)
</code></pre>

<p>Or, with some pointfree-<em>fu</em>,</p>

<pre><code>    editPairL = leads.pure &lt;*&gt; editPairF
</code></pre>

<p>where <code>leads</code> is a convenience function:</p>

<pre><code>leads :: [b] -&gt; a :-&gt; b -&gt; a :&gt;- b
leads bs (Follows fol) = Leads (Lead (bs,fol))
</code></pre>

<p>Our <code>prod</code> example is now just a simple application of these generally useful pair editors.</p>

<pre><code>prod = (fmap.fmap) (uncurry (*)) editPairL
</code></pre>

<p>The first <code>fmap</code> is for <code>(-&gt;) (Int,Int)</code>, and the second is for <code>(:&gt;-) (Either Int Int)</code>.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=17&amp;md5=4ba1b4145cb3870813e23b2184945b41"><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/functional-reactive-partner-dancing/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%2Ffunctional-reactive-partner-dancing&amp;language=en_GB&amp;category=text&amp;title=Functional+reactive+partner+dancing&amp;description=This+note+continues+an+exploration+of+arrow-friendly+formulations+of+functional+reactive+programming.+I+refine+the+previous+representations+into+an+interactive+dance+with+dynamically+interchanging+roles+of+follow+and+lead.+These...&amp;tags=applicative+functor%2Carrow%2Cbot%2Cdance%2CFRP%2Cfunctional+reactive+programming%2Cfunctor%2Clead%2Ffollow%2Cmutant%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
