<?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; philosophy</title>
	<atom:link href="http://conal.net/blog/tag/philosophy/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>The C language is purely functional</title>
		<link>http://conal.net/blog/posts/the-c-language-is-purely-functional</link>
		<comments>http://conal.net/blog/posts/the-c-language-is-purely-functional#comments</comments>
		<pubDate>Wed, 20 May 2009 01:19:21 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[philosophy]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=87</guid>
		<description><![CDATA[There has been a scurry of reaction on twitter and reddit to Robert Fischer&#8217;s post saying that Scala is Not a Functional Programming Language. James Iry responded by saying that analogous reasoning leads to the conclusion that Erlang is Not Functional My first inclination was to suggest that Haskell, as commonly practiced (with monadic IO), [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: The C language is purely functional

Tags: philosophy, monad, history

URL: http://conal.net/blog/posts/the-c-language-is-purely-functional/

-->

<!-- references -->

<!-- teaser -->

<p>There has been a scurry of reaction on twitter and reddit to Robert Fischer&#8217;s post saying that <a href="http://www.reddit.com/goto?id=8kl8l">Scala is Not a Functional Programming Language</a>.
James Iry responded by saying that analogous reasoning leads to the conclusion that <a href="http://www.reddit.com/goto?id=8krbo">Erlang is Not Functional</a></p>

<p>My first inclination was to suggest that Haskell, as commonly practiced (with monadic IO), is not a functional language either.
Instead, I&#8217;m going to explain how it is that <em>the C language is purely functional</em>.</p>

<!--
**Edits**:

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

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

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

<h3>The story of C</h3>

<p>First, let me make that claim more precise.
What I really mean is that almost everyone who &#8220;writes C programs&#8221; is really programming in a purely functional language.
Modulo some minor historical accidents.</p>

<p>&#8220;C programmers&#8221; really program not in C, but in the purely functional language <em>cpp</em> (<a href="http://en.wikipedia.org/wiki/C_preprocessor">the &#8220;C Preprocessor&#8221;</a>).
As with any purely functional language, cpp consists only of (nestable) <em>expressions</em>, not statements.
Now here&#8217;s the clever part: when <em>evaluated</em> (not executed), a cpp expression yields a (pure) value of type <code>C</code>, which is an ADT (abstract data type) that represents imperative programs.
That value of type <code>C</code> is then <em>executed</em> (not evaluated) by the cpp language&#8217;s RTS (run-time system).
As a clever optimization, cpp&#8217;s RTS actually includes a code generator, considerably improving performance over more naïve implementations of the <code>C</code> abstract data type.</p>

<p>(So now you can see that, &#8220;C programmer&#8221; is almost always a misnomer when applied to a person.
Instead people are cpp programmers, and cpp programs are the real C programmers (producers of <code>C</code>).
I don&#8217;t think I can buck this misnomer, so I&#8217;ll stay with the herd and use &#8220;C programming&#8221; to mean cpp programming.)</p>

<p>In this way, the geniuses Brian Kernighan and Dennis Ritchie applied monads (from category theory) to programming language design considerably before the work of Moggi and Wadler.
(Even earlier examples of this idea can be found, so I&#8217;m taking liberty with history, choosing to give K&amp;R credit for the sake of this post.)</p>

<p>Now along the way, some mistakes were made that tarnished the theoretical beauty of cpp+C:</p>

<ul>
<li><p>Although cpp is in spirit purely functional, pragmatists insisted on the unnecessary and theoretically awkward construct <code>#undef</code>, which allows a single name to be re-defined.</p></li>
<li><p>Scoping rules are not as clean as, say, the lambda calculus or Scheme,  (But hey &#8212; even John McCarthy got scoping wrong.)</p></li>
<li><p>The <code>C</code> ADT is implemented simply as <code>String</code> (or <code>char *</code>, for you type theorists, using a notation from Kleene), and the representation is <em>exposed</em> rather than hidden, so that cpp programs operate directly on strings.  The wisdom of data abstraction caught on later.</p></li>
</ul>

<p>Fortunately for purists, the relatively obscure programming language <a href="http://haskell.org">Haskell</a> restored the original untarnished beauty of K&amp;R&#8217;s vision by fixing these defects.
The type system was modernized, scoping rules improved, and the <code>C</code> type (renamed to &#8220;<code>IO</code>&#8220;, to avoid expensive legal battles) made abstract.</p>

<p>(Admittedly, Haskell also had some shameful pre-teen influences from hooligans like Church, Curry, Landin, and Milner, before it settled into its true cpp+C nature, under the guiding influence of Wadler (under the influence of Moggi).)</p>

<h3>What about Haskell?</h3>

<p>Which leads to the question: is Haskell+IO a purely functional programming language?</p>

<p>Sure it is, even as much as its predecessor C (more precisely, cpp+C).</p>

<p>Some who ought to know better would claim that Haskell+IO is only <em>technically</em> pure.
(See <a href="http://www.reddit.com/r/programming/comments/8krbo/erlang_is_not_functional_response_to_scala_is_not/c09lqfi">claims and follow-up discussion</a>, in response to a remark from James Iry.)</p>

<p>I must then respond that, in that case, <em>even C programming</em> is only technically pure.</p>

<p>Which is absurd.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=87&amp;md5=a592089bcedaab4dfa655e3347d0dcc0"><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/the-c-language-is-purely-functional/feed</wfw:commentRss>
		<slash:comments>43</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%2Fthe-c-language-is-purely-functional&amp;language=en_GB&amp;category=text&amp;title=The+C+language+is+purely+functional&amp;description=There+has+been+a+scurry+of+reaction+on+twitter+and+reddit+to+Robert+Fischer%26%238217%3Bs+post+saying+that+Scala+is+Not+a+Functional+Programming+Language.+James+Iry+responded+by+saying+that...&amp;tags=history%2Cmonad%2Cphilosophy%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
