<?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; solution</title>
	<atom:link href="http://conal.net/blog/tag/solution/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>Implementing a type for partial values</title>
		<link>http://conal.net/blog/posts/implementing-a-type-for-partial-values</link>
		<comments>http://conal.net/blog/posts/implementing-a-type-for-partial-values#comments</comments>
		<pubDate>Mon, 02 Jul 2007 01:17:00 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[partial value]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://conal.net/blog/posts/implementing-a-type-for-partial-values/</guid>
		<description><![CDATA[In my previous post, I gave an interface for a type of partial values and invited implementations. Here&#8217;s mine, which surprised me in its simplicity. The trick is to represent Partial a as a -&#62; a, i.e., a way to selectively replace parts of a value. Typically the replaced parts are bottom/undefined. I want Partial [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Implementing a type for partial values

Tags: library, partial value, solution 

URL: http://conal.net/blog/posts/implementing-a-type-for-partial-values/

-->

<!-- references -->

<!-- teaser -->

<p>In my <a href="http://conal.net/blog/posts/a-type-for-partial-values/" title="Blog post: &quot;A type for partial values&quot;">previous post</a>, I gave an interface for a type of <em>partial values</em> and invited implementations. Here&#8217;s mine, which surprised me in its simplicity.</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-36"></span></p>

<p>The trick is to represent <code>Partial a</code> as <code>a -&gt; a</code>, i.e., a way to selectively replace parts of a value. Typically the replaced parts are bottom/undefined. I want <code>Partial a</code> to be a monoid, and <code>Data.Monoid</code> provides an instance:</p>

<pre><code>-- | The monoid of endomorphisms under composition.
newtype Endo a = Endo { appEndo :: a -&gt; a }

instance Monoid (Endo a) where
  mempty = Endo id
  Endo f `mappend` Endo g = Endo (f . g)
</code></pre>

<p>So my definition is simply a synonym:</p>

<pre><code>type Partial = Endo
</code></pre>

<p>Note that <code>mempty</code> and <code>mappend</code> do exactly what I want. <code>mempty</code> adds no information at all, while <code>u `mappend` v</code> adds (overrides) information with <code>u</code> and then with <code>v</code>.</p>

<p>The implementation of functions on <code>Partial</code> is trivial.</p>

<pre><code>inPartial :: ((a-&gt;a) -&gt; (a'-&gt;a')) -&gt; (Partial a -&gt; Partial a')
inPartial f = Endo . f . appEndo

valp :: c -&gt; Partial c
valp c = Endo (const c)

pval :: Partial c -&gt; c
pval (Endo f) = f undefined
unFst :: Partial a -&gt; Partial (a,b)
unFst = inPartial first

unSnd :: Partial b -&gt; Partial (a,b)
unSnd = inPartial second

unElt :: Functor f =&gt; Partial a -&gt; Partial (f a)
unElt = inPartial fmap
</code></pre>

<p>I&#8217;m not sure I&#8217;ll end up using the <code>Partial</code> type in Eros, but I like having it around.   If you think of variations, extensions and/or other uses, please let me know.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=36&amp;md5=7a0d8fdfaf3d3af9ca985274869c2b27"><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/implementing-a-type-for-partial-values/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%2Fimplementing-a-type-for-partial-values&amp;language=en_GB&amp;category=text&amp;title=Implementing+a+type+for+partial+values&amp;description=In+my+previous+post%2C+I+gave+an+interface+for+a+type+of+partial+values+and+invited+implementations.+Here%26%238217%3Bs+mine%2C+which+surprised+me+in+its+simplicity.+The+trick+is+to+represent...&amp;tags=library%2Cpartial+value%2Csolution%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
