<?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; purity</title>
	<atom:link href="http://conal.net/blog/tag/purity/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>Notions of purity in Haskell</title>
		<link>http://conal.net/blog/posts/notions-of-purity-in-haskell</link>
		<comments>http://conal.net/blog/posts/notions-of-purity-in-haskell#comments</comments>
		<pubDate>Mon, 30 Mar 2009 19:00:48 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[purity]]></category>
		<category><![CDATA[referential transparency]]></category>
		<category><![CDATA[semantics]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=86</guid>
		<description><![CDATA[Lately I&#8217;ve been learning that some programming principles I treasure are not widely shared among my Haskell comrades. Or at least not widely among those I&#8217;ve been hearing from. I was feeling bummed, so I decided to write this post, in order to help me process the news and to see who resonates with what [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Notions of purity in Haskell

Tags: purity, referential transparency, semantics

URL: http://conal.net/blog/posts/notions-of-purity-in-haskell/

-->

<!-- references -->

<!-- teaser -->

<p>Lately I&#8217;ve been learning that some programming principles I treasure are not widely shared among my Haskell comrades.
Or at least not widely among those I&#8217;ve been hearing from.
I was feeling bummed, so I decided to write this post, in order to help me process the news and to see who resonates with what I&#8217;m looking for.</p>

<p>One of the principles I&#8217;m talking about is that the value of a closed expression (one not containing free variables) depends solely on the expression itself &#8212; not influenced by the dynamic conditions under which it is executed.
I relate to this principle as the soul of functional programming and of referential transparency in particular.</p>

<p><strong>Edits</strong>:</p>

<ul>
<li>2009-10-26: Minor typo fix</li>
</ul>

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

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

<p>Recently I encountered two facts about standard Haskell libraries that I have trouble reconciling with this principle.</p>

<ul>
<li>The meaning of <code>Int</code> operations in overflow situations is machine-dependent.  Typically they use 32 bits when running on 32-bit machine and 64 bits when running on 64-bit machines.  Implementations are free to use as few as 29 bits.  Thus the value of the expression &#8220;<code>2^32 == (0 ::Int)</code>&#8221; may be either <code>False</code> or <code>True</code>, depending on the dynamic conditions under which it is evaluated.</li>
<li>The expression &#8220;<code>System.Info.os</code>&#8221; has type <code>String</code>, although its value as a sequence of characters depends on the circumstances of its execution.  (Similarly for the other exports from <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/System-Info.html"><code>System.Info</code></a>.  Hm.  I just noticed that the module is labeled as &#8220;portable&#8221;.  Typo?  Joke?)</li>
</ul>

<p>Although I&#8217;ve been programming primarily in Haskell since around 1995, I didn&#8217;t realize that these implementation-dependent meanings were there.
As in many romantic relationships, I suppose I&#8217;ve been seeing Haskell not as she is, but as I idealized her to be.</p>

<p>There&#8217;s another principle that is closely related to the one above and even more fundamental to me: every type has a precise, specific, and preferably simple denotation.
If an expression <code>e</code> has type <code>T</code>, then the meaning (value) of <code>e</code> is a member of the collection denoted by <code>T</code>.
For instance, I think of the meaning of the type <code>String</code>, i.e., of <code>[Char]</code>, as being sequences of characters.
Well, not quite that simple, because it also contains some partially defined sequences and has a partial information ordering (non-flat in this case).
Given this second principle, if <code>os :: String</code>, then the meaning of <code>os</code> is some sequence of characters.
Assuming the sequence is finite and non-partial, it can be written down as a literal string, and that literal can be substituted for every occurrence of &#8220;<code>os</code>&#8221; in a program, without changing the program&#8217;s meaning.
However, <code>os</code> evaluates to &#8220;linux&#8221; on my machine and evaluates to &#8220;darwin&#8221; on my friend Bob&#8217;s machine, so substituting <em>any</em> literal string for &#8220;<code>os</code>&#8221; would change the meaning, as observable on at least one of these machines.</p>

<p>Now I realize I&#8217;m really talking about standard Haskell <em>libraries</em>, not Haskell itself.
When I <a href="http://tunes.org/~nef/logs/haskell/09.03.29">discussed my confusion &amp; dismay in the #haskell chat room</a>, someone suggested explaining these semantic differences in terms of different libraries and hence different programs (if one takes programs to include the libraries they use).
One would not expect different programs (due to different libraries) to have the same meaning.</p>

<p>I understand this different-library perspective &#8212; in a literal way.
And yet I&#8217;m not really satisfied.
What I get is that standard libraries are &#8220;standard&#8221; in signature (form), not in meaning (substance).
With no promises about semantic commonality, I don&#8217;t know how standard libraries can be useful.</p>

<p>Another perspective that came up on #haskell was that the kind of semantic consistency I&#8217;m looking for is <em>impossible</em>, because of possibilities of failure.
For instance, evaluating an expression might one time fail due to memory exhaustion, while succeeding (perhaps just barely) on another attempt.
After mulling over that point, I&#8217;d like to weaken my principle a little.
Instead of asking that all evaluations of an expression yield <em>same</em> value, I ask that all evaluations of an expression yield <em>consistent</em> answers.
By &#8220;consistent&#8221; I mean in the sense of information content.
<em>Answers don&#8217;t have to agree, but they must not disagree.</em>
Failures like exhausted memory are modeled as ⊥, which is called &#8220;bottom&#8221; because it is the bottom of the information partial ordering.
It contains no information and so is consistent with every value, disagreeing with no value.
More precisely, values are <em>consistent</em> when they have a shared upper (information) bound, and <em>inconsistent</em> when they don&#8217;t.
The value ⊥ means <em>i-don&#8217;t-know</em>, and the value <code>(1,⊥,3)</code> means (1, <em>i-don&#8217;t-know</em>, 3).
The consistent-value principle accepts possible failures due to finite resources and hardware failure, while rejecting &#8220;linux&#8221; vs &#8220;darwin&#8221; for <code>System.Info.os</code> or False vs True for &#8220;<code>2^32 == (0 ::Int)</code>.
It also accepts <code>System.Info.os :: IO String</code>, which is the type I would have expected, because the semantics of <code>IO String</code> is big enough to accommodate dependence on dynamic conditions.</p>

<p>If you also cherish the principles I mention above, I&#8217;d love to hear from you.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=86&amp;md5=9ff66b6506b3b491599ed696fdd04e2d"><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/notions-of-purity-in-haskell/feed</wfw:commentRss>
		<slash:comments>55</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%2Fnotions-of-purity-in-haskell&amp;language=en_GB&amp;category=text&amp;title=Notions+of+purity+in+Haskell&amp;description=Lately+I%26%238217%3Bve+been+learning+that+some+programming+principles+I+treasure+are+not+widely+shared+among+my+Haskell+comrades.+Or+at+least+not+widely+among+those+I%26%238217%3Bve+been+hearing+from.+I...&amp;tags=purity%2Creferential+transparency%2Csemantics%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
