<?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>porges &#187; indexed</title>
	<atom:link href="http://porg.es/blog/tag/indexed/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description></description>
	<lastBuildDate>Thu, 12 Jan 2012 23:45:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Parametrizing Monoids and Monads</title>
		<link>http://porg.es/blog/parametrizing-monoids-and-monads</link>
		<comments>http://porg.es/blog/parametrizing-monoids-and-monads#comments</comments>
		<pubDate>Mon, 16 Feb 2009 05:10:19 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[arity]]></category>
		<category><![CDATA[category]]></category>
		<category><![CDATA[criticism]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[indexed]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[monads]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=284</guid>
		<description><![CDATA[Dan Piponi’s latest post “Beyond Monads” has prompted some wonderment (and forehead-slapping). For example: How did we all miss that before? —Peaker The answer (of course!) is that while we might have, they didn&#8217;t. For example, Edward Kmett’s category-extras package has had Control.Monad.Indexed available for use in Haskell since early last year, while the concept [...]]]></description>
			<content:encoded><![CDATA[<p>Dan Piponi’s latest post “<a href="http://blog.sigfpe.com/2009/02/beyond-monads.html">Beyond Monads</a>” has prompted some wonderment (and forehead-slapping). For example:</p>
<blockquote><p>How did we all miss that before?<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
<p style="text-align:right">—<cite><a href="http://www.reddit.com/r/haskell/comments/7xi0r/a_neighborhood_of_infinity_beyond_monads/c07ohx9">Peaker</a></cite></p></blockquote>
<p>The answer (of course!) is that while <em>we</em> might have, <em>they</em> didn&#8217;t. <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /> For example, Edward Kmett’s <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras-0.53.5">category-extras</a> package has had <a href="http://hackage.haskell.org/packages/archive/category-extras/0.53.5/doc/html/Control-Monad-Indexed.html">Control.Monad.Indexed</a> available for use in Haskell since early last year, while the concept for its implementation in Haskell has been around since at least 2005 (see the paper “<a href="http://crab.rutgers.edu/~pjohann/f14-ghani.pdf">Monadic Augment and Generalised Short Cut Fusion</a>”). Needless to say, Oleg has <a href="http://okmij.org/ftp/Computation/monads.html#param-monad">been there</a> too.</p>
<p>For me, this prompted a vaguely-related revelation. Say that we have a Category class:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> Category <span style="color: green;">&#40;</span>⤳<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
	<span style="font-weight: bold;">id</span> ∷ a ⤳ a
	<span style="color: green;">&#40;</span>∘<span style="color: green;">&#41;</span> ∷ <span style="color: green;">&#40;</span>b ⤳ c<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a ⤳ b<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a ⤳ c<span style="color: green;">&#41;</span></pre></div></div>

<p>I had recently been wondering how categories other than (→), such as graphs (and even the trivial category) fit into this model. Now that I have figured it out, the answer is simple; it is a matter of arity. Graphs and other datastructure-related categories don’t have the extra type arguments (unless you’re working in a dependently-typed language), and so can’t fit into this. What we want to do is <em>fake</em> the extra arguments, by having some extra ones that we can just ignore (the name, FF, is short for FakeFake, i.e. two fake arguments).</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">newtype</span> FF f a b <span style="color: #339933; font-weight: bold;">=</span> FF <span style="color: green;">&#123;</span> unFF <span style="color: #339933; font-weight: bold;">::</span> f <span style="color: green;">&#125;</span></pre></div></div>

<p>Now we can define the trivial category!</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> Category <span style="color: green;">&#40;</span>FF <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
	<span style="font-weight: bold;">id</span> <span style="color: #339933; font-weight: bold;">=</span> FF <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
	f ∘ g <span style="color: #339933; font-weight: bold;">=</span> FF <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>Viewed in this light, it is obvious why (as Dan notes in the original post) Monoids give rise to Monads and Categories give rise to ParametrizedMonads, as we have:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> Monoid m ⇒ Category <span style="color: green;">&#40;</span>FF m<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
	<span style="font-weight: bold;">id</span> <span style="color: #339933; font-weight: bold;">=</span> FF mempty
	<span style="color: green;">&#40;</span>FF f<span style="color: green;">&#41;</span> ∘ <span style="color: green;">&#40;</span>FF g<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> FF <span style="color: green;">&#40;</span>mappend f g<span style="color: green;">&#41;</span></pre></div></div>

<p>Next, we want to write implementations for all our old Monads in the new PMonad class. We might do it like this (with a new newtype for the different arguments of this datatype, RealFakeFakeReal):</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">newtype</span> RFFR f x y a <span style="color: #339933; font-weight: bold;">=</span> RFFR <span style="color: green;">&#123;</span> unRFFR ∷ <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> <span style="color: green;">&#125;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">class</span> PMonad m <span style="color: #06c; font-weight: bold;">where</span>
	<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&gt;&gt;=</span><span style="color: green;">&#41;</span> ∷ m s1 s2 a → <span style="color: green;">&#40;</span>a → m s2 s3 b<span style="color: green;">&#41;</span> → m s1 s3 b
	<span style="font-weight: bold;">return</span> ∷ a → m s s a
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Old<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Monad</span> m<span style="color: green;">&#41;</span> ⇒ PMonad <span style="color: green;">&#40;</span>RFFR m<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
	<span style="color: green;">&#40;</span>RFFR x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> f <span style="color: #339933; font-weight: bold;">=</span> RFFR <span style="color: green;">&#40;</span>x Old<span style="color: #339933; font-weight: bold;">.&gt;&gt;=</span> unRFFR ∘ f<span style="color: green;">&#41;</span>
	<span style="font-weight: bold;">return</span> x <span style="color: #339933; font-weight: bold;">=</span> RFFR <span style="color: green;">&#40;</span>Old<span style="color: #339933; font-weight: bold;">.</span><span style="font-weight: bold;">return</span> x<span style="color: green;">&#41;</span></pre></div></div>

<p>There is a big issue that this highlights. It is <em>hard</em> to work with types of different arities in Haskell. (This is noted in the ‘<a href="http://haskell.org/haskellwiki/Quantified_contexts">quantified contexts</a>’ proposal.)</p>
<p>As an example, since we now know that every Monoid is a Category, we might think to drop Monoids and just use Categories everywhere (I should note that this is actually a silly idea <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_razz.gif" alt="" />!). Unfortunately, we can’t just do this as we’ll have to use <code>FF</code> wrappers/unwrappers everywhere:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">x <span style="color: #339933; font-weight: bold;">=</span> unFF <span style="color: #339933; font-weight: bold;">$</span> FF <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: green;">&#93;</span> ∘ FF <span style="color: green;">&#91;</span><span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: green;">&#93;</span></pre></div></div>

<p>I have not yet found a workaround for this. Help would be nice <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/parametrizing-monoids-and-monads/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

