<?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; Types</title>
	<atom:link href="http://porg.es/blog/tag/types/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description></description>
	<lastBuildDate>Sun, 06 May 2012 22:13:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<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>NullPointerException</title>
		<link>http://porg.es/blog/nullpointerexception</link>
		<comments>http://porg.es/blog/nullpointerexception#comments</comments>
		<pubDate>Thu, 24 May 2007 21:26:41 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Critique]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Thought]]></category>
		<category><![CDATA[Types]]></category>

		<guid isPermaLink="false">http://porg.es/blog/nullpointerexception</guid>
		<description><![CDATA[Why is this such an insidious error in Java? (An opinion piece!) A Comparison Firstly, I&#8217;ll show a short comparison between some Java code and some code from a language that doesn&#8217;t have NullPointerExceptions, but does have something that allows you to accomplish anything you might want to do with null pointers. This Java code: [...]]]></description>
			<content:encoded><![CDATA[<p>Why is this such an insidious error in Java? (An opinion piece!)</p>
<h3>A Comparison</h3>
<p>Firstly, I&#8217;ll show a short comparison between some Java code and some code from a language that doesn&#8217;t have <code>NullPointerException</code>s, but does have something that allows you to accomplish anything you might want to do with null pointers.</p>
<p>This Java code:</p>
<pre><code>AnObject thing = someMethod();</code></pre>
<p>is equivalent to the following Haskell:</p>
<pre><code>let thing = Maybe AnObject</code></pre>
<p>That is, every Java object reference is possibly null. What this means is that (using the type notation mentioned in earlier posts) every Java reference has the type:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/80c7aa7402953bdabc5fcf23a7a9b406.gif' title='ObjectReference[Object] = 1 + Object' alt='ObjectReference[Object] = 1 + Object' align=absmiddle></p>
<p><small>(Every object reference for an object is either to &#8216;null&#8217; (<abbr title="also known as">AKA</abbr> &#8216;unit&#8217; or &#8216;singleton&#8217<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" />, or the object itself.)</small></p>
<p>&#8230; and when you&#8217;re dealing with so many objects—Java is, after all, an object-oriented language, so (almost) everything is an object—this simple little &#8220;+1&#8243; gets lost easily.</p>
<h3>Why so bad?</h3>
<p>This wouldn&#8217;t seem to be such a big deal, but when contrasted against Java&#8217;s penchant for making things (sometimes painfully) explicit—think <code>public static void main (string argh!)</code>—a small, implicit item like this is easily overlooked. Conversely, one might say that the verbosity of Java <em>increases</em> the cognitive load of understanding its code; thus helping the chances of a small mistake like this sneaking through.</p>
<p>Either way, I think I can say that this is objectively A Bad Thing. C# has gone some of the way to a &#8216;correct&#8217; solution in its introduction of possibly-null primitives, which are explicitly marked with a <code>?</code> (such as <code>int?</code>), but unfortunately this hasn&#8217;t been &#8216;back-ported&#8217; to the rest of the language. If one were able to force between possibly-null and definitely-not-null references in C#—perhaps by the analogous <code>object?</code> vs. <code>object</code>, this would help to reduce the number of errors &#8216;hidden&#8217; by the syntax.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/nullpointerexception/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

