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