<?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; Mathematics</title>
	<atom:link href="http://porg.es/blog/tag/mathematics/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>2π better than π?</title>
		<link>http://porg.es/blog/2%cf%80-better-than-%cf%80</link>
		<comments>http://porg.es/blog/2%cf%80-better-than-%cf%80#comments</comments>
		<pubDate>Mon, 21 Jan 2008 08:29:02 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[commentary]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[thoughts]]></category>

		<guid isPermaLink="false">http://porg.es/blog/2%cf%80-better-than-%cf%80</guid>
		<description><![CDATA[I just came across Bob Palais’ article from 2001, entitled Pi is wrong! He makes some very interesting points; however the notation he has chosen is rather unfortunate. The definition of his three-legged ‘2π’ is given as: \def \newpi{{\pi\mskip -7.8 mu \pi}} This, when rendered by pdfLaTeX, gives: Zoomed in, we can see the problems [...]]]></description>
			<content:encoded><![CDATA[<p>I just came across Bob Palais’ article from 2001, entitled <a href="http://www.math.utah.edu/~palais/pi.pdf">Pi is wrong!</a> He makes some very interesting points; however the notation he has chosen is rather unfortunate. The definition of his three-legged ‘2π’ is given as:</p>

<div class="wp_syntax"><div class="code"><pre class="latex" style="font-family:monospace;"><span style="color: #E02020; ">\</span><span style="color: #800000;">def</span> <span style="color: #800000; font-weight: normal;">\newpi</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">{<span style="color: #800000; font-weight: normal;">\pi</span><span style="color: #800000; font-weight: normal;">\mskip</span> -7.8 mu <span style="color: #800000; font-weight: normal;">\pi</span></span><span style="color: #E02020; ">}}</span></pre></div></div>

<p>This, when rendered by pdfLaTeX, gives:</p>
<p><img src='http://porg.es/blog/wp-content/uploads/2008/01/pii.png' alt='Pii' ></p>
<p>Zoomed in, we can see the problems with the symbol. I propose instead that the symbol defined as this should be used:</p>

<div class="wp_syntax"><div class="code"><pre class="latex" style="font-family:monospace;"><span style="color: #E02020; ">\</span><span style="color: #800000;">newcommand</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\cak</span></span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #800000; font-weight: normal;">\mathring</span><span style="color: #800000; font-weight: normal;">\pi</span></span><span style="color: #E02020; ">}</span></pre></div></div>

<p><img src='http://porg.es/blog/wp-content/uploads/2008/01/cak.png' alt='Cak' ></p>
<p>This symbol (rendered here in Zapf’s <a href="http://en.wikipedia.org/wiki/AMS_Euler">Euler</a>) displays nicely the idea of pi as a full circle—and it has a nice tie-in with ancient mathematics: <a href="http://en.wikipedia.org/wiki/Diophantus">Diophantus</a> used M̊ (an M with a ring above it) to represent the unit term in polynomial equations&#8230; we can think of pi as the ‘unit’ of angle.</p>
<p>Oh, and the name ‘cak’ is due to <a href="http://reddit.com/user/apotheon/">apotheon</a> on Reddit, because:</p>
<blockquote><p>As pi is to pie, so cak is to cake. Cakes are usually somewhere around twice as big as pies (in terms of height/volume), too.</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/2%cf%80-better-than-%cf%80/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Computing with XSLT</title>
		<link>http://porg.es/blog/computing-with-xslt</link>
		<comments>http://porg.es/blog/computing-with-xslt#comments</comments>
		<pubDate>Thu, 19 Jul 2007 02:34:18 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://porg.es/blog/computing-with-xslt</guid>
		<description><![CDATA[Since XSLT is basically a pattern-matching functional programming language, we should be able to use it to compute. I&#8217;m going to use it to implement one of the most basic functions: Peano-style addition. First of all we have to have an idea of what the numbers look like. We want to simulate this in XSL: [...]]]></description>
			<content:encoded><![CDATA[<p>Since XSLT is basically a pattern-matching functional programming language, we should be able to use it to compute. I&#8217;m going to use it to implement one of the most basic functions: Peano-style addition.</p>
<p>First of all we have to have an idea of what the numbers look like. We want to simulate this in XSL:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> Nat <span style="color: #339933; font-weight: bold;">=</span> Zero <span style="color: #339933; font-weight: bold;">|</span> Succ Nat</pre></div></div>

<p>Using XSD we can have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;complexType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Nat&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;choice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Zero&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Succ&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Nat&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/choice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><small>(Note that this is not tested, and probably wrong!)</small></p>
<p>However, since I don&#8217;t have an XSLT processor on hand that can deal with XSD types, I had to drop this course of action (although I still used the introduced type for the data). The type gives data like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;Zero</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&#8230;in this case standing for the number two. Now the idea of addition on these numbers is given by a function:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">add Zero y <span style="color: #339933; font-weight: bold;">=</span> y
add <span style="color: green;">&#40;</span>Succ x<span style="color: green;">&#41;</span> y <span style="color: #339933; font-weight: bold;">=</span> Succ <span style="color: green;">&#40;</span>add x y<span style="color: green;">&#41;</span></pre></div></div>

<p>Since this function involves pattern-matching it is an ideal candidate for implementation in XSLT, and it looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span> </span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- apply the add function to two numbers --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;*[1]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;*[2]&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- add Succ(x) y = Succ(add x y) --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;addSucc&quot;</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;Succ&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- add Zero y = y --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;addZero&quot;</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;Zero&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The first template matches any occurances of <code>&lt;add/&gt;</code>, and calls the real &#8216;add&#8217; function on them. It passes the second number as a parameter, while the first is what the function actually recurses on&mdash;exactly as in the Haskell example. The rest proceeds similarly.</p>
<p>You can <a href="http://www.w3.org/2005/08/online_xslt/xslt?xslfile=http%3A%2F%2Fporg.es%2Fprocessor.xsl&#038;xmlfile=http%3A%2F%2Fporg.es%2Fdata.xml&#038;content-type=&#038;submit=transform">test it yourself</a> by using the W3C online XSLT processor, which uses <a href="http://porg.es/processor.xsl">my processor</a> and <a href="http://porg.es/data.xml">some sample input data</a>. It should work for any similar data that you can throw at it as well. In this case it is adding 2 + 2 and coming up with 4, as we expect <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/computing-with-xslt/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Functors aren&#8217;t as hard as they sound</title>
		<link>http://porg.es/blog/functors-arent-as-hard-as-they-sound</link>
		<comments>http://porg.es/blog/functors-arent-as-hard-as-they-sound#comments</comments>
		<pubDate>Tue, 17 Jul 2007 01:45:34 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Types]]></category>

		<guid isPermaLink="false">http://porg.es/blog/functors-arent-as-hard-as-they-sound</guid>
		<description><![CDATA[Suppose we have a functor. Let&#8217;s call it &#8220;My Functor&#8221;, because that&#8217;s a nice friendly name. I&#8217;ll call it for short. Now, a functor is&#8212;according to higher sources&#8212;something which takes any type to a type . Let&#8217;s see how we can accomplish that with our functor, using Haskell code: data MyFunctor a = Construct a [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose we have a functor. Let&#8217;s call it &#8220;My Functor&#8221;, because that&#8217;s a nice friendly name. I&#8217;ll call it <img src='/blog/wp-content/plugins/latexrender/pictures/12c578c9f48dd6727464670d5daa0f9c.gif' title='MF' alt='MF' align=absmiddle> for short.</p>
<p>Now, a functor <img src='/blog/wp-content/plugins/latexrender/pictures/12c578c9f48dd6727464670d5daa0f9c.gif' title='MF' alt='MF' align=absmiddle> is&mdash;according to higher sources&mdash;something which takes any type <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle> to a type <img src='/blog/wp-content/plugins/latexrender/pictures/d643c19abe07b4b5d972095c2a2c90cf.gif' title='MF(a)' alt='MF(a)' align=absmiddle>. Let&#8217;s see how we can accomplish that with our functor, using Haskell code:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> MyFunctor a <span style="color: #339933; font-weight: bold;">=</span> Construct a</pre></div></div>

<p>This is quite simply a type constructor for our functor for any type <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle>. For example, we can use it on the Integer type as Construct Integer, which will give us the type MyFunctor Integer.</p>
<p>That&#8217;s half the definition of a functor. The other half is that it also converts any function <img src='/blog/wp-content/plugins/latexrender/pictures/392cfb6d2afe518b0a351a7d4561aa8b.gif' title='f \colon a \to b' alt='f \colon a \to b' align=absmiddle> to a function <img src='/blog/wp-content/plugins/latexrender/pictures/1cf4a6d3aabd63c18fd65fa17986f585.gif' title='MF(f) \colon MF(a) \to MF(b)' alt='MF(f) \colon MF(a) \to MF(b)' align=absmiddle>. This means for any function, it converts that function to work on stuff &#8220;inside&#8221; the functor.</p>
<p>In terms of our Haskell code, this means that we have a function that acts on another function to modify it. This function is usually called &#8220;fmap&#8221; (for &#8220;functor map&#8221; or something similar). This function has the type:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">fmap</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>MyFunctor a <span style="color: #339933; font-weight: bold;">-&gt;</span> MyFunctor b<span style="color: green;">&#41;</span></pre></div></div>

<p>It takes any function <img src='/blog/wp-content/plugins/latexrender/pictures/12261581da48b192397177dd2a6be281.gif' title='f\colon a\to b' alt='f\colon a\to b' align=absmiddle> to <img src='/blog/wp-content/plugins/latexrender/pictures/1cf4a6d3aabd63c18fd65fa17986f585.gif' title='MF(f) \colon MF(a) \to MF(b)' alt='MF(f) \colon MF(a) \to MF(b)' align=absmiddle>. Now we can define the function itself. This is the part which will be the most unique for our functor, and we can define it how we like. For our functor, this isn&#8217;t going to be very interesting:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">fmap</span> f <span style="color: green;">&#40;</span>MyFunctor a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> MyFunctor <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span></pre></div></div>

<p>We unwrap the object <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle> from our MyFunctor, apply the function <img src='/blog/wp-content/plugins/latexrender/pictures/8fa14cdd754f91cc6554c9e71929cce7.gif' title='f' alt='f' align=absmiddle>, then wrap it back up.</p>
<p>Now there are two laws that <img src='/blog/wp-content/plugins/latexrender/pictures/02bd1f8596236925b11b67bbb95c74eb.gif' title='fmap' alt='fmap' align=absmiddle> must satisfy in order for this to be a &#8220;real&#8221; functor. In Haskell these are:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#40;</span><span style="font-weight: bold;">fmap</span> f<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">fmap</span> g<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="font-weight: bold;">fmap</span> <span style="color: green;">&#40;</span>f <span style="color: #339933; font-weight: bold;">.</span> g<span style="color: green;">&#41;</span>
<span style="font-weight: bold;">id</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">fmap</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="font-weight: bold;">fmap</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">id</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="font-weight: bold;">fmap</span></pre></div></div>

<p>These can&#8217;t be enforced from &#8220;within&#8221; Haskell, but you must keep them in mind when writing your functor. If you&#8217;d like, you can check that these laws hold for our MyFunctor defined above.</p>
<p>Now we have our entire functor:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> MyFunctor a <span style="color: #339933; font-weight: bold;">=</span> Construct a
<span style="font-weight: bold;">fmap</span> f <span style="color: green;">&#40;</span>MyFunctor a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> MyFunctor <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span></pre></div></div>

<p>&#8230;and in fact, Haskell has a built-in class for functors:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> <span style="color: #cccc00; font-weight: bold;">Functor</span> f <span style="color: #06c; font-weight: bold;">where</span>
  <span style="font-weight: bold;">fmap</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>f a <span style="color: #339933; font-weight: bold;">-&gt;</span> f b<span style="color: green;">&#41;</span></pre></div></div>

<p>We can declare our MyFunctor to be a Functor like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <span style="color: #cccc00; font-weight: bold;">Functor</span> MyFunctor <span style="color: #06c; font-weight: bold;">where</span>
  <span style="font-weight: bold;">fmap</span> f <span style="color: green;">&#40;</span>MyFunctor a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> MyFunctor <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span></pre></div></div>

<p>&#8230;which still doesn&#8217;t seem very interesting.</p>
<p>An interesting functor is <img src='/blog/wp-content/plugins/latexrender/pictures/4ee29ca12c7d126654bd0e5275de6135.gif' title='List' alt='List' align=absmiddle>. This is defined as:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> List a <span style="color: #339933; font-weight: bold;">=</span> Empty <span style="color: #339933; font-weight: bold;">|</span> Cons a <span style="color: green;">&#40;</span>List a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: #cccc00; font-weight: bold;">Functor</span> List <span style="color: #06c; font-weight: bold;">where</span>
  <span style="font-weight: bold;">fmap</span> f <span style="color: green;">&#40;</span>Cons first rest<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> Cons <span style="color: green;">&#40;</span>f first<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">fmap</span> f rest<span style="color: green;">&#41;</span>
  <span style="font-weight: bold;">fmap</span> f Empty <span style="color: #339933; font-weight: bold;">=</span> Empty</pre></div></div>

<p>&#8230;wherein the functor takes a type <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle> to a list of <img src='/blog/wp-content/plugins/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.gif' title='a' alt='a' align=absmiddle>, and <img src='/blog/wp-content/plugins/latexrender/pictures/02bd1f8596236925b11b67bbb95c74eb.gif' title='fmap' alt='fmap' align=absmiddle> is the familiar map list function, only expressed in a functor context.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/functors-arent-as-hard-as-they-sound/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Surreal and natural numbers</title>
		<link>http://porg.es/blog/surreal-and-natural-numbers</link>
		<comments>http://porg.es/blog/surreal-and-natural-numbers#comments</comments>
		<pubDate>Tue, 17 Apr 2007 22:20:00 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Thought]]></category>

		<guid isPermaLink="false">http://porg.es/blog/surreal-and-natural-numbers</guid>
		<description><![CDATA[After reading a recent post on Good Math, Bad Math concerning surreal numbers, I got to thinking about how to model these in Haskell. I came up with the following formulation: data Surreal = Zero &#124; Plus Surreal &#124; Minus Surreal &#8230; which also seems very similar to the usual construction for natural numbers: data [...]]]></description>
			<content:encoded><![CDATA[<p>After reading <a href="http://scienceblogs.com/goodmath/2007/04/signexpanded_surreal_numbers_1.php">a recent post on Good Math, Bad Math concerning surreal numbers</a>, I got to thinking about how to model these in Haskell. I came up with the following formulation:</p>
<pre><code>data Surreal = Zero | Plus Surreal | Minus Surreal</code></pre>
<p>&#8230; which also seems very similar to the usual construction for natural numbers:</p>
<pre><code>data Nat = Zero | Succ Nat</code></pre>
<p><small>(I noted this in a comment on the original post.)</small></p>
<p>This got me thinking about how this construction (the sign-expanded version) mapped onto the original construction of the surreal numbers, which uses a left and a right set:</p>
<pre><code>data Surreal = Surreal [Surr] [Surr]</code></pre>
<p>These types translate to the following (in order of appearance):</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/eaa6c4cf2842115a727f9910baf907a1.gif' title='\begin{aligned}S &amp;= 1 + S + S\\&amp;= 1 + 2S\\N &amp;= 1 + N\\S &amp;= e^S \times e^S \\&amp;= e^{2S}\end{aligned}' alt='\begin{aligned}S &amp;= 1 + S + S\\&amp;= 1 + 2S\\N &amp;= 1 + N\\S &amp;= e^S \times e^S \\&amp;= e^{2S}\end{aligned}' align=absmiddle></p>
<p>This last type is suggestive for the natural numbers as well, and of course there is a analogous construction for the naturals:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/1bff22f7d420900aca0c5631b811d1b8.gif' title='N = e^N' alt='N = e^N' align=absmiddle></p>
<p>&#8230;which is a set-based construction for the naturals. (Note that in the above formulas multisets are used instead of true sets, but I don&#8217;t think this makes a difference in this context.)</p>
<p>Of course this raises the question of what is possible with three sets (or a 3-enum (3-num?)). More on this in another post.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/surreal-and-natural-numbers/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

