<?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; point-free</title>
	<atom:link href="http://conal.net/blog/tag/point-free/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>Prettier functions for wrapping and wrapping</title>
		<link>http://conal.net/blog/posts/prettier-functions-for-wrapping-and-wrapping</link>
		<comments>http://conal.net/blog/posts/prettier-functions-for-wrapping-and-wrapping#comments</comments>
		<pubDate>Tue, 02 Dec 2008 06:46:27 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[point-free]]></category>
		<category><![CDATA[technique]]></category>
		<category><![CDATA[type composition]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=68</guid>
		<description><![CDATA[The post Semantic editor combinators gave an example of a pattern that comes up a lot for me in Haskell programming. I want to apply functions inside of a newtype without cumbersome unwrapping and wrapping of the representation insides. While chatting with Daniel Peebles in #haskell today, the realization hit me that these &#8220;higher-order wrappers&#8221; [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Prettier functions for wrapping and wrapping

Tags: technique, type composition, point-free

URL: http://conal.net/blog/posts/prettier-functions-for-wrapping-and-wrapping/

-->

<!-- references -->

<!-- teaser -->

<p>The post <em><a href="http://conal.net/blog/posts/semantic-editor-combinators/" title="blog post">Semantic editor combinators</a></em> gave an example of a pattern that comes up a lot for me in Haskell programming.
I want to apply functions inside of a <code>newtype</code> without cumbersome unwrapping and wrapping of the representation insides.</p>

<p>While chatting with Daniel Peebles in #haskell today, the realization hit me that these &#8220;higher-order wrappers&#8221; can not only make other code pretty, but can themselves be expressed more beautifully and clearly, using two of the combinators given in that post.</p>

<!--
**Edits**:

* 2008-02-09: just fiddling around
-->

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

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

<p>The example I gave was type composition, taken from the <a href="http://haskell.org/haskellwiki/TypeCompose" title="Wiki page">TypeCompose</a> library:</p>

<pre><code>newtype (g :. f) a = O { unO :: g (f a) }
</code></pre>

<p>The convenient higher-order wrappers apply n-ary function within <code>O</code> constructors:</p>

<pre><code>inO  h (O gfa) = O (h gfa)
inO2 h (O gfa) (O gfa') = O (h gfa gfa')
...
</code></pre>

<p>Then I get to implement <code>Functor</code> and <code>Applicative</code> instances in the style of semantic editor combinators.</p>

<pre><code>instance (Functor g, Functor f) =&gt; Functor (g :. f) where
  fmap  = inO . fmap . fmap

instance (Applicative g, Applicative f) =&gt; Applicative (g :. f) where
  pure  = O . pure . pure
  (&lt;*&gt;) = (inO2 . liftA2) (&lt;*&gt;)
</code></pre>

<p>The point-free definitions I gave before are pretty cryptic if you&#8217;re not used to the style:</p>

<pre><code>inO  = (  O  .) . (. unO)
inO2 = (inO  .) . (. unO)
inO3 = (inO2 .) . (. unO)
...
</code></pre>

<p>What dawned on me today is that I can instead say what I mean plainly: <code>inO</code> applies <code>unO</code> to the argument and <code>O</code> to the result.</p>

<pre><code>inO  = result   O  . argument unO
</code></pre>

<p>Similarly, <code>inO2</code> applies <code>unO</code> to the (first) argument and <code>inO</code> to the resulting function.
Similarly for <code>inO3</code>:</p>

<pre><code>inO2 = result inO  . argument unO
inO3 = result inO2 . argument unO
...
</code></pre>

<p>The unwrapping and wrapping don&#8217;t interact, so, we can write equivalent definitions, swapping the compositions:</p>

<pre><code>inO2 = argument unO  . result inO
</code></pre>

<p>Equivalence follows from associativity of function composition.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=68&amp;md5=9c0a30468e629eb7494cdab448736186"><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/prettier-functions-for-wrapping-and-wrapping/feed</wfw:commentRss>
		<slash:comments>4</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%2Fprettier-functions-for-wrapping-and-wrapping&amp;language=en_GB&amp;category=text&amp;title=Prettier+functions+for+wrapping+and+wrapping&amp;description=The+post+Semantic+editor+combinators+gave+an+example+of+a+pattern+that+comes+up+a+lot+for+me+in+Haskell+programming.+I+want+to+apply+functions+inside+of+a+newtype...&amp;tags=point-free%2Ctechnique%2Ctype+composition%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
