<?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; belief</title>
	<atom:link href="http://conal.net/blog/tag/belief/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>&#8220;Everything is a function&#8221; in Haskell?</title>
		<link>http://conal.net/blog/posts/everything-is-a-function-in-haskell</link>
		<comments>http://conal.net/blog/posts/everything-is-a-function-in-haskell#comments</comments>
		<pubDate>Wed, 04 Aug 2010 22:22:39 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[belief]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=175</guid>
		<description><![CDATA[There a belief about Haskell that keeps popping up in chat rooms and mailing lists &#8212; one that I&#8217;ve been puzzling over for a while. One expression of the belief is &#8220;everything is a function&#8221; in Haskell. Of course, there are all of these non-functions that need to be accounted for, including integers, booleans, tuples, [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: "Everything is a function" in Haskell?

Tags: belief

URL: http://conal.net/blog/posts/everything-is-a-function-in-haskell/

-->

<!-- references -->

<!-- teaser -->

<p>There a belief about Haskell that keeps popping up in chat rooms and mailing lists &#8212; one that I&#8217;ve been puzzling over for a while.
One expression of the belief is &#8220;everything is a function&#8221; in Haskell.</p>

<p>Of course, there are all of these non-functions that need to be accounted for, including integers, booleans, tuples, and lists.
What about them?
A recurring answer is that such things are &#8220;functions of no arguments&#8221; or functions of a one-element type or &#8220;constant functions&#8221;.</p>

<p>I wonder about how beliefs form, spread, and solidify, and so I asked around about how people came to this notion and how they managed to hold onto it.
I had a few conjectures in mind, which I kept to myself to avoid biasing people&#8217;s responses.
Of the responses I got, some were as I&#8217;d imagined, and some were quite surprising to me, revealing some of my blind spots about others&#8217; thinking and about conversation dynamics.</p>

<p>My thanks to the many Haskellers, especially newbies, who took the time to help me understand their thought processes.
If you&#8217;re interested and in a patient mood, you can see the unedited responses on a <a href="http://www.reddit.com/r/haskell/comments/cs3gw/so_im_eating_breakfast_this_morning_posting_about/c0uu70s">Haskell reddit thread</a> and on <a href="http://tunes.org/~nef/logs/haskell/10.07.22">a #haskell IRC log</a>.
There were also a few responses on Twitter.</p>

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

<ul>
<li>2009-08-04: Added &#8220;simplify&#8221;: &#8220;Would making everything a function really simplify the formal system that is Haskell programming?&#8221;.  Thanks, SLi.</li>
<li>2009-08-04: Focus on &#8220;constant function&#8221; story for &#8220;It makes things simpler&#8221;.  I realized that I hadn&#8217;t said what I intended there.  Thanks, Jonathan Cast.</li>
<li>2011-03-04: Remarks on mutability &amp; dynamic typing, under &#8220;Operational thinking&#8221;</li>
</ul>

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

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

<h3>Analogy with object-oriented programming</h3>

<blockquote>
  <p>Hmm&#8230; I think it was because I was originally taught &#8216;everything is an object&#8217; in OOP.</p>
</blockquote>

<p>Many people come to functional programming (FP) after object-oriented programming (OOP).
One may have heard that in a &#8220;pure OOP&#8221; language, everything is an object.
By analogy, one could then wonder whether in a &#8220;pure FP&#8221; language, everything is a function, i.e., whether function is to functional as object is to object-oriented.</p>

<p>Perhaps such an analogy sounds plausible at first, and some people don&#8217;t give it critical thought to test whether this first guess fits with reality.
Or perhaps they&#8217;ve heard someone speak with confidence, and they confused confidence with understanding.</p>

<blockquote>
  <p>To try to answer the question about the development of beliefs: I think part of it has to do with newcomers hearing the old-hands saying it, and so it becomes part of the dogma they learn.</p>
</blockquote>

<h3>It makes things simpler</h3>

<blockquote>
  <p>It&#8217;s a unification (like electro-weak interaction). Having only one kind of things is better than having two.</p>
</blockquote>

<p>Many of us like simple, unifying principles, and we dislike exceptions.
Fewer building blocks and fewer exceptions often leads to a simpler, more compelling precise formulation, which supports more tractable reasoning (mechanized in compilers and non-mechanized in programmers).</p>

<p>This reason is perhaps in the realm of wishful thinking.
As in &#8220;it would be nice if everything were a function in Haskell, since things would be simpler that way&#8221;.</p>

<p>Would making everything a function really simplify the formal system that is Haskell programming?
For instance, what would be the impact on the type rules?</p>

<p>First, let&#8217;s separate the nullary and constant-function perspectives.
The (formal) type system has no notion of functions other than unary, so I&#8217;ll focus on the constant-function story.
For instance, that &#8220;<code>7</code>&#8221; is shorthand for &#8220;<code>() -&gt; 7</code>&#8220;, and so &#8220;<code>x = 7</code>&#8221; means &#8220;<code>x =  () -&gt; 7</code>&#8220;, i.e., &#8220;<code>x () = 7</code>&#8221; (adopting the usual syntactic sugar).
(<em>Edit:</em> added this paragraph.)</p>

<p>If we did decide that values of non-function type are constant functions, then what order functions?
1st, 2nd, 3rd, &#8230;?
In other words, if 7 is a nullary or constant function, what is the result type of that function?
If the answer is, say, <code>Integer</code>, then we&#8217;d have to recall that <code>Integer</code> really means nullary or constant function whose result type is <code>Integer</code>, ad infinitum.
In a first-order functional language, this troubling question would not arise, and for imperative programmers (including OOP-ers), functions are a mainly first-order concept.</p>

<blockquote>
  <p>Why complicate your universe with non-functions when functions naturally generalize to cover them?</p>
</blockquote>

<p>Similarly, why complicate your universe with functions when non-functions naturally generalize to cover them? For instance, every value (including functions) can be promoted to a list, tree, Maybe, pair,  Id (identity <code>newtype</code> wrapper), etc.</p>

<p>But then I wonder what type of thing is in those lists, trees, maybes, etc, and what type of thing is returned from those functions.</p>

<p>And I do like static typing, which is lost when one wraps up everything into a universal type.  For instance, we lose knowing that the list or tree has exactly one element, that the Maybe is a Just, and that the function is a constant function.</p>

<h3>Mixing up functions and definitions</h3>

<p>Many times, I&#8217;ve seen Haskell programmers (perhaps mainly newbies) use the word &#8220;function&#8221; to mean &#8220;top-level definition&#8221;.
Which surprises me, as the meaning I attach to &#8220;function&#8221; has nothing to do with &#8220;top-level&#8221; or with &#8220;definition&#8221;.</p>

<p>For instance,</p>

<blockquote>
  <p>&#8216;y = 3&#8242; can be considered as a function with no arguments that returns just 3.</p>
</blockquote>

<p>I guess what&#8217;s going on here is a conflation of <em>functions</em> and (top-level) <em>definitions</em>.
Maybe because these two notions are tightly connected in C programming, where functions are always named (defined) and always at the top level and are always immutable, whereas non-functions can be defined in a nested scope and historically were mutable (before <code>const</code>).</p>

<h3>Church&#8217;s lambda calculus</h3>

<blockquote>
  <p>Well, blame Church.</p>
</blockquote>

<p>In Church&#8217;s original lambda calculus, everything is a function.
For instance,</p>

<pre><code>type Bool = ∀ a. a -&gt; a -&gt; a

false, true :: Bool
true  t e = t
false t e = e

type Nat  = ∀ a. (a -&gt; a) -&gt; (a -&gt; a)

three :: Nat
three f = f ∘ f ∘ f
</code></pre>

<p>Do some folks believe we&#8217;re still doing what Church did, i.e., to encode all data as functions and build all types out of <code>-&gt;</code>?</p>

<h3>Operational thinking</h3>

<blockquote>
  <p>Haskell&#8217;s laziness by default was what made me think of constants as functions that take no parameters.</p>
</blockquote>

<p>A <em>thunk</em> at the operational level (language implementation) is vaguely reminiscent of a <em>function</em> at the level of language specification and semantics.</p>

<p>This bit of operational reasoning had not occurred to me as a source of thinking that even non-functions are &#8220;functions&#8221;.
I was looking for denotational reasons, i.e., what things mean rather than how an implementation might work.
One of my recurring blind spots when trying to understand other&#8217;s thought processes is this sort of operational-by-default thinking.</p>

<p>(<em>Edit:</em> added the following paragraph.)</p>

<p>Lazy evaluation replaces thunks by more evaluated forms (pure values for simple types but otherwise an evaluated outer-wrapper and possibly unevaluated inner contents, i.e., weak head normal form).
That replacement is done <em>destructively</em>, i.e., a side-effect of evaluation.
So the thunk-focused operational-thinking explanation of &#8220;everything is a function in Haskell&#8221; leads to the interesting conclusion that Haskell has pervasive (and difficult to predict) side-effects and that these side-effects cause changes to <em>types</em>, not just to values (going from function to non-function).
That&#8217;s a lot of mutability and dynamic typing for a purely functional, statically typed language!</p>

<h3>Interested in a different conversation</h3>

<p>This one was the hardest of all for me to get so far and was a real lightbulb moment for some people&#8217;s belief that everything is a function in Haskell.
They don&#8217;t believe it.
They&#8217;d rather talk about something else, such as formalisms in which everything is a function.
Or maybe they even have a different notion of what topic is being discussed.</p>

<p>And I realize I often do the same thing.
I hear a discussion topic that overlaps with one interesting to me (such as how do people form, spread, and solidify beliefs), and I focus more on the topic that interests me and less on the original topic.
And if someone doesn&#8217;t notice the switch, they can get confused.</p>

<p>Wow &#8212; this reason was in another blind spot for me.</p>

<h3>Is everything a function in Haskell?</h3>

<blockquote>
  <p>conal, that&#8217;s a funny one.  in Haskell, static types give a pretty sharp objective determination of what is a function and what isn&#8217;t</p>
</blockquote>

<p>Haskell has a precisely specified type system that defines what it means for (the value of) an expression to have function type.</p>

<h3>Reasons vs rationalizations</h3>

<blockquote>
  <p>Most of our so-called reasoning consists in finding arguments for going on believing as we already do. (James Harvey Robinson)</p>
</blockquote>

<p>Although I keep hearing &#8220;everything is a function&#8221; (and 3 is a nullary or constant function), I don&#8217;t hear people say &#8220;everything is a list&#8221;, and <code>3</code> is really the singleton list <code>[3]</code>.
Or &#8220;everything is a pair&#8221;, and <code>7</code> is really <code>(7,⊥)</code> or some such.
Or &#8220;everything is a <code>Maybe</code>&#8220;, and <code>True</code> is really <code>Just True</code>.
Personally I don&#8217;t like to equate non-functions (number, bools, trees, etc) with 0-ary functions any more than I to equate them with singleton lists (or trees, &#8230;) or non-<code>Nothing</code> <code>Maybe</code> values, etc.</p>

<p>So my best guess is that a statement like &#8220;well, 3 is a constant function&#8221; is not so much a reason as a rationalization.
In other words, not so much a means of arriving at a belief as a means of holding onto belief in the face of evidence to the contrary.
After all, one could use the same style of &#8220;reasoning&#8221; to arrive at quite different beliefs, namely that 3 is a list, pair, or <code>Maybe</code> in Haskell.
On the other hand, if the task at hand is to solidify a given belief rather than arrive at an understanding, then one would be less likely to notice that a given rationalization can be easily adapted to alternative beliefs.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=175&amp;md5=6de0f8be3bafaca461be28065ab6b085"><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/everything-is-a-function-in-haskell/feed</wfw:commentRss>
		<slash:comments>53</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%2Feverything-is-a-function-in-haskell&amp;language=en_GB&amp;category=text&amp;title=%26%238220%3BEverything+is+a+function%26%238221%3B+in+Haskell%3F&amp;description=There+a+belief+about+Haskell+that+keeps+popping+up+in+chat+rooms+and+mailing+lists+%26%238212%3B+one+that+I%26%238217%3Bve+been+puzzling+over+for+a+while.+One+expression+of+the+belief...&amp;tags=belief%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
