<?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; lead/follow</title>
	<atom:link href="http://conal.net/blog/tag/leadfollow/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>Applicative bots</title>
		<link>http://conal.net/blog/posts/applicative-bots</link>
		<comments>http://conal.net/blog/posts/applicative-bots#comments</comments>
		<pubDate>Wed, 13 Feb 2008 06:30:43 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[correction]]></category>
		<category><![CDATA[FRP]]></category>
		<category><![CDATA[functional reactive programming]]></category>
		<category><![CDATA[lead/follow]]></category>

		<guid isPermaLink="false">http://conal.net/blog/posts/applicative-bots/</guid>
		<description><![CDATA[In Functional reactive partner dancing, I mentioned that (a) the partially applied leading and following types have boilerplate Applicative instances, and (b) the leading type corresponds to varying (reactive) values. Today I realized that those boilerplate instances are not very useful, and that they do not correspond to the Applicative instance of Reactive. In this [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Applicative bots

Tags: functional reactive programming, FRP, applicative functors, bot, lead/follow, correction

-->

<!-- references -->

<!-- teaser -->

<p>In <a href="http://conal.net/blog/posts/functional-reactive-partner-dancing/" title="Blog post: &quot;Functional reactive partner dancing&quot;">Functional reactive partner dancing</a>, I mentioned that (a) the partially applied leading and following types have boilerplate <code>Applicative</code> instances, and (b) the leading type corresponds to <a href="http://conal.net/blog/posts/reactive-values-from-the-future/" title="Blog post: &quot;Reactive values from the future&quot;">varying (reactive) values</a>.  Today I realized that those boilerplate instances are not very useful, and that they <em>do not</em> correspond to the <code>Applicative</code> instance of <code>Reactive</code>.  In this post, I give a useful <code>Applicative</code> instance that does correspond to the <code>Reactive</code> instance.  The instance definition is expressed in terms of the pair editor bot shown at the end of the &#8220;dancing&#8221; post, which seems to have a variety of applications.</p>

<p>The <code>Applicative</code> instance has one awkward aspect that suggests a tweak to the formulation of leading.  I give simplified versions of pair editing and <code>Applicative</code> for the revised type. This change is in <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/bot-0.1" title="Haskell package: bot">version 0.1</a> of the <a href="http://haskell.org/haskellwiki/Bot" title="Wiki page for the Bot library">Bot</a> libary.</p>

<p><em>Edit 2008-02-15</em>: added FRP tags; prose tweak.</p>

<!-- without a comment or something here, the last item above becomes a paragraph -->

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

<h3>Oops</h3>

<p>The multi-output versions of leading and following are formulated simply as the single-output versions, with a list-valued output type:</p>

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

<p>Partially applied, each of these types is a sort of composition of type constructors.  For instance, <code>(:-&gt;) a</code> is the type composition of <code>Follow a</code> and <code>[]</code>.  Since both of those type constructors are applicative functors, there are standard definitions of <code>Functor</code> and <code>Applicative</code>.</p>

<pre><code>instance Functor ((:-&gt;) i) where
  fmap f (Follows z) = Follows ((fmap.fmap) f z)

instance Applicative ((:-&gt;) i) where
  pure x                  = Follows ((pure.pure) x)
  Follows f &lt;*&gt; Follows x = Follows (liftA2 (&lt;*&gt;) f x)
</code></pre>

<p>and similarly for <code>(:&gt;-) i</code>.</p>

<p>In fact, these instance templates are abstracted into instances for the type composition operator <code>(:.)</code> found in <a href="http://haskell.org/haskellwiki/TypeCompose" title="Wiki page for the TypeCompose library">TypeCompose</a>, so we can get the four instances for free if we define</p>

<pre><code>type (:-&gt;) a = Follow a :. []
type (:&gt;-) a = Lead   a :. []
</code></pre>

<p>While the <code>Functor</code> instances work fine, the rub (which I didn&#8217;t realize when writing the &#8220;dancing&#8221; post) is that the <code>Applicative</code> instances are not what I want.  They delegate to the <code>Applicative</code> instances for <code>Follow a</code> and for <code>[]</code>.  The result is that each output of <code>lf &lt;*&gt; lx</code> is the list of <code>f x</code> for all <code>f</code> in the (list-valued) <code>lf</code> output at that point and all <code>x</code> in the (list-valued) <code>lx</code> output.  In particular, the <code>lf &lt;*&gt; lx</code> will have an output only when <em>both</em> <code>lf</code> and <code>lx</code> have simultaneous outputs.</p>

<p>Instead, I&#8217;d like <code>lf &lt;*&gt; lx</code> to have an output whenever <em>either</em> <code>lf</code> or <code>lx</code> has an output.  If <code>lf</code> has an output <code>f</code>, I want to output <code>f x</code>, where <code>x</code> is the most recent <code>lx</code> output.  Similarly, if <code>lx</code> has an output, I want to output <code>f x</code>, where <code>f</code> is the most recent <code>lf</code> output.  This behavior is exactly how <code>Applicative</code> works for <code>Reactive</code>, as described in <a href="http://conal.net/blog/posts/reactive-normal-form/" title="Blog post: &quot;Reactive normal form&quot;">reactive-normal-form</a>.</p>

<h3>A solution and a problem</h3>

<p>At the end of <a href="http://conal.net/blog/posts/functional-reactive-partner-dancing/" title="Blog post: &quot;Functional reactive partner dancing&quot;">Functional reactive partner dancing</a>, I showed a tool that is related to the desired <code>Applicative</code> behavior.</p>

<pre><code>editPairF :: (c,d) -&gt; Either c d :-&gt; (c,d)
</code></pre>

<p>Given an initial pair, <code>editPairF</code> accepts replacements of either the first or second element and produces updated pairs, remembering the previous values.  Since memory is involved, <code>editPairL</code> is defined in terms of the generic accumulation combinator <code>accumF</code>.</p>

<p>Let&#8217;s put <code>editPairF</code> to work, to pair up two follows into a pair-valued follow.  A new pair is output whenever either element gets output.</p>

<pre><code>pairF :: (b,c) -&gt; a:-&gt;b -&gt; a:-&gt;c -&gt; a:-&gt;(b,c)
pairF bc ab ac = (Left &lt;$&gt; ab) `mappend` (Right &lt;$&gt; ac) &gt;&gt;&gt; editPairF bc
</code></pre>

<p>We had to supply the initial pair here, because follows don&#8217;t have initial values.  Leads do, however, so the lead-pairing function has a simpler-looking type:</p>

<pre><code>pairL :: a:&gt;-b -&gt; a:&gt;-c -&gt; a:&gt;-(b,c)
</code></pre>

<p>The definition of <code>pairL</code> works in terms of <code>pairF</code>.  The extra work involves disassembling and reassembling leads into and from initial values and follows.</p>

<pre><code>ab `pairL` ac =
  leads (liftA2 (,) bs cs) $ pairF (b,c) abf acf
 where
   (bs,abf) = splitL ab
   (cs,acf) = splitL ac
   -- Oh dear.  b &amp; c might not be well-defined
   b = last bs
   c = last cs
</code></pre>

<p>The awkward bit here is that, as formulated, a multi-lead (<code>a :&gt;- b</code>) could have multiple values even initially.  For that reason, I (a) use a cross product (<code>liftA2 (,)</code>) for the initial pairs, and (b) extract a single value from each lead to use in the pair-valued lead&#8217;s initial value.  This second consideration is worse than awkward; it will fail if either initial value list is empty.</p>

<p>Is it really useful for a lead to have an initial <em>list</em> of values?  Not that I know of.  I allowed the flexibility because it made the type definitions so simple and uniform, which I&#8217;ve found has a decisive impact on the simplicity of code that works on the type.</p>

<p>Placing the initial-list problem aside for now, here is the simple and useful Applicative instance for leads.  The <code>pure</code> method makes a lead with an initial value an no future reponses.  The <code>(&lt;*&gt;)</code> method uses <code>pairL</code> above to make a lead whose outputs are function/argument pairs, and then maps uncurried function application onto the pairs to get out the results.</p>

<pre><code>instance Applicative ((:&gt;-) i) where
  pure x    = leads [x] mempty
  lf &lt;*&gt; lx = uncurry ($) &lt;$&gt; (lf `pairL` lx)
</code></pre>

<p>What about <code>(:-&gt;)</code> (following)?  I don&#8217;t think an <code>Applicative</code> instance like the leading one can exist, due to lack of initial values.  Perhaps there is an <code>Applicative</code> instance corresponding to the <a href="http://conal.net/blog/posts/reactive-values-from-the-future/" title="Blog post: &quot;Reactive values from the future&quot;">one on Event</a> (combining all pairs of occurrences over time).</p>

<h3>A tweak, and we&#8217;re back to safe &amp; elegant</h3>

<p>The difficulty with <code>pairL</code> comes from a feature of dubious value, namely having an initial list of values rather than exactly one initial value.  Let&#8217;s consider removing that flexibility.  Replace</p>

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

<p>with</p>

<pre><code>newtype a :&gt;- b = Leads (b, a :-&gt; b)
</code></pre>

<p>The pairing combinator for leads is now simpler.  It&#8217;s also bomb-proof, since it has initial single values instead of lists:</p>

<pre><code>pairL :: a:&gt;-b -&gt; a:&gt;-c -&gt; a:&gt;-(b,c)
Leads (a,fa) `pairL` Leads (b,fb) =
  Leads ((a,b), pairF (a,b) fa fb)
</code></pre>

<p>And we have simple and (I think) trouble-free instances:</p>

<pre><code>instance Functor ((:&gt;-) i) where
  fmap f (Leads (b,fol)) = Leads (f b, fmap f fol)

instance Applicative ((:&gt;-) i) where
  pure x    = Leads (x,mempty)
  lf &lt;*&gt; lx = uncurry ($) &lt;$&gt; (lf `pairL` lx)
</code></pre>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=18&amp;md5=e9b6c004f8a05a0539c86bb9d5b605c2"><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/applicative-bots/feed</wfw:commentRss>
		<slash:comments>0</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%2Fapplicative-bots&amp;language=en_GB&amp;category=text&amp;title=Applicative+bots&amp;description=In+Functional+reactive+partner+dancing%2C+I+mentioned+that+%28a%29+the+partially+applied+leading+and+following+types+have+boilerplate+Applicative+instances%2C+and+%28b%29+the+leading+type+corresponds+to+varying+%28reactive%29+values....&amp;tags=applicative+functor%2Cbot%2Ccorrection%2CFRP%2Cfunctional+reactive+programming%2Clead%2Ffollow%2Cblog" type="text/html" />
	</item>
		<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>
