<?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</title>
	<atom:link href="http://conal.net/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://conal.net/blog</link>
	<description>Inspirations &#38; experiments, mainly about denotational programming in Haskell</description>
	<lastBuildDate>Wed, 03 Feb 2010 17:10:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Blog upgraded.  RSS fixed.</title>
		<link>http://conal.net/blog/posts/blog-upgraded-rss-fixed/</link>
		<comments>http://conal.net/blog/posts/blog-upgraded-rss-fixed/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 01:56:22 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[blog]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=99</guid>
		<description><![CDATA[





I&#8217;ve heard from a few people that my blog&#8217;s RSS feeds were broken.
I just upgraded my blog to Wordpress 2.9.1, which appears to have fixed the problem with RSS generation.

If you notice any problems with RSS or anything else, please let me know.
]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Blog upgraded.  RSS fixed.

Tags: blog

URL: http://conal.net/blog/posts/blog-upgraded-rss-fixed/

-->

<!--
**Edits**:

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

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

<p>I&#8217;ve heard from a few people that my blog&#8217;s RSS feeds were broken.
I just upgraded my blog to Wordpress 2.9.1, which appears to have fixed the problem with RSS generation.</p>

<p>If you notice any problems with RSS or anything else, please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/blog-upgraded-rss-fixed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is Haskell a purely functional language?</title>
		<link>http://conal.net/blog/posts/is-haskell-a-purely-functional-language/</link>
		<comments>http://conal.net/blog/posts/is-haskell-a-purely-functional-language/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 23:08:31 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=98</guid>
		<description><![CDATA[



There is a lot of confusion about the meaning of &#8220;functional&#8221; and &#8220;declarative&#8221; as descriptions of programming languages and paradigms.
For instance, Haskell is sometimes advertised as a &#8220;purely functional programming language&#8221; and other times as &#8220;the world&#8217;s finest imperative programming language&#8221;.
I added some playful confusion (and clarity, I hope) with my post The C language [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Is Haskell a purely functional language?

Tags: 

URL: http://conal.net/blog/posts/is-haskell-a-purely-functional-language/


Alternative titles:

Is monadic IO functional?

What is a purely functional language?

-->

<!-- references -->

<p>There is a lot of confusion about the meaning of &#8220;functional&#8221; and &#8220;declarative&#8221; as descriptions of programming languages and paradigms.
For instance, Haskell is sometimes advertised as a <a href="http://haskell.org">&#8220;purely functional programming language&#8221;</a> and other times as <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.13.9123">&#8220;the world&#8217;s finest imperative programming language&#8221;</a>.
I added some playful confusion (and clarity, I hope) with my post <em><a href="http://conal.net/blog/posts/the-c-language-is-purely-functional/" title="blog post">The C language is purely functional</a></em>.</p>

<p>I still regularly hear people ask and disagree about whether Haskell&#8217;s monadic I/O is &#8220;functional&#8221;.
I would say yes in one (technical) sense of &#8220;functional&#8221;.
But not the same sense in which we say &#8220;functional programming has simple, precise semantics&#8221; or &#8220;functional programming has good compositional properties&#8221; or &#8220;functional programming is good for reasoning about&#8221;.
Monadic I/O is a clever trick for encapsulating sequential, imperative computation, so that it can &#8220;do no evil&#8221; to the part that really does have precise semantics and good compositional properties.</p>

<p>It&#8217;s because of this confusion that I&#8217;ve started using the more specific term &#8220;denotational programming&#8221; in my blog subtitle and elsewhere, as an alternative to what I used to call &#8220;functional programming&#8221;.
While there are other notions of &#8220;functional&#8221;, applicable even to monadic IO, I think &#8220;denotational&#8221; captures the fundamental and far-reaching benefits that we called &#8220;good for reasoning about&#8221; and &#8220;powerfully compositional&#8221;.</p>

<p>When I bash monadic I/O, my real complaint isn&#8217;t with the technical invention&#8211;which I like.
Rather, I&#8217;m cranky about confusion and misleading communication, and about distraction from what we originally meant by &#8220;functional programming&#8221;&#8211;from what I call &#8220;denotational programming&#8221;.</p>

<p>I don&#8217;t mind that we haven&#8217;t yet been <a href="http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm/" title="blog post">liberated from the von Neumann model</a>.
As long as <em>remember</em> we haven&#8217;t.</p>

<p>As long as we keep on searching.</p>

<!--
**Edits**:

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

<!-- without a comment or something here, the last item above becomes a paragraph -->
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/is-haskell-a-purely-functional-language/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Can functional programming be liberated from the von Neumann paradigm?</title>
		<link>http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm/</link>
		<comments>http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 18:32:59 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[vision]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=97</guid>
		<description><![CDATA[





This post describes an idea I&#8217;ve had in mind for years and have chatted about with friends but haven&#8217;t written down before.

Functional programmers used to worry about how to solve &#8220;the I/O problem&#8221;.
Functional programming (FP) eschews any notion of side-effect or order of execution, and instead has an order-independent notion of evaluation.
In contrast, input &#38; [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Can functional programming be liberated from the von Neumann paradigm?

Tags: vision

URL: http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm/

-->

<!-- references -->

<!-- teaser -->

<p>This post describes an idea I&#8217;ve had in mind for years and have chatted about with friends but haven&#8217;t written down before.</p>

<p>Functional programmers used to worry about how to solve &#8220;the I/O problem&#8221;.
Functional programming (FP) eschews any notion of side-effect or order of <em>execution</em>, and instead has an order-independent notion of <em>evaluation</em>.
In contrast, input &amp; output (I/O) seems to be an inherently effectful, ordered notion.</p>

<p>For a long time, I&#8217;ve been dissatisfied with the current Haskell solution to &#8220;the I/O problem&#8221;.
Of course it&#8217;s very clever and effective.
With &#8220;monadic IO&#8221; we can program both functionally and imperatively, and the imperative semantics doesn&#8217;t compromise the functional semantics.</p>

<p>I look at monadic IO not as any solution to functional I/O, but rather as a (clever) way <em>not</em> to bring I/O into the functional world.
There&#8217;s a ton of confusion buzzing around monadic IO, so let me try to restate more carefully:
&#8220;monadic IO&#8221; is a way to <em>generate</em> imperative computations (not just I/O) without having to say anything at all about the semantic content of those computations.
Rather than retooling our mental habits completely away from the imperative model of computation, monadic IO is a way to hang onto these old mental habits, and use them in a hybrid functional/imperative programming style, in which the imperative doesn&#8217;t contaminate the functional, semantically.</p>

<p>John Backus offered us a gift and a blessing in his 1977 Turing Award lecture, <em><a href="http://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf" title="Turing Award lecture by John Backus">Can Programming Be Liberated from the von Neumann Style?  A functional style and its algebra of programs</a></em>.  He told us how the sequential/imperative model enslaves us in weak thoughts, inhibiting manageable scaling (sections 1 through 5.1).
In the abstract, Backus says</p>

<blockquote>
  <p>Conventional programming languages are growing ever more enormous, but not stronger. Inherent defects at the most basic level cause them to be both fat and weak: their primitive word-at-a-time style of programming inherited from their common ancestor&#8211;the von Neumann computer, their close coupling of semantics to state transitions, their division of programming into a world of expressions and a world of statements, their inability to effectively use powerful combining forms for building new programs from existing ones, and their lack of useful mathematical properties for reasoning about programs.</p>
</blockquote>

<p>As well, Backus showed one way to throw off the shackles of this mental and expressive slavery &#8212; how to <em>liberate</em> our notions of programming by leaving the von Newmann mental model behind.</p>

<p>Haskell is often described as a &#8220;purely functional programming language&#8221;, and in a technical sense it is.
(In good company, too; see <em><a href="http://conal.net/blog/posts/the-c-language-is-purely-functional/" title="blog post">The C language is purely functional</a></em>.)
However, as commonly practiced, imperative computation (<code>IO</code>) still plays a significant role in most Haskell programs.
Although monadic IO is wily enough to keep a good chunk of purity in our model, Haskell programmers still use the imperative model as well and therefore have to cope with the same fundamental difficulties that Backus told us about.
In a sense, we&#8217;re essentially still programming in Fortran, with its mixture of statements and expressions (semantically, of commands and values).
Don&#8217;t get me wrong; Haskell is a <em>much better</em> Fortran than the original.
(If you have an urge to inform me how monadic IO works, re-read what I&#8217;m really saying.  Honestly, I know.)</p>

<p>(Sadly, the very definition of &#8220;program&#8221; in Haskell requires that there be a <code>main</code> of type <code>IO ()</code>!
Haskell programs thus cannot be purely functional (except technically), and so cannot be composed except very weakly, as Backus describes.
For an alternative model, see 
<em><a href="http://conal.net/blog/posts/tangible-functional-programming-a-modern-marriage-of-usability-and-composability/" title="blog post">Tangible Functional Programming: a modern marriage of usability and composability</a></em>.)</p>

<p>In a sense, I see us as in a worse position than before.
Since the adoption of monadic IO, it&#8217;s been less immediately apparent that we&#8217;re still enslaved, so there&#8217;s been much less activity in searching for the promised land that the prophet JB foretold.
Our current home is not painful enough to goad us onward, as were continuation-based I/O and stream-based I/O.
(See <a href="http://research.microsoft.com/~simonpj/papers/history-of-haskell/">A History of Haskell</a>, section 7.1.)
Nor does it fully liberate us.</p>

<p>Since I/O is an intrisically sequential, effectful notion, and we need some way of doing input &amp; output, I guess the best we can do, is what we&#8217;ve done with monadic I/O: support both imperative and functional programming and proctect the functional from the imperative.</p>

<p>Or can we?</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-97"></span></p>

<p>In my recent post, <em><a href="http://conal.net/blog/posts/why-program-with-continuous-time/" title="blog post">Why program with continuous time?</a></em>, I had suggested that the abstraction of continuous time is useful even if &#8220;in the end all streams are discrete&#8221;.
I advised designing APIs based on the principle that &#8220;programming is more about the middle than the end, i.e., more about composition than about output.&#8221;
As an example, I pointed out that we don&#8217;t represent numbers as strings even if &#8220;in the end&#8221;, numbers are output as strings, suggesting that the key reason to choose an abstraction other than strings is composability (here, arithmetic).  The post gives a few other examples of this principle of designing an API for composition, not for input or output.</p>

<p>Roly Perera noted that</p>

<blockquote>
  <p>&#8230; you never really need to reach “the end”. (It really is all about composition.)</p>
</blockquote>

<p>To extend an example in the previous post, after numbers, then strings, and then pixels, are phosphors the end?
(Sorry for the obsolete technology.)
After phosphors come photons.
After photons comes retina &amp; optic nerve.
Then cognitive processing (and emotional and who-knows-what-else).
Then, via motor control, to mouse, keyboard, joystick etc.
Then machine operating system, and software again.
Round &amp; round.
More importantly, our interactions with other wetware organisms and with our planet and cosmos, and so on.</p>

<p>Roly added:</p>

<blockquote>
  <p>What looks like imperative output can be just what you observe at the boundary between two subsystems.</p>
</blockquote>

<p>Which is exactly how I look at imperative input/output.</p>

<p>Representation shifts often happen at programming interfaces.
<em>Major</em> interfaces in a whole system sometimes require a major representation shift, namely marshalling to and unmarshalling from serialized, memory-external representations.
For instance, conversion from strings to numbers on input to a computer or brain and conversion of numbers to strings on the way out.
Or replace strings with other rendered representations.
Another example: conversion from continuous time &amp; space to discrete time &amp; space (&#8220;sampling&#8221;) on input to a digital machine, and conversion from discrete time &amp; space to continuous time &amp; space (&#8220;reconstruction&#8221;) on the way from digital machine (computer, CD/DVD player) to a brain.</p>

<p>This perspective is what&#8217;s behind my confidence that we will someday really learn to look at whole systems in a consistently functional/denotational style (simple &amp; precise semantics).
The imperative interfaces in today OSs and data-bases are troubling at first, and indeed I often hear people (even on #haskell) using these examples as demonstration that the imperative programming model is inescapable.
These examples don&#8217;t trouble me, however, because I see that we&#8217;ve already dealt with others of their kind.
Underneath the <em>implementation</em> of our current functional abstractions (numbers, strings, trees, functions, etc), there are imperative mechanisms, such as memory allocation &amp; deallocation, stack frame modification, and thunk overwriting (to implement laziness).
Every one of these mechanisms can be used as evidence against the functional paradigm by someone who hasn&#8217;t yet realized how the mechanism can be shifted out of the role of a programming <em>model</em> notion and into the role of <em>implementation</em> of a programming model notion.
The new notion is more composable by virtue of being semantically simpler and mathematically more well-behaved.</p>

<p>Stack and register munging and jump/GOTO are implementations of the semantically simpler notion of function application.
(I mean &#8220;function&#8221; in the sense of math and of pure functional programming.)  Moreover, stack &amp; register munging <em>is</em> the input/output (IO) part of the implementation of function application.
Information goes <strong>out</strong> of one context and <strong>in</strong>to another.
My vision of &#8220;solving the I/O problem&#8221; for functional programming is nothing like Haskell&#8217;s semantically imperative (non-)solution (caled &#8220;IO&#8221; in Haskell), but rather to continue shifting semantically imperative mechanisms out of the programming model and into the implementation of (semantically simple) function application.</p>

<p>How do we get from here (still stuck in the quagmire of imperative programming models) to there (simple semantics and consistently excellent composability)?
First, stop rehearsing the thinking that keeps us stuck.
When we keep telling ourselves and each other that imperative notions are inevitable, accompanying such statements with hand-waving arguments, we are weaving self-fulfilling prophecies.
A person convinced of (and perhaps ego-invested in) the nonexistence of new possibilities is a person actively resisting their opportunity to discover of new possibilities &#8212; too <a href="http://conal.net/blog/posts/fostering-creativity-by-relinquishing-the-obvious/" title="blog post">stuck in the obvious</a> to discover new truths.</p>

<blockquote>
  <p><em>The significant problems of our time cannot be solved by the same level of thinking that created them.</em> &#8211; Albert Einstein</p>
  
  <p><em>They are ill discoverers that think there is no land, when they can see nothing but sea.</em> &#8211; Francis Bacon</p>
</blockquote>

<p>Hand-waving is an important factor in staying stuck in impossibility thinking, since rigorous argument uncovers unconscious limiting assumptions.
I&#8217;m just as happy if you are working on showing <em>impossibility</em> as discovering the possible, <em>as long as you are rigorous</em> and get rigorous peer review.
Otherwise, we all fall into the trap of self-deceit:</p>

<blockquote>
  <p><em>Nothing is easier than self-deceit. For what each man wishes, that he also believes to be true.</em> &#8211; Demosthenes</p>
  
  <p><em>If you wish to see the truth, then hold no opinion for or against</em> &#8211; Osho (Rajneesh Chandra Mohan Jain)</p>
  
  <p><em>The discovery of this reality is hindered rather than helped by belief, whether one believes in God or believes in atheism. We must make here a clear distinction between belief and faith, because, in general practice, belief has come to mean a state of mind which is almost the opposite of faith. Belief, as I use the word here, is the insistence that the truth is what one would &#8220;lief&#8221; or wish it to be. The believer will open his mind to the truth on the condition that it fits in with his preconceived ideas and wishes. Faith, on the other hand, is an unreserved opening of the mind to the truth, whatever it may turn out to be. Faith has no preconceptions; it is a plunge into the unknown. Belief clings, but faith lets go. In this sense of the word, faith is the essential virtue of science, and likewise of any religion that is not self-deception.</em> &#8211; Alan Watts</p>
</blockquote>

<p>In summary, I&#8217;m suggesting we take a fresh look at I/O and functional programming.
Instead of putting I/O into functional programming model (as in continuation- and stream-based I/O), or adopting a hybrid functional/imperative model (as in monadic I/O), let&#8217;s explore how to move I/O entirely out of our programming model into the <em>implementation</em> of a denotationally simple model for whole systems.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Garbage collecting the semantics of FRP</title>
		<link>http://conal.net/blog/posts/garbage-collecting-the-semantics-of-frp/</link>
		<comments>http://conal.net/blog/posts/garbage-collecting-the-semantics-of-frp/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 21:55:30 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[FRP]]></category>
		<category><![CDATA[functional reactive programming]]></category>
		<category><![CDATA[semantics]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=96</guid>
		<description><![CDATA[





Ever since ActiveVRML, the model we&#8217;ve been using in functional reactive programming (FRP) for interactive behaviors is (T-&#62;a) -&#62; (T-&#62;b), for dynamic (time-varying) input of type a and dynamic output of type b (where T is time).
In &#8220;Classic FRP&#8221; formulations (including ActiveVRML, Fran &#38; Reactive), there is a &#8220;behavior&#8221; abstraction whose denotation is a function [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Garbage collecting the semantics of FRP

Tags: FRP, functional reactive programming, semantics, design, derivative

URL: http://conal.net/blog/posts/garbage-collecting-the-semantics-of-frp/

-->

<!-- references -->

<!-- teaser -->

<p>Ever since <a href="http://conal.net/papers/ActiveVRML/" title="Tech report: &quot;A Brief Introduction to ActiveVRML&quot;">ActiveVRML</a>, the model we&#8217;ve been using in functional reactive programming (FRP) for interactive behaviors is <code>(T-&gt;a) -&gt; (T-&gt;b)</code>, for dynamic (time-varying) input of type <code>a</code> and dynamic output of type <code>b</code> (where <code>T</code> is time).
In &#8220;Classic FRP&#8221; formulations (including <a href="http://conal.net/papers/ActiveVRML/" title="Tech report: &quot;A Brief Introduction to ActiveVRML&quot;">ActiveVRML</a>, <a href="http://conal.net/papers/icfp97/" title="paper">Fran</a> &amp; <a href="http://conal.net/papers/push-pull-frp/" title="Paper by Conal Elliott and Paul Hudak">Reactive</a>), there is a &#8220;behavior&#8221; abstraction whose denotation is a function of time.
Interactive behaviors are then modeled as host language (e.g., Haskell) functions between behaviors.
Problems with this formulation are described in <em><a href="http://conal.net/blog/posts/why-classic-FRP-does-not-fit-interactive-behavior/" title="blog post">Why classic FRP does not fit interactive behavior</a></em>.
These same problems motivated &#8220;Arrowized FRP&#8221;.
In Arrowized FRP, behaviors (renamed &#8220;signals&#8221;) are purely conceptual.
They are part of the semantic model but do not have any realization in the programming interface.
Instead, the abstraction is a <em>signal transformer</em>, <code>SF a b</code>, whose semantics is <code>(T-&gt;a) -&gt; (T-&gt;b)</code>.
See <em><a href="http://conal.net/papers/genuinely-functional-guis.pdf" title="Paper by Antony Courtney and Conal Elliott">Genuinely Functional User Interfaces</a></em> and <em><a href="http://www.haskell.org/yale/papers/haskellworkshop02/" title="Paper by Henrik Nilsson, Antony Courtney, and John Peterson">Functional Reactive Programming, Continued</a></em>.</p>

<p>Whether in its classic or arrowized embodiment, I&#8217;ve been growing uncomfortable with this semantic model of functions between time functions.
A few weeks ago, I realized that one source of discomfort is that this model is <em>mostly junk</em>.</p>

<p>This post contains some partially formed thoughts about how to eliminate the junk (&#8220;garbage collect the semantics&#8221;), and what might remain.</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-96"></span></p>

<p>There are two generally desirable properties for a denotational semantics: <em>full abstraction</em> and <em>junk-freeness</em>.
Roughly, &#8220;full abstraction&#8221; means we must not distinguish between what is (operationally) indistinguishable, while &#8220;junk-freeness&#8221; means that every semantic value must be denotable.</p>

<p>FRP&#8217;s semantic model, <code>(T-&gt;a) -&gt; (T-&gt;b)</code>, allows not only arbitrary (computable) transformation of input values, but also of time.
The output at some time can depend on the input at any time at all, or even on the input at arbitrarily many different times.
Consequently, this model allows respoding to <em>future</em> input, violating a principle sometimes called &#8220;causality&#8221;, which is that outputs may depend on the past or present but not the future.</p>

<p>In a causal system, the present can reach backward to the past but not forward the future.
I&#8217;m uneasy about this ability as well.
Arbitrary access to the past may be much more powerful than necessary.
As evidence, consult the system we call (physical) Reality.
As far as I can tell, Reality operates without arbitrary access to the past or to the future, and it does a pretty good job at expressiveness.</p>

<p>Moreover, arbitrary past access is also problematic to implement in its semantically simple generality.</p>

<p>There is a thing we call informally &#8220;memory&#8221;, which at first blush may look like access to the past, it isn&#8217;t really.
Rather, memory is access to a <em>present</em> input, which has come into being through a process of filtering, gradual accumulation, and discarding (forgetting).
I&#8217;m talking about &#8220;memory&#8221; here in the sense of what our brains do, but also what all the rest of physical reality does.
For instance, weather marks on a rock are part of the rock&#8217;s (present) memory of the past weather.</p>

<p>A very simple memory-less semantic model of interactive behavior is just <code>a -&gt; b</code>.
This model is too restrictive, however, as it cannot support <em>any</em> influence of the past on the present.</p>

<p>Which leaves a question: what is a simple and adequate formal model of interactive behavior that reaches neither into the past nor into the future, and yet still allows the past to influence the present?
Inspired in part by a design principle I call &#8220;what would reality do?&#8221; (WWRD), I&#8217;m happy to have some kind of infinitesimal access to the past, but nothing further.</p>

<p>My current intuition is that differentiation/integration plays a crucial role.
That information is carried forward moment by moment in time as &#8220;momentum&#8221; in some sense.</p>

<blockquote>
  <p><em>I call intuition cosmic fishing. You feel a nibble, then you&#8217;ve got to hook the fish.</em> &#8211; Buckminster Fuller</p>
</blockquote>

<p>Where to go with these intuitions?</p>

<p>Perhaps interactive behaviors are some sort of function with all of its derivatives.
See <em><a href="http://conal.net/blog/posts/beautiful-differentiation/" title="blog post">Beautiful differentiation</a></em> for an specification and derived implementation of numeric operations, and more generally of <code>Functor</code> and <code>Applicative</code>, on which much of FRP is based.</p>

<p>I suspect the whole event model can be replaced by integration.
Integration is the main remaining piece.</p>

<p>How weak a semantic model can let us define integration?</p>

<h3>Thanks</h3>

<p>My thanks to Luke Palmer and to Noam Lewis for some clarifying chats about these half-baked ideas.
And to the folks on #haskell IRC for <a href="http://tunes.org/~nef/logs/haskell/10.01.04">brainstorming titles for this post</a>.
My favorite suggestions were</p>

<ul>
<li>luqui: instance HasJunk FRP where</li>
<li>luqui: Functional reactive programming&#8217;s semantic baggage</li>
<li>sinelaw: FRP, please take out the trash!</li>
<li>cale: Garbage collecting the semantics of FRP</li>
<li>BMeph: Take out the FRP-ing Trash</li>
</ul>

<p>all of which I preferred over my original &#8220;FRP is mostly junk&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/garbage-collecting-the-semantics-of-frp/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Communication, curiosity, and personal style</title>
		<link>http://conal.net/blog/posts/communication-curiosity-and-personal-style/</link>
		<comments>http://conal.net/blog/posts/communication-curiosity-and-personal-style/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 07:11:02 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[communication]]></category>
		<category><![CDATA[creativity]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=95</guid>
		<description><![CDATA[



When people talk, especially via the Internet, I expect misunderstandings, since human language is so fraught with ambiguity.  And though I work very hard at clarity, I know my skill is limited and I sometimes fail.

So I see communication as an iterative process: I say something that you don&#8217;t quite get, and you say [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Communication, curiosity, and personal style

Tags: creativity, communication

URL: http://conal.net/blog/posts/communication-curiosity-and-personal-style/

-->

<!-- references -->

<p>When people talk, <em>especially</em> via the Internet, I expect misunderstandings, since human language is so fraught with ambiguity.  And though I work very hard at clarity, I know my skill is limited and I sometimes fail.</p>

<p>So I see communication as an iterative process: I say something that you don&#8217;t quite get, and you say &#8220;Huh?&#8221; about the what or why or how of some particular part, and I refine my message.  Likewise, I listen to you, and check out the places were I&#8217;m puzzled.</p>

<p>I&#8217;m very sensitive where I think I&#8217;m seeing criticisms/dismissals without inquiry/curiosity, and hence some peevishness in some remarks (now edited) in my previous post, <em><a href="http://conal.net/blog/posts/why-program-with-continuous-time/" title="blog post">Why program with continuous time?</a></em>.
See <a href="http://conal.net/blog/posts/fostering-creativity-by-relinquishing-the-obvious/">Fostering creativity by relinquishing the obvious</a> for related remarks.</p>

<p>In some venues, like reddit, aggressive non-inquiry can become the dominant mode of discussion.  Because I prefer high-quality, open-minded inquiry, I mostly choose moderated blogging instead.
If I don&#8217;t pass a comment through, I&#8217;ll usually offer the writer some suggestions, perhaps suggesting a possible misreading of whatever was being responded to, and invite re-submission.
If I&#8217;m particularly annoyed with the writer, I&#8217;ll usually take time to get over my annoyance before responding, so there can be a delay.</p>

<p>In this way, I try to keep a high signal-to-noise ratio, where noise includes assumptions, reactions to misreadings, and often compounded public attempts by people to get each other to listen more carefully.</p>

<p>I&#8217;m starting to discover that people I don&#8217;t get along with sometimes have a very different style from mine.  I like to invite many possibilities into a space and explore them.
My mother shared with me a quote from Henry Nelson Wieman, which she now uses as her email signature:</p>

<blockquote>
  <p>To get the viewpoint of the other person appreciatively and profoundly and reconcile it with his own so far as possible is the supreme achievement of man and his highest vocation.</p>
</blockquote>

<p>While I&#8217;m agnostic about the &#8220;supreme/highest&#8221; part (and open to it), I very much like Wieman&#8217;s description of individual and collective learning as progressing most powerfully by integrating different viewpoints, as founded on working to understanding each other &#8220;appreciatively and profoundly&#8221;.</p>

<p>I&#8217;m learning that some other folks have an oppositional style of learning and discovering.</p>

<p>When <a href="http://noordering.wordpress.com/">Thomas (Bob) Davie</a> and I worked together in Antwerp, he told me that his style is to fiercely resist (battle) any new idea proposed to him.  Whatever breaks through his defenses is worth his learning.  I was flabbergasted and the distance between his style and mine.  And greatly relieved also, because I had to work with him, and I had previously interpreted his behavior as non-curious and even anti-creative.  Although I wasn&#8217;t willing to collaborate in the battle mode at the time, fortunately he was willing to try shifting his style.  And the recognition of our differing style toward similar ends helped greatly in relieving the building tension between us.  Now I enjoy him very much.</p>

<p>Since this surprising discovery, I&#8217;ve wondered how often friction I have with other people coincides with this particular difference in personal styles and whether there are additional style that I hadn&#8217;t been aware of.  So when friction arises, I now try to find out, via a private chat or email.</p>

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

<ul>
<li>2010-01-04: Filled in Bob&#8217;s name (with permission).</li>
</ul>

<!-- without a comment or something here, the last item above becomes a paragraph -->
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/communication-curiosity-and-personal-style/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Is program proving viable and useful?</title>
		<link>http://conal.net/blog/posts/is-program-proving-viable-and-useful/</link>
		<comments>http://conal.net/blog/posts/is-program-proving-viable-and-useful/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 03:21:48 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[creativity]]></category>
		<category><![CDATA[proof]]></category>
		<category><![CDATA[simplicity]]></category>
		<category><![CDATA[specification]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=94</guid>
		<description><![CDATA[







Is program proving a viable and useful endeavor?
Does it create more problems than it solves?

Today I participated in a reddit discussion  thread.
The discussion brought up some issues I care about, and I think others will care also.

Although I&#8217;m quoting from another person whose viewpoint may contrast with mine, I mean no insult or disrespect [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Is program proving viable and useful?

Tags: specification, proof, creativity, simplicity

URL: http://conal.net/blog/posts/is-program-proving-viable-and-useful/

-->

<!-- references -->

<!--
**Edits**:

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

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

<p>Is program proving a viable and useful endeavor?
Does it create more problems than it solves?</p>

<p>Today I participated in <a href="http://www.reddit.com/r/programming/comments/akhvr/reflections_on_a_holy_grail_functional_reactive/c0i2gsd">a reddit discussion  thread</a>.
The discussion brought up some issues I care about, and I think others will care also.</p>

<p>Although I&#8217;m quoting from another person whose viewpoint may contrast with mine, I mean no insult or disrespect to him.
I think his views are fairly common, and I appreciate his putting them into writing and stimulating me to put some of mine into writing as well.</p>

<blockquote>
  <p>[...] you could specify the behavior it should have precisely.
  However in this case (and in most practical cases that aren&#8217;t tricky algorithms actually) the mathematical spec is not much shorter than the implementation.
  So meeting the spec does not help, the spec is just as likely to be wrong/incomplete as the implementation.
  With wrong/incomplete I mean that even if you meet the spec you don&#8217;t attain your goal.</p>
</blockquote>

<p>I like the observation that precise specifications can be as complex and difficult to get correct as precise programs.
And indeed specification complexity is problematic.
Rather than give up on precision, my own interest is in finding simpler and clearer ways to think about things so that specifications can be quite simple.
Thus my focus on simple and precise semantic models.
One example is as given in <em><a href="http://conal.net/papers/push-pull-frp/" title="Paper">Push-pull functional reactive programming</a></em>.
I&#8217;m proud of the simplicity of this specification and of how clearly it shows what parts of the model could use improving.
(The <code>Event</code> type doesn&#8217;t follow the <a href="">type class morphism</a> principle, and indeed has difficulty in practice.)
And since such semantics-based software design methods are still in their infancy, I agree that it&#8217;s usually more practical to abandon preciseness in software development.</p>

<p>For more examples of formally simple specifications, see the papers <em><a href="http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/" title="blog post">Denotational design with type class morphisms</a></em> and <em><a href="http://conal.net/blog/posts/beautiful-differentiation/" title="blog post">Beautiful differentiation</a></em>.</p>

<p>I suspect the crux of the issue here is that <em>combining precision and simplicity is very difficult</em> &#8212; whether for specifications or programs (or for many other other human endeavors).</p>

<blockquote>
  <p><em>Any intelligent fool can make things bigger and more complex &#8230; it takes a touch of genius &#8212; and a lot of courage &#8212; to   move in the opposite direction.</em> &#8211; Albert Einstein</p>
</blockquote>

<p>We had been writing informal/imprecise &#8220;programs&#8221; for a long time before computers were invented, as various kinds of instructions/recipies.
With mechanization of program handling in the 1940s came the requirement of precision.
Since simple precision is difficult (according to my hypothesis), and precision is required, simplicity usually loses out.
So our history of executably-precise programs is populated mostly by complexity.</p>

<blockquote>
  <p><em>I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are   obviously no deficiencies, and the other way is make it so complicated that there are no obvious deficiencies.
  The first   method is far more difficult.</em>  &#8211; C.A.R. Hoare, <a href="http://scifac.ru.ac.za/cspt/hoare.htm">The Emperor&#8217;s Old Clothes, Turing Award lecture,   1980</a></p>
</blockquote>

<p>As much as one assumes that the future is bound to perpetuate the past, one might believe that we will <em>never</em> escape complexity.
In my own creative process, I like to distinguish between the notion of <em>necessarily true</em> and merely <em>historically true</em>.
I understand progress itself to be about this distinction &#8212; the overturning of the historically true that turns out (often surprisingly) not to be necessarily true.</p>

<p>What about <em>specification</em>?
Are precise specifications <em>necessarily</em> complex, or merely <em>historically</em> complex?
(I.e., <em>must</em> we sit by while the future repeats the complexity of the past?)  This question I see as even more unexplored than the same question for programs, because we have not been required to work with formal specifications nearly as much as with formal programs.
Even so, some people have <em>chosen</em> to work on precise specification and so some progress has been made in improving their simplicity, and I&#8217;m optimistic about the future.
Optimistic enough to devote some of my life energy to the search.</p>

<blockquote>
  <p><em>Simplicity is the most difficult thing to secure in this world; it is the last limit of experience and the last effort of genius.</em> &#8211; George Sand</p>
  
  <p><em>Everything is vague to a degree you do not realize till you have tried to make it precise.</em> &#8211; Bertrand Russell</p>
</blockquote>

<p>I expect that future software developers will look back on our era as barbaric, the way we look at some of the medical practices of the past.
Of course, we&#8217;re doing the best we know how, just as past medical practitioners were.</p>

<p>I know people are apt to hear criticism/insult whether present or not, and I hope that these words of mine are not heard as such.
I appreciate both the current practitioners, doing their best with tools at hand, and the pioneer, groping toward the methods of the future.
Personally, I play both roles, and I&#8217;m guessing you do as well, perhaps differing only in balance.
I urge us all to appreciate and respect each other more as we muddle on.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/is-program-proving-viable-and-useful/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Why program with continuous time?</title>
		<link>http://conal.net/blog/posts/why-program-with-continuous-time/</link>
		<comments>http://conal.net/blog/posts/why-program-with-continuous-time/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 02:39:49 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[continuous]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=93</guid>
		<description><![CDATA[



For me functional reactive programming (FRP) has been mainly about two principles.

One is denotational design (i.e., based on simple, precise denotational semantics), which has been integral to FRP since the first incarnation as ActiveVRML.
I&#8217;ve written several things recently about denotational design.

My second core principle is continuous time, which has been present since Fran &#38; ActiveVRML&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Why program with continuous time?

Tags: design, continuous

URL: http://conal.net/blog/posts/why-program-with-continuous-time/

-->

<!-- references -->

<p>For me functional reactive programming (FRP) has been mainly about two principles.</p>

<p>One is <em>denotational design</em> (i.e., based on simple, precise denotational semantics), which has been integral to FRP since the first incarnation as <a href="http://conal.net/papers/ActiveVRML/" title="Paper">ActiveVRML</a>.
I&#8217;ve written <a href="http://conal.net/papers/push-pull-frp/" title="Paper">several</a> <a href="http://conal.net/blog/posts/beautiful-differentiation/" title="blog post">things</a> <a href="http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/" title="blog post">recently</a> about denotational design.</p>

<p>My second core principle is <em>continuous time</em>, which has been present since <a href="http://conal.net/papers/icfp97/" title="Paper">Fran</a> &amp; ActiveVRML&#8217;s predecessor <a href="http://conal.net/papers/siggraph94.pdf" title="Paper">TBAG</a>.</p>

<p>Today I read a confusion that I&#8217;ve heard many times before about continuous time, which I&#8217;d like to bring up, in part because I love continuous time, and in part because there&#8217;s a much broader underlying principle of software design.</p>

<blockquote>
  <p>[...] I don&#8217;t see why the lack of continuous streams leaves a &#8220;gap&#8221;.  In the end all streams are discrete.</p>
</blockquote>

<p>&#8220;In the end&#8221;, yes.
Just as in the end, numbers are displayed as ascii numerals.
However, programming is more about the middle than the end, i.e., more about composition than about output.
For that reason, we don&#8217;t generally use strings in place of numbers.
If we did, composition operations (arithmetic) would be very awkward.
Similarly, continuity in space and in time is better for composition/modularity, leaving discreteness to the output step.</p>

<p>Another name for &#8220;continuous&#8221; is &#8220;resolution-independent&#8221;, and thus able to be transformed in time and space with ease and without propagating and amplifying sampling artifacts.</p>

<p>As another example, consider the data types in a 3D graphics API.
In the end, all graphics is pixels, isn&#8217;t it?
So what gap is left in a pixel-oriented API that doesn&#8217;t address higher-level continuous notions like triangles or curved surfaces?
(Hint: it&#8217;s not just speed.)</p>

<p>One could go further than strings and pixels, and say that &#8220;in the end&#8221; my data types will end up as phosphors or electrical impulses, so programming at those levels is perfectly adequate.
Again, composability would suffer.</p>

<p>Another example is functional vs imperative programming.
It&#8217;s all side-effects <em>in the end</em>.
Functional programming excels in composability, as explained and illustrated by John Hughes in <em><a href="http://www.cs.chalmers.se/~rjmh/Papers/whyfp.html" title="Paper by John Hughes">Why Functional Programming Matters</a></em>.
And I&#8217;m not just talking about <em>pure</em> functional programming, but the availability of compound <em>expressions</em>, as introduced in Fortran (controversially), despite that machines just execute sequences of atomic, side-effecting statements <em>in the end</em>.</p>

<p>Another example, and really the heart of John&#8217;s paper, is finite vs infinite data structures.
We only access a finite amount of data <em>in the end</em>.
However, allowing infinite data structures <em>in the middle</em> makes for a much more composable programming style.</p>

<p>Some unsolicited advice to us all: Next time you see someone doing something and you don&#8217;t understand their underlying motivation, ask them.
Many issues are not immediately obvious, so don&#8217;t be shy!
Reading papers can help as well.</p>

<p>For more on continuous time:</p>

<ul>
<li><em><a href="http://conal.net/papers/siggraph94.pdf" title="Paper">TBAG: A High Level Framework for Interactive, Animated 3D Graphics Applications</a></em></li>
<li><em><a href="http://conal.net/papers/icfp97/" title="Paper">Functional Reactive Animation</a></em></li>
<li><em><a href="http://conal.net/papers/tse-modeled-animation/" title="Paper">An Embedded Modeling Language Approach to Interactive 3D and Multimedia Animation</a></em></li>
<li><em><a href="http://conal.net/papers/plilpalp98/" title="Paper">Functional Implementations of Continuous Modeled Animation</a></em></li>
</ul>

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

<ul>
<li>2010-01-03: Trimmed &amp; tweaked the unsolicited advice.  Hopefully less peevish/patronizing now.  Thanks for the feedback.</li>
<li>2010-01-07: Trimmed quote.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/why-program-with-continuous-time/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Exact numeric integration</title>
		<link>http://conal.net/blog/posts/exact-numeric-integration/</link>
		<comments>http://conal.net/blog/posts/exact-numeric-integration/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 18:22:43 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[partial value]]></category>
		<category><![CDATA[unamb]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=92</guid>
		<description><![CDATA[





This post describes a simple way to integrate a function over an interval and get an exact answer.
The question came out of another one, which is how to optimally render a continuous-space image onto a discrete array of pixels.

For anti-aliasing, I&#8217;ll make two simplying assumptions (to be revisited):


Each pixel is a square area.  (With [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Exact numeric integration

Tags: integration, unamb

URL: http://conal.net/blog/posts/exact-numeric-integration/

-->

<!-- references -->

<!-- teaser -->

<p>This post describes a simple way to integrate a function over an interval and get an exact answer.
The question came out of another one, which is how to optimally render a continuous-space image onto a discrete array of pixels.</p>

<p>For anti-aliasing, I&#8217;ll make two simplying assumptions (to be revisited):</p>

<ul>
<li>Each pixel is a square area.  (With apologies to <a href="http://www.alvyray.com/memos/" title="Paper: &quot;A Pixel is Not a Little Square, a Pixel is Not a Little Square, a Pixel is Not a Little Square! (And a Voxel is Not a Little Cube)&quot; by Alvy Ray Smith">Alvy Ray Smith</a>.)</li>
<li>Since I can choose only one color per pixel, I want exactly the <em>average</em> of the continuous image over pixel&#8217;s subregion of 2D space.</li>
</ul>

<p>The average of a function over a region (here a continuous image over a 2D interval) is equal to the integral of the function across the region divided by the size (area for 2D) of the region.
Since our regions are simple squares, the average and the integral can each be defined easily in terms of the other (dividing or multiplying by the area).</p>

<p>To simplify the problem further, I&#8217;ll consider one-dimensional integral, i.e., integrating a function of <strong>R</strong> over a 1D interval.
My solution below involves the least upper bound operator <a href="http://conal.net/blog/tag/unamb/" title="posts on unamb and lub">I&#8217;ve written about</a> (and its specialization <code>unamb</code>).</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-92"></span></p>

<h3>A first simple algorithm</h3>

<p>Integration takes a real-valued function and an interval (low &amp; high) and gives a real.</p>

<p><div>
<pre class="haskell">integral :: <span style="color: green;">&#40;</span>R-&gt;R<span style="color: green;">&#41;</span> -&gt; R -&gt; R -&gt; R</pre>
</div></p>

<p>Integration has a property of interval additivity, i.e., the integral of a function from <em>a</em> to <em>c</em> is the sum of the integral from <em>a</em> to <em>b</em> and the integral from <em>b</em> to <em>c</em>.</p>

<p><div>
<pre class="haskell">∀ a b c. integral f a c == integral f a b + integral f b c</pre>
</div></p>

<p>which immediately leads to a simple recursive algorithm:</p>

<p><div>
<pre class="haskell">integral f low high = integral f low mid + integral f mid high
  <span style="color: #050; font-weight: bold;">where</span> mid = <span style="color: green;">&#40;</span>low + high<span style="color: green;">&#41;</span> / <span style="color: red;">2</span></pre>
</div></p>

<p>Extending to 2D is simple: we could divide a rectangular region into four quarter subregions, or into two subregions by splitting the longer dimension.
The quartering variation is very like <a href="http://en.wikipedia.org/wiki/Mipmap">mipmapping</a>, as used to anti-alias textures.
Mipmapping takes a pixel array and builds a pyramid of successively lower resolution versions.
Each level except the first is constructed out of the previous (next higher-resolution) level by averaging blocks of four pixels into one.
The simple <code>integral</code> algorithm above (extended to 2D) is like mipmapping when we start with an <em>infinite resolution</em> (i.e., continuous) texture.</p>

<h3>Umm &#8230;</h3>

<p>Maybe you&#8217;re thinking what I&#8217;m thinking: Hey!  We don&#8217;t have a base case, so we won&#8217;t even get off the ground.</p>

<p>Given that our domain is continuous, I don&#8217;t know what to use for a base case.
So let&#8217;s consider what the purpose of a base case is, and see whether that purpose can be accomplished some other way.</p>

<h3>What&#8217;s so important about base cases?</h3>

<p>Does every recursion need a base case in order to avoid being completely undefined?</p>

<p>Here&#8217;s a counter-example: mapping a function over an infinite stream, taken from the source code of the <a href="http://hackage.haskell.org/package/Stream" title="Haskell package: Stream">Stream package</a>.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Stream a = Cons a <span style="color: green;">&#40;</span>Stream a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #833; font-weight: bold;">Functor</span></a> Stream <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f ~<span style="color: green;">&#40;</span>Cons x xs<span style="color: green;">&#41;</span> = Cons <span style="color: green;">&#40;</span>f x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f xs<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The key thing here is that <code>Cons</code> is <em>non-strict</em> in its second argument, which holds the recursive call.
(Definition: a function <code>f</code> is &#8220;strict&#8221; if <code>f ⊥ == ⊥</code>.)</p>

<p>Non-strictness of <code>if-then-else</code> is exactly what allows more mundane recursions to produce defined (non-⊥) results as well, e.g.,</p>

<p><div>
<pre class="haskell">fac n = <span style="color: #050; font-weight: bold;">if</span> n &lt;= <span style="color: red;">0</span> <span style="color: #050; font-weight: bold;">then</span> <span style="color: red;">1</span> <span style="color: #050; font-weight: bold;">else</span> fac <span style="color: green;">&#40;</span>n<span style="color: red;">-1</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>In this <code>fac</code> example, <code>if-then-else</code> must be (and <em>is</em>) non-strict in its third argument (the recursive call).</p>

<p>So &#8220;base case&#8221; is not the heart of the matter; non-strictness is.</p>

<h3>Finding non-strictness</h3>

<p>The trouble with our elegant recursive derivation of <code>integral</code> above is that addition is strict in its arguments (where the recursive <code>integral</code> calls appear).
This strictness means that we cannot get <em>any information at all</em> out of the right-hand side until we get some information out of the recursive calls, which won&#8217;t happen until we get information out of <em>their</em> recursive calls, ad infinitum.</p>

<p>To escape this information black hole, can we find some scrap of information about the value of the integral that doesn&#8217;t (always) rely on the recursive calls?</p>

<p><a href="http://en.wikipedia.org/wiki/Interval_arithmetic" title="Wikipedia article">Interval analysis</a> (IA) provides an answer.
The idea of IA is to extend functions to apply to <em>intervals</em> of values and produce intervals of results.
The interval versions are sloppy in that a result interval may hold values not corresponding to anything in the source interval.
(In math-speak, a result interval may be a <em>proper</em> superset of the image of the source interval under the function.)</p>

<p>Applying an interval version of the function to the source interval results in lower and upper bounds for <code>f</code>.
The average must be between these bounds, so the integral is bounded by these bounds multiplied by the interval size.
(Or use more sophisticated variations on IA, such as affine analysis, etc.)</p>

<p>Now we have <em>some</em> information.
How can we mix it in with the sum of recursive calls to <code>integral</code>?
We can use <code>(⊔)</code> (least upper bound or &#8220;lub&#8221;), which is perfect for the job because its meaning is exactly to combine two pieces of information.
See <em><a href="http://conal.net/blog/posts/merging-partial-values/" title="blog post">Merging partial values</a></em> and <em><a href="http://conal.net/blog/posts/lazier-function-definitions-by-merging-partial-values/" title="blog post">Lazier function definitions by merging partial values</a></em>.</p>

<p>Instead of treating IA as operating on <em>intervals</em>, think of it as operating on &#8220;partial numbers&#8221;, i.e., inexact values.
Suppose we define a type of numbers that consistently generalizes exact numbers but is additionally populated with inexact values.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Partial R  <span style="color: #5d478b; font-style: italic;">-- abstract</span>
&nbsp;
between :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> a =&gt; a -&gt; a -&gt; Partial a
exact   :: a -&gt; Partial a</pre>
</div></p>

<p>Perhaps <code>exact a = between a a</code>.</p>

<p>Now we can escape the black hole:</p>

<p><div>
<pre class="haskell">integral f low high = <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>high - low<span style="color: green;">&#41;</span> * f <span style="color: green;">&#40;</span>between low high<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> ⊔
                      <span style="color: green;">&#40;</span>integral f low mid + integral f mid high<span style="color: green;">&#41;</span>
  <span style="color: #050; font-weight: bold;">where</span> mid = <span style="color: green;">&#40;</span>low + high<span style="color: green;">&#41;</span> / <span style="color: red;">2</span></pre>
</div></p>

<h3>Representation</h3>

<p>One representation of a partial value is an interval.
In this representation, <code>(⊔)</code> is interval intersection.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Partial a = <span style="color: green;">&#40;</span>a,a<span style="color: green;">&#41;</span>  <span style="color: #5d478b; font-style: italic;">-- first try</span>
&nbsp;
<span style="color: green;">&#40;</span>l,h<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>l',h'<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>l `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:max"><span style="font-weight: bold;">max</span></a>` l', h `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:min"><span style="font-weight: bold;">min</span></a>` h'<span style="color: green;">&#41;</span></pre>
</div></p>

<p>If the lower and bounds are plain old exact numbers, then this choice of representation and <code>(⊔)</code> has a fatal flaw.
The <code>max</code> and <code>min</code> functions are strict, so <code>(⊔)</code> can easily produce <em>less</em> information than it is given, while its job is to combine all information present in both arguments.
(For instance, let <code>l == 3</code> and <code>l' == ⊥</code>.  Then <code>l `max` l' == ⊥</code>, so we&#8217;ve lost the information from <code>l</code>.)</p>

<p>One possible solution is to change the kind of numbers used in the bounds in such a way that <code>max</code> and <code>min</code> are not strict.
Let&#8217;s use two new types, for lower and upper bounds:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Lower a  <span style="color: #5d478b; font-style: italic;">-- abstract</span>
<span style="color: #050; font-weight: bold;">type</span> Upper a  <span style="color: #5d478b; font-style: italic;">-- abstract</span></pre>
</div></p>

<p>These two types also capture partial information about numbers, though they cannot express exact numbers (other than <code>maxBound</code> and <code>minBound</code>, where available).</p>

<p>Now we can represent partial numbers.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Partial a = <span style="color: green;">&#40;</span>Lower a, Upper a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: green;">&#40;</span>l,h<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>l',h'<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>l ⊔ l', h ⊔ h'<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Notice that I&#8217;ve replaced both <code>max</code> and <code>min</code> each by <code>(⊔)</code>.
Now I realize that I only used <code>max</code> and <code>min</code> above as a way of combining the information that the lower and upper bounds (respectively) gave us.
The <code>(⊔)</code> operator states this intention directly.</p>

<p>Pleasantly, we don&#8217;t even have to state this definition, as <code>(⊔)</code> is already defined this way for pairs.
(Well, not quite; see <em><a href="http://conal.net/blog/posts/merging-partial-values/" title="blog post">Merging partial values</a></em>.)</p>

<h3>Improving values and intervals</h3>

<p>This idea for representing partial values is very like what Warren Burton and Ken Jackson called &#8220;improving intervals&#8221;, which is a two-sided version of Warren&#8217;s &#8220;improving values&#8221; (corresponding to <code>Lower</code> above).
(See <em>Encapsulating nondeterminacy in an abstract data type with deterministic semantics</em> (JFP, 1991) and <em>Improving Intervals</em> (JFP, 1993).  As these papers are hard to find, you might start with <a href="http://ir.lib.sfu.ca/bitstream/1892/7097/1/b15233182.pdf" title="Ken Jackson's dissertation: &quot;Functional programming applied to parallel combinatorial search">Ken Jackson&#8217;s dissertation</a>.)</p>

<p>Warren and Ken represented improving values and intervals as lazy, possibly-infinite lists of improving approximations.
While an improving value is represented as a sequence (finite or infinite) of monotonically increasing values, an improving interval is represented as a sequence of monotonically shrinking intervals (each containing the next).
The <em>denotation</em> of one of these improving representations is the limit of the sequence.
Any query for information not specifically present in a partial value would yield ⊥, just as applying a partial function (or data structure) outside its domain yields ⊥.
For instance, one could ask a partial number for successive bits.
If a requested bit cannot be determined from the available bounds, then that bit and all later bits are ⊥.</p>

<h3>Dropping <code>lub</code></h3>

<p>There&#8217;s a subtlety in the second definition of <code>integral</code> above.
My goal is that <code>integral</code> yield a completely defined (exact) number.
We can meet this goal for fairly well-behaved functions, since IA gives decreasing errors as input intervals shrink, and the result intervals are multiplied by shrinking interval sizes.
Even discontinuities in the integrated function will be smoothed out, if there are only finitely many, thanks to the multiplication.</p>

<p>Completely defined values are at the tops of the information ordering.
(I&#8217;m assuming we don&#8217;t have the &#8220;over-defined&#8221; (self-contradictory) value ⊤.)
The sum of recursive calls is also fully defined, and starter partial value, <code>f (between low high)</code>, has strictly less information, i.e.,</p>

<p><div>
<pre class="haskell">   <span style="color: green;">&#40;</span>high - low<span style="color: green;">&#41;</span> * f <span style="color: green;">&#40;</span>between low high<span style="color: green;">&#41;</span>
⊑  integral f low high
=  integral f low mid + integral f mid high</pre>
</div></p>

<p>So we can get by with a less general form of <code>(⊔)</code>.
If we represent our numbers as lazy lists of improving intervals, then we can simply use <code>(:)</code> in place of <code>(⊔)</code>.</p>

<p>Hm.  My reasoning just above is muddled.
I think the crucial property is that</p>

<p><div>
<pre class="haskell">     <span style="color: green;">&#40;</span>high - low<span style="color: green;">&#41;</span> * f <span style="color: green;">&#40;</span>between low high<span style="color: green;">&#41;</span>
⊑    <span style="color: green;">&#40;</span>mid  - low<span style="color: green;">&#41;</span> * f <span style="color: green;">&#40;</span>between low mid <span style="color: green;">&#41;</span>
   + <span style="color: green;">&#40;</span>high - mid<span style="color: green;">&#41;</span> * f <span style="color: green;">&#40;</span>between mid high<span style="color: green;">&#41;</span></pre>
</div></p>

<p>To see why this property holds, first subdivide:</p>

<p><div>
<pre class="haskell">     <span style="color: green;">&#40;</span>high - low<span style="color: green;">&#41;</span> * f <span style="color: green;">&#40;</span>between low high<span style="color: green;">&#41;</span>
==   <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>mid - low<span style="color: green;">&#41;</span> + <span style="color: green;">&#40;</span>high - mid<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> * f <span style="color: green;">&#40;</span>between low high<span style="color: green;">&#41;</span>
==   <span style="color: green;">&#40;</span>mid  - low<span style="color: green;">&#41;</span> * f <span style="color: green;">&#40;</span>between low high<span style="color: green;">&#41;</span>
   + <span style="color: green;">&#40;</span>high - mid<span style="color: green;">&#41;</span> * f <span style="color: green;">&#40;</span>between low high<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Next apply information monotonicity, i.e., more information (smaller interval) in yields more information out:</p>

<p><div>
<pre class="haskell">f <span style="color: green;">&#40;</span>between low high<span style="color: green;">&#41;</span> ⊑ f <span style="color: green;">&#40;</span>between low med<span style="color: green;">&#41;</span>
&nbsp;
f <span style="color: green;">&#40;</span>between low high<span style="color: green;">&#41;</span> ⊑ f <span style="color: green;">&#40;</span>between med high<span style="color: green;">&#41;</span></pre>
</div></p>

<p>from which the crucial property follows.</p>

<p><em>Note</em>: the notation is potentially confusing, since smaller intervals means more information, and so <code>(⊑)</code> means <code>(⊇)</code>, and <code>(⊔)</code> means <code>(∩)</code>.</p>

<h3>Efficiency and benign side-effects</h3>

<p>There is a serious efficiency problem with this (lazy) list-based representation of improving values or intervals, as pointed out by Ken and Warren:</p>

<blockquote>
  <p>At any point in time, it is really only the tightest bounds currently in the list that are important.  [...]  Hence, the list representation consumes more space than is necessary, and time is wasted examining the out-of-date values in the list.  A better representation, in a language that allows update-in-place, would be a pair consisting of the tightest lower bound and the tightest upper bound found so far.  This pair would be updated-in-place when better bounds become known.  (<em>Improving Intervals</em>, section 5.2)</p>
</blockquote>

<p>The update-in-place suggested here is semantically benign, i.e., does not compromise pure functional semantics, because it doesn&#8217;t change the information content.
Instead, it makes that content more efficient to access a second time.
The same sort of semantically benign optimization underlies lazy evaluation, with the run-time system destructively replacing a thunk upon its first evaluation.</p>

<p>This benign-update idea is also explored in <em><a href="http://conal.net/blog/posts/another-angle-on-functional-future-values/" title="blog post">Another angle on functional future values</a></em>.</p>

<h3>Averaging vs integration</h3>

<p>In practice, I&#8217;d probably rather use a recursive interval average function instead of a recursive integral.
Recall that with the recursive <code>integral</code>, we had to multiply by the interval size at each step.
The intervals get very small, and I worry about having to combine numbers of greatly differing magnitudes.
With a recursive average, the numbers get averaged at each step, which I expect means we&#8217;ll be combining numbers of similar magnitudes.</p>

<p><div>
<pre class="haskell">average f low high = f <span style="color: green;">&#40;</span>between low high<span style="color: green;">&#41;</span> ⊔
                     average f low mid `avg` average f mid high
  <span style="color: #050; font-weight: bold;">where</span> mid = low `avg` high
&nbsp;
a `avg` b = <span style="color: green;">&#40;</span>a + b<span style="color: green;">&#41;</span> / <span style="color: red;">2</span>
&nbsp;
integral f low high = <span style="color: green;">&#40;</span>high - low<span style="color: green;">&#41;</span> * average f low high   <span style="color: #5d478b; font-style: italic;">-- non-recursive</span></pre>
</div></p>

<p>Here&#8217;s a modified form that can apply to higher dimensions as well:</p>

<p><div>
<pre class="haskell">average f iv = f iv ⊔ mean <span style="color: green;">&#91;</span>average f iv' | iv' &lt;- subdivide iv<span style="color: green;">&#93;</span>
&nbsp;
mean l = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sum"><span style="font-weight: bold;">sum</span></a> l / <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span style="font-weight: bold;">length</span></a> l
&nbsp;
integral f iv = size iv * average f iv   <span style="color: #5d478b; font-style: italic;">-- non-recursive</span></pre>
</div></p>

<h3>Exact computation</h3>

<p>The algorithms described above can easily run afoul of inexact numeric representations, such as <code>Float</code> or <code>Double</code>.
With such representation types, as recursive subdivision progresses, at some point, an interval will be bounded by consecutive representable numbers.
At that point, sub-dividing an interval will result in an empty interval plus the pre-divided interval, and we will stop making progress.</p>

<p>One solution to this problem is use <em>exact</em> number representations.
Another is to use progressively precise inexact representations.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/exact-numeric-integration/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Thoughts on semantics for 3D graphics</title>
		<link>http://conal.net/blog/posts/thoughts-on-semantics-for-3d-graphics/</link>
		<comments>http://conal.net/blog/posts/thoughts-on-semantics-for-3d-graphics/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 07:41:30 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[arrow]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[semantics]]></category>
		<category><![CDATA[type class morphism]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=90</guid>
		<description><![CDATA[





The central question for me in designing software is always


  What does it mean?


With functional programming, this question is especially crisp.
For each data type I define, I want to have a precise and simple mathematical model.
(For instance, my model for behavior is function-of-time, and my model of images is function-of-2D-space.)
Every operation on the type [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Thoughts on semantics for 3D graphics

Tags: semantics, design, 3D, arrow, type class morphism, geometry

URL: http://conal.net/blog/posts/thoughts-on-semantics-for-3d-graphics/

-->

<!-- references -->

<!-- teaser -->

<p>The central question for me in designing software is always</p>

<blockquote>
  <p>What does it mean?</p>
</blockquote>

<p>With functional programming, this question is especially crisp.
For each data type I define, I want to have a precise and simple mathematical model.
(For instance, my model for behavior is function-of-time, and my model of images is function-of-2D-space.)
Every operation on the type is also given a meaning in terms of that semantic model.</p>

<p>This specification process, which is denotational semantics applied to data types, provides a basis for</p>

<ul>
<li>correctness of the implementation,</li>
<li>user documentation free of implementation detail,</li>
<li>generating and proving properties, which can then be used in automated testing, and</li>
<li>evaluating and comparing the elegance and expressive power of design decisions.</li>
</ul>

<p>For an example (2D images), some motivation of this process, and discussion, see Luke Palmer&#8217;s post <em><a href="http://lukepalmer.wordpress.com/2008/07/18/semantic-design/" title="Blog post by Luke Palmer">Semantic Design</a></em>.
See also my posts on the idea and use of <em><a href="http://conal.net/blog/tag/type-class-morphism/" title="Posts on type class morphisms">type class morphisms</a></em>, which provide additional structure to denotational design.</p>

<p>In spring of 2008, I started working on a functional 3D library, <a href="http://haskell.org/haskellwiki/FieldTrip" title="Library wiki page">FieldTrip</a>.
I&#8217;ve designed functional 3D libraries before as part of <a href="http://conal.net/tbag/" title="Project web page">TBAG</a>, <a href="http://conal.net/papers/ActiveVRML/" title="Tech report: &quot;A Brief Introduction to ActiveVRML&quot;">ActiveVRML</a>, and <a href="http://conal.net/Fran" title="Functional reactive animation">Fran</a>.
This time I wanted a semantics-based design, for all of the reasons given above.
As always, I want a model that is</p>

<ul>
<li>simple, </li>
<li>elegant, and </li>
<li>general.</li>
</ul>

<p>For 3D, I also want the model to be GPU-friendly, i.e., to execute well on (modern) GPUs and to give access to their abilities.</p>

<p>I hadn&#8217;t thought of or heard a model that I was happy with, and so I didn&#8217;t have the sort of firm ground I like to stand on in working on FieldTrip.
Last February, such a model occurred to me.
I&#8217;ve had this blog post mostly written since then.
Recently, I&#8217;ve been focused on functional 3D again for GPU-based rendering, and then Sean McDirmid <a href="http://mcdirmid.wordpress.com/2009/11/20/designing-a-gpu-oriented-geometry-abstraction-part-one/">posed a similar question</a>, which got me thinking again.</p>

<!--
**Edits**:

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

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

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

<h3>Geometry</h3>

<p>3D graphics involves a variety of concepts.
Let&#8217;s start with 3D geometry, using a <em>surface</em> (rather than a <em>solid</em>) model.</p>

<p>Examples of 3D (surface) geometry include</p>

<ul>
<li>the boundary (surface) of a solid box, sphere, or torus,</li>
<li>a filled triangle, rectangle, or circle,</li>
<li>a collection of geometry , and</li>
<li>a spatial transformation of geometry.</li>
</ul>

<h4>First model: set of geometric primitives</h4>

<p>One model of geometry is a set of geometric primitives.
In this model, <code>union</code> means set union, and spatial transformation means transforming all of the 3D points in all of the primitives in the set.
Primitives contain infinitely (even uncountably) many points, so that&#8217;s a lot of transforming.
Fortunately, we&#8217;re talking about what (semantics), and not how (implementation).</p>

<p><em>What is a geometric primitive?</em></p>

<p>We could say it&#8217;s a triangle, specified by three coordinates.
After all, computer graphics reduces everything to sets of triangles.
Oops &#8212; we&#8217;re confusing semantics and implementation.
Tessellation <em>approximates</em> curved surfaces by sets of triangles but loses information in the process.
I want a story that includes this approximation process but keeps it clearly distinct from semantically ideal curved surfaces.
Then users can work with the ideal, simple semantics and rely on the implementation to perform intelligent, dynamic, view-dependent tessellation that adapts to available hardware resources.</p>

<p>Another model of geometric primitive is a function from 2D space to 3D space, i.e., the &#8220;parametric&#8221; representation of surfaces.
Along with the function, we&#8217;ll probably want some means of describing the subset of 2D over which the surface is defined, so as to trim our surfaces.
A simple formalization would be</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Surf = R2 -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> R3</pre>
</div></p>

<p>where</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> R  <span style="color: #5d478b; font-style: italic;">-- real numbers</span>
<span style="color: #050; font-weight: bold;">type</span> R2 = <span style="color: green;">&#40;</span>R,R<span style="color: green;">&#41;</span>
<span style="color: #050; font-weight: bold;">type</span> R3 = <span style="color: green;">&#40;</span>R,R,R<span style="color: green;">&#41;</span></pre>
</div></p>

<p>For shading, we&#8217;ll also need normals, and possibly tangents &amp; bitangents,
We can get these features and more by including derivatives, either just first derivatives or all of them.
See my <a href="http://conal.net/blog/tag/derivative/" title="Posts on derivatives">posts on derivatives</a> and paper <em><a href="http://conal.net/papers/beautiful-differentiation/" title="Paper: Beautiful differentiation">Beautiful differentiation</a></em>.</p>

<p>In addition to position and derivatives, each point on a primitive also has material properties, which determines how light is reflected by and transmitted through the surface at the point.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Surf = R2 -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span>R2 :&gt; R3, Material<span style="color: green;">&#41;</span></pre>
</div></p>

<p>where <code>a :&gt; b</code> contains all derivatives (including zeroth) at a point of a function of type <code>a-&gt;b</code>.
See <em><a href="http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally/" title="blog post">Higher-dimensional, higher-order derivatives, functionally</a></em>.
We could perhaps also include derivatives of material properties:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Surf = R2 :~&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span>R3, Material<span style="color: green;">&#41;</span></pre>
</div></p>

<p>where <code>a :~&gt; b</code> is the type of infinitely differentiable functions.</p>

<h4>Combining geometry values</h4>

<p>The <code>union</code> function gives one way to combine two geometry values.
Another is morphing (interpolation) of positions and of material properties.
What can the semantics of morphing be?</p>

<p>Morphing betwen two <em>surfaces</em> is easier to define.
A surface is a function, so we can interpolate <em>point-wise</em>: given surfaces <code>r</code> and <code>s</code>, for each point <code>p</code> in parameter space, interpolate between (a) <code>r</code> at <code>p</code> and (b) <code>s</code> at <code>p</code>, which is what <code>liftA2</code> (on functions) would suggest.</p>

<p>This definition works <em>if</em> we have a way to interpolate between <code>Maybe</code> values.
If we use <code>liftA2</code> again, now on <code>Maybe</code> values, then the <code>Just</code>/<code>Nothing</code> (and <code>Nothing</code>/<code>Just</code>) cases will yield <code>Nothing</code>.
Is this semantics desirable?
As an example, consider a flat square surface with hole in the middle.
One square has a small hole, and the other has a big hole.
If the size of the hole corresponds to size of the portion of parameter space mapped to <code>Nothing</code>, then point-wise interpolation will always yield the larger hole, rather than interpolating between hole sizes.
On the other hand, the two surfaces with holes might be <code>Just</code> over exactly the same set of parameters, with the function determining how much the <code>Just</code> space gets stretched.</p>

<p>One way to characterize this awkwardness of morphing is that the two functions (surfaces) might have <em>different domains</em>.
This interpretation comes from seeing <code>a -&gt; Maybe b</code> as encoding a function from a <em>subset</em> of <code>a</code> (i.e., a <em>partial</em> function on <code>a</code>).</p>

<p>Even if we had a satisfactory way to combine surfaces (point-wise), how could we extend it to combining full geometry values, which can contain any number of surfaces?
One idea is to model geometry as an <em>structured</em> collection of surfaces, e.g., a list.
Then we could combine the collections element-wise.
Again, we&#8217;d have to deal with the possibility that the collections do not match up.</p>

<h3>Surface tuples</h3>

<p>Let&#8217;s briefly return to a simpler model of surfaces:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Surf = R2 -&gt; R3</pre>
</div></p>

<p>We could represent a collection of such surfaces as a structured collection, e.g., a list:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Geometry = <span style="color: green;">&#91;</span>Surf<span style="color: green;">&#93;</span></pre>
</div></p>

<p>But then the type doesn&#8217;t capture the number of surfaces, leading to mismatches when combining geometry values point-wise.</p>

<p>Alternatively, we could make the number of surfaces explicit in the type, via tuples, possibly nested.
For instance, two surfaces would have type <code>(Surf,Surf)</code>.</p>

<p>Interpolation in this model becomes very simple.
A general interpolator works on vector spaces:</p>

<p><div>
<pre class="haskell">lerp :: VectorSpace v =&gt; v -&gt; v -&gt; Scalar v -&gt; v
lerp a b t = a ^+^ t*^<span style="color: green;">&#40;</span>b ^-^ a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>or on affine spaces:</p>

<p><div>
<pre class="haskell">alerp :: <span style="color: green;">&#40;</span>AffineSpace p, VectorSpace <span style="color: green;">&#40;</span>Diff p<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> =&gt;
         p -&gt; p -&gt; Scalar <span style="color: green;">&#40;</span>Diff p<span style="color: green;">&#41;</span> -&gt; p
alerp p p' s = p .+^ s*^<span style="color: green;">&#40;</span>p' .-. p<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Both definitions are in the <a href="http://haskell.org/haskellwiki/vector-space" title="Library wiki page">vector-space</a> package.
That package also includes <code>VectorSpace</code> and <code>AffineSpace</code> instances for both functions and tuples.
These instances, together with instances for real values suffice to make (possibly nested) tuples of surfaces be vector spaces and affine spaces.</p>

<h3>From products to sums</h3>

<p>Function pairing admits some useful isomorphisms.
One replaces a product with a product:</p>

<!-- <div class=latex><img src='http://conal.net/blog/wp-content/latex/584/584e9fae120440616c7b25c5f45c4a11-ffffff000000.png' alt='(a \to b) \times (a \to c) \cong a \to (b \times c)' class='latex' /></div> -->

<p><div>
<pre class="haskell"><span style="color: green;">&#40;</span>a → b<span style="color: green;">&#41;</span> × <span style="color: green;">&#40;</span>a → c<span style="color: green;">&#41;</span> ≅ a → <span style="color: green;">&#40;</span>b × c<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Using this product/product isomorphism, we could replace tuples of surfaces with a single function from <em>R<sup>2</sup></em> to tuples of <em>R<sup>3</sup></em>.</p>

<p>There is also a handy isomorphism that relates products to sums, in the context of functions:</p>

<!-- <div class=latex><img src='http://conal.net/blog/wp-content/latex/94f/94f2e7c17f0492d9b05d1b3f9ee58837-ffffff000000.png' alt='Formula does not parse: (b \to a) \times (c \to a) \cong (b + c) \to a' class='latex' /></div> -->

<p><div>
<pre class="haskell"><span style="color: green;">&#40;</span>b → a<span style="color: green;">&#41;</span> × <span style="color: green;">&#40;</span>c → a<span style="color: green;">&#41;</span> ≅ <span style="color: green;">&#40;</span>b + c<span style="color: green;">&#41;</span> → a</pre>
</div></p>

<p>This second isomorphism lets us replace tuples of surfaces with a single &#8220;surface&#8221;, if we generalize the notion of surface to include domains more complex than <em>R<sup>2</sup></em>.</p>

<p>In fact, these two isomorphisms are uncurried forms of the general and useful Haskell functions <code>(&amp;&amp;&amp;)</code> and <code>(|||)</code>, defined on arrows:</p>

<p><div>
<pre class="haskell"><span style="color: green;">&#40;</span>&amp;&amp;&amp;<span style="color: green;">&#41;</span> :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#t:Arrow"><span style="color: #833; font-weight: bold;">Arrow</span></a>       <span style="color: green;">&#40;</span>~&gt;<span style="color: green;">&#41;</span> =&gt; <span style="color: green;">&#40;</span>a ~&gt; b<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a ~&gt; c<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a ~&gt; <span style="color: green;">&#40;</span>b,c<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
<span style="color: green;">&#40;</span>|||<span style="color: green;">&#41;</span> :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#t:ArrowChoice"><span style="color: #833; font-weight: bold;">ArrowChoice</span></a> <span style="color: green;">&#40;</span>~&gt;<span style="color: green;">&#41;</span> =&gt; <span style="color: green;">&#40;</span>a ~&gt; c<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>b ~&gt; c<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="color: #833; font-weight: bold;">Either</span></a> a b ~&gt; c<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Restricted to the function arrow, <code>(|||) == either</code>.</p>

<p>The second isomorphism, <code>uncurry (|||)</code>, has another benefit.
Relaxing the domain type to allow sums opens the way to other domain variations as well.
For instance, we can have types for triangular domains, shapes with holes, and other flavors of bounded and unbounded parameter spaces.
All of these domains are two-dimensional, although they may result from several patches.</p>

<p>Our <code>Geometry</code> type now becomes parameterized:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Geometry a = a -&gt; <span style="color: green;">&#40;</span>R3,Material<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The first isomorphism, <code>uncurry (&amp;&amp;&amp;)</code>, is also useful in a geometric setting.
Think of each component of the range type (here <code>R3</code> and <code>Material</code>) as a surface &#8220;attribute&#8221;.
Then <code>(&amp;&amp;&amp;)</code> merges two compatible geometries, including attributes from each.
Attributes could include position (and derivatives) and shading-related material, as well as non-visual properties like temperature, elasticity, stickiness, etc.</p>

<p>With this flexibility in mind, <code>Geometry</code> gets a second type parameter, which is the range type.
Now there&#8217;s nothing left of the <code>Geometry</code> type but general functions:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Geometry = <span style="color: green;">&#40;</span>-&gt;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Recall that we&#8217;re looking for a <em>semantics</em> for 3D geometry.
The <em>type</em> for <code>Geometry</code> might be abstract, with <code>(-&gt;)</code> being its semantic model.
In that case, the model suggests that <code>Geometry</code> have all of the same type class instances that <code>(-&gt;)</code> (and its full or partial applications) has, including <code>Monoid</code>, <code>Functor</code>, <code>Applicative</code>, <code>Monad</code>, and <code>Arrow</code>.
The semantics of these instances would be given by the corresponding instances for <code>(-&gt;)</code>.
(See posts on <a href="http://conal.net/blog/tag/type-class-morphism/" title="Posts on type class morphisms">type class morphisms</a> and the paper <em><a href="http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/" title="blog post">Denotational design with type class morphisms</a></em>.)</p>

<p>Or drop the notion of <code>Geometry</code> altogether and use functions directly.</p>

<h3>Domains</h3>

<p>I&#8217;m happy with the simplicity of geometry as functions.
Functions fit the flexibility of programmable GPUs, and they provide simple, powerful &amp; familiar notions of attribute merging (<code>(&amp;&amp;&amp;)</code>) and union (<code>(|||)</code>/<code>either</code>).</p>

<p>The main question I&#8217;m left with: what are the domains?</p>

<p>One simple domain is a one-dimensional interval, say [-1,1].</p>

<p>Two useful domain building blocks are sum and product.
I mentioned sum above, in connection with geometric union (<code>(|||)</code>/<code>either</code>)
Product combines domains into higher-dimensional domains.
For instance, the product of two 1D intervals is a 2D interval (axis-aligned filled rectangle), which is handy for some parametric surfaces.</p>

<p>What about other domains, e.g., triangular, or having one more holes?  Or multi-way branching surfaces?  Or unbounded?</p>

<p>One idea is to stitch together simple domains using sum.
We don&#8217;t have to build any particular spatial shapes or sizes, since the &#8220;geometry&#8221; functions themselves yield the shape and size.
For instance, a square region can be mapped to a triangular or even circular region.
An infinite domain can be stitched together from infinitely many finite domains.
Or it can be mapped to from a single finite domain.
For instance, the function <code>\ x -&gt; x / abs (1-x)</code> maps [-1,1] to [-∞,∞].</p>

<p>Alternatively, we could represent domains as typed predicates (characteristic functions).
For instance, the closed interval [-1,1] would be <code>\ x -&gt; abs x &lt;= 1</code>.
Replacing <code>abs</code> with <code>magnitude</code> (for <a href="http://hackage.haskell.org/packages/archive/vector-space/latest/doc/html/Data-VectorSpace.html#t%3AInnerSpace">inner product spaces</a>), generalizes this formulation to encompass [-1,1] (1D), a unit disk (2D), and a unit ball (3D).</p>

<p>I like the simple generality of the predicate approach, while I like how the pure type approach supports interpolation and other pointwise operations (via <code>liftA2</code> etc).</p>

<h3>Tessellation</h3>

<p>I&#8217;ve intentionally formulated the graphics semantics over continuous space, which makes it resolution-independent and easy to compose.
(This formulation is typical for 3D geometry and 2D vector graphics.
The benefits of continuity apply to generally <a href="http://conal.net/Pan/Gallery" title="Pan image gallery">imagery</a> and to <a href="http://conal.net/Fran/tutorial.htm" title="Animated tutorial: &quot;Composing Reactive Animations&quot;">animation/behavior</a>.)</p>

<p>Graphics hardware specializes in finite collections of triangles.
For rendering, curved surfaces have to be <em>tessellated</em>, i.e., approximated as collections of triangles.
Desirable choice of tessellation depends on characteristics of the surface and of the view, as well as scene complexity and available CPU and GPU resources.
Formulating geometry in its ideal curved form allows for automated analysis and choice of tessellation.
For instance, since triangles are linear, the error of a triangle relative to the surface it approximates depends on how <em>non-linear</em> the surface is over the subset of its domain corresponding to the triangle.
Using <a href="http://en.wikipedia.org/wiki/Talk:Interval_arithmetic" title="Wikipedia page on interval analysis/arithmetic">interval analysis</a> and <a href="http://conal.net/blog/tag/derivative/" title="Posts on derivatives">derivatives</a>, non-linearity can be measured as a size bound on the second derivative or a range of first derivative.
Error could also be analyzed in terms of the resulting image rather than the surface.</p>

<p>For a GPU-based implementation, one could tessellate dynamically, in a &#8220;geometry shader&#8221; or (I presume) in a more general framework like CUDA or OpenCL.</p>

<h3>Abstractness</h3>

<p>A denotational model is &#8220;fully abstract&#8221; when it equates observationally equivalent terms.
The parametric model of surfaces is not fully abstract in that reparameterizing a surface yields a different function that looks the same as a surface.
(Surface reparametrization alters the relationship between domain and range, while covering exactly the same surface, geometrically.)
Properties that are independent of particular parametrization are called &#8220;geometric&#8221;, which I think corresponds to full abstraction (considering those properties as semantic functions).</p>

<p>What might a fully abstract (geometric) model for geometry be?</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/thoughts-on-semantics-for-3d-graphics/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Memoizing polymorphic functions &#8211; part two</title>
		<link>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-two/</link>
		<comments>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-two/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 22:04:01 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[memoization]]></category>
		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=89</guid>
		<description><![CDATA[





Part one of this series introduced the problem of memoizing functions involving polymorphic recursion.
The caching data structures used in memoization typically handle only one type of argument at a time.
For instance, one can have finite maps of differing types, but each concrete finite map holds just one type of key and one type of value.

I [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Memoizing polymorphic functions - part two
Tags: memoization, polymorphism

URL: http://conal.net/blog/posts/memoizing-polymorphic-functions-part-two/

-->

<!-- references -->

<!-- teaser -->

<p><a href="http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/" title="Blog post">Part one</a> of this series introduced the problem of memoizing functions involving polymorphic recursion.
The caching data structures used in memoization typically handle only one <em>type</em> of argument at a time.
For instance, one can have finite maps of differing types, but each concrete finite map holds just one type of key and one type of value.</p>

<p>I extended memoization to handle polymorphic recursion by using an existential type together with a reified type of types.
This extension works (afaik), but it is restricted to a particular form for the type of the polymorphic function being memoized, namely</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">-- Polymorphic function</span>
<span style="color: #050; font-weight: bold;">type</span> k :<span style="color: #5d478b; font-style: italic;">--&gt; v = forall a. HasType a =&gt; k a -&gt; v a</span></pre>
</div></p>

<p>My motivating example is a GADT-based representation of typed lambda calculus, and some of the functions I want to memoize do not fit the pattern.
After writing <a href="http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/" title="Blog post">part one</a>, I fooled around and found that I could transform these awkwardly typed polymorphic functions into isomorphic form that does indeed fit the restricted pattern of polymorphic types I can handle.</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-89"></span></p>

<h3>Awkward types</h3>

<p>The first awkwardly typed memoizee is the function application constructor:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> AT = <span style="color: #050; font-weight: bold;">forall</span> a b . <span style="color: green;">&#40;</span>HasType a, HasType b<span style="color: green;">&#41;</span> =&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; E a -&gt; E b
&nbsp;
<span style="color: green;">&#40;</span>:^<span style="color: green;">&#41;</span> :: AT</pre>
</div></p>

<p>Right away <code>AT</code> misses the required form.
It has two <code>HasType</code> constraints, and the first argument is parameterized over two type variables instead of one.
However, the second argument looks more promising, so let&#8217;s <code>flip</code> the arguments to get an isomorphic type:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> a b . <span style="color: green;">&#40;</span>HasType a, HasType b<span style="color: green;">&#41;</span> =&gt; E a -&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; E b</pre>
</div></p>

<p>And then move the quantifier and constraint on <code>b</code> inside the outer (first) <code>-&gt;</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> a . HasType a =&gt; E a -&gt; <span style="color: green;">&#40;</span><span style="color: #050; font-weight: bold;">forall</span> b. HasType b =&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; E b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>We&#8217;re getting closer.
Next, define a <code>newtype</code> wrapper.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> A2 a = A2 <span style="color: green;">&#40;</span><span style="color: #050; font-weight: bold;">forall</span> b. HasType b =&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; E b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>So that <code>AT</code> is isomorphic to</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> a . HasType a =&gt; E a -&gt; A2 a</pre>
</div></p>

<p>i.e.,</p>

<p><div>
<pre class="haskell">E :<span style="color: #5d478b; font-style: italic;">--&gt; A2</span></pre>
</div></p>

<p>The function inside of <code>A2</code> doesn&#8217;t have the required form, but another <code>newtype</code> wrapper finishes the job.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> EF a b = EF <span style="color: green;">&#123;</span>unEF :: E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> <span style="color: green;">&#125;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> H' a = EF a :<span style="color: #5d478b; font-style: italic;">--&gt; E</span>
&nbsp;
<span style="color: #050; font-weight: bold;">newtype</span> H a = H <span style="color: green;">&#123;</span> unH :: H' a <span style="color: green;">&#125;</span></pre>
</div></p>

<p>The <code>AT</code> type is isomorphic <code>AP</code> where</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> AP = E :<span style="color: #5d478b; font-style: italic;">--&gt; H</span></pre>
</div></p>

<h3>Curried memoization</h3>

<p>A &#8220;curried memo function&#8221; is one that takes one argument and produces another memo function.
For a simple memo function, not involving polymorphic recursion, there&#8217;s a simple recipe for curried memoization:</p>

<p><div>
<pre class="haskell">memo2 :: <span style="color: green;">&#40;</span>a -&gt; b -&gt; c<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a -&gt; b -&gt; c<span style="color: green;">&#41;</span>
memo2 f = memo <span style="color: green;">&#40;</span>memo . f<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Our more polymorphic <code>memo</code> makes currying a little more awkward.
First, here&#8217;s a helper function for working <em>inside</em> of the representation of an <code>H</code>:</p>

<p><div>
<pre class="haskell">inH :: <span style="color: green;">&#40;</span>H' a -&gt; H' a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>H a -&gt; H a<span style="color: green;">&#41;</span>
inH h z = H <span style="color: green;">&#40;</span>h <span style="color: green;">&#40;</span>unH z<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>The following more elegant definition doesn&#8217;t type-check, due to the rank 2 polymorphism:</p>

<p><div>
<pre class="haskell">inH f = H . f . unH  <span style="color: #5d478b; font-style: italic;">-- type error</span></pre>
</div></p>

<p>Now our <code>AP</code> memoizer is much like <code>memo2</code>:</p>

<p><div>
<pre class="haskell">memoAP :: AP -&gt; AP
memoAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>' = memo <span style="color: green;">&#40;</span>inH memo . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>'<span style="color: green;">&#41;</span></pre>
</div></p>

<p>(A more general, consistent type for <code>memoAP</code> is <code>(f :--&gt; H) -&gt; (f :--&gt; H)</code>.)</p>

<h3>Isomorphisms</h3>

<p>Now, to define the isomorphisms.  Define</p>

<p><div>
<pre class="haskell">toAP   :: AT -&gt; AP
fromAP :: AP -&gt; AT</pre>
</div></p>

<p>The definitions:</p>

<p><div>
<pre class="haskell">toAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a> ea = H $ \ <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a> eab ea
fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>' eab ea = unH <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>' ea<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span></pre>
</div></p>

<p>If you erase the <code>newtype</code> wrappers &amp; unwrappers, you&#8217;ll see that <code>toAP</code> and <code>fromAP</code> are both just <code>flip</code>.</p>

<p>I constructed <code>fromAP</code> from the following specification:</p>

<p><div>
<pre class="haskell">toAP <span style="color: green;">&#40;</span>fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>'<span style="color: green;">&#41;</span> == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>'</pre>
</div></p>

<p>Transforming step-by-step into equivalent specifications:</p>

<p><div>
<pre class="haskell">\ ea -&gt; H $ \ <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>'<span style="color: green;">&#41;</span> eab ea == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>'
&nbsp;
H $ \ <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>'<span style="color: green;">&#41;</span> eab ea == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>' ea
&nbsp;
\ <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>'<span style="color: green;">&#41;</span> eab ea == unH <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>' ea<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: green;">&#40;</span>fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>'<span style="color: green;">&#41;</span> eab ea == unH <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>' ea<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span>
&nbsp;
fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>' eab ea == unH <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>' ea<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span></pre>
</div></p>

<h3>Memoizing vis isomorphisms</h3>

<p>Finally, I can memoize</p>

<p><div>
<pre class="haskell">memoAT :: AT -&gt; AT
memoAT <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a> = fromAP <span style="color: green;">&#40;</span>memoAP <span style="color: green;">&#40;</span>toAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Again, a more elegant definition via <code>(.)</code> fails to type-check, due to rank 2 polymorphism.</p>

<p>The <code>Lam</code> (lambda abstraction) constructor can be handled similarly:</p>

<p><div>
<pre class="haskell">Lam  :: <span style="color: green;">&#40;</span>HasType a, HasType b<span style="color: green;">&#41;</span> =&gt; V a -&gt; E b -&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This time, no <code>flip</code> is needed.</p>

<h3>I wonder</h3>

<p>How far does this isomorphism trick go?</p>

<p>Is there an easier way to memoize polymorphic functions?</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-two/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Memoizing polymorphic functions &#8211; part one</title>
		<link>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/</link>
		<comments>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 00:36:34 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[memoization]]></category>
		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=88</guid>
		<description><![CDATA[





Memoization takes a function and gives back a semantically equivalent function that reuses rather than recomputes when applied to the same argument more than once.
Variations include not-quite-equivalence due to added strictness, and replacing value equality with pointer equality.

Memoization is often packaged up polymorphically:


memo :: &#40;???&#41; =&#62; &#40;k -&#62; v&#41; -&#62; &#40;k -&#62; v&#41;


For pointer-based (&#8220;lazy&#8221;) [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Memoizing polymorphic functions - part one
Tags: memoization, polymorphism

URL: http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/

-->

<!-- references -->

<!-- teaser -->

<p>Memoization takes a function and gives back a semantically equivalent function that reuses rather than recomputes when applied to the same argument more than once.
Variations include <a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/#comment-10166">not-quite-equivalence due to added strictness</a>, and replacing value equality with pointer equality.</p>

<p>Memoization is often packaged up polymorphically:</p>

<p><div>
<pre class="haskell">memo :: <span style="color: green;">&#40;</span>???<span style="color: green;">&#41;</span> =&gt; <span style="color: green;">&#40;</span>k -&gt; v<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>k -&gt; v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>For pointer-based (&#8220;lazy&#8221;) memoization, the type constraint (&#8220;???&#8221;) is empty.
For equality-based memoization, we&#8217;d need at least <code>Eq k</code>, and probably <code>Ord k</code> or <a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post"><code>HasTrie k</code></a> for efficient lookup (in a finite map or a possibly infinite <a href="http://conal.net/blog/tag/memoization/">memo trie</a>).</p>

<p>Although <code>memo</code> is polymorphic, its argument is a <em>monomorphic</em> function.
Implementations that use maps or tries exploit that monomorphism in that they use a type like <code>Map k v</code> or <code>Trie k v</code>.
Each map or trie is built around a particular (monomorphic) type of keys.
That is, a single map or trie does not mix keys of different types.</p>

<p>Now I find myself wanting to memoize <em>polymorphic</em> functions, and I don&#8217;t know how to do it.</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-88"></span></p>

<h3>Flavors of polymorphism</h3>

<p>If a recursively defined function <code>f</code> is polymorphic, then the recursion may still be monomorphic.
That is, recursive calls may be restricted to the same type instance as the parent call.
Most recursive polymorphic functions fit this form, because most polymorphic recursive data types are &#8220;regular&#8221;, meaning that a polymorphic data type is included in itself only at the same type instance.
For instance, the usual polymorphic lists and trees are regular.</p>

<h3>Example: GADTs</h3>

<p>Among other places, non-regular, or &#8220;<a href="ftp://ftp.kestrel.edu/pub/papers/meertens/nest5.ps">nested data types</a>&#8221; arise in statically typed encodings of typed languages.
For instance, here&#8217;s a GADT (generalized algebraic data type) for typed lambda calculus expressions:</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">-- Variables</span>
<span style="color: #050; font-weight: bold;">data</span> V a = V <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #833; font-weight: bold;">String</span></a> <span style="color: green;">&#40;</span>Type a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Expressions</span>
<span style="color: #050; font-weight: bold;">data</span> E :: * -&gt; * <span style="color: #050; font-weight: bold;">where</span>
  Lit  :: a -&gt; E a                      <span style="color: #5d478b; font-style: italic;">-- literal</span>
  Var  :: V  a -&gt; E a                   <span style="color: #5d478b; font-style: italic;">-- variable</span>
  <span style="color: green;">&#40;</span>:^<span style="color: green;">&#41;</span> :: E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; E a -&gt; E b      <span style="color: #5d478b; font-style: italic;">-- application</span>
  Lam  :: V a -&gt; E b -&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span>      <span style="color: #5d478b; font-style: italic;">-- abstraction</span></pre>
</div></p>

<p>The <code>Type</code> type is sort of like <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Typeable.html#v:TypeRep"><code>TypeRep</code></a>, except that it is statically typed.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Type :: * -&gt; * <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #833; font-weight: bold;">Bool</span></a>   :: Type <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #833; font-weight: bold;">Bool</span></a>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #833; font-weight: bold;">Float</span></a>  :: Type <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #833; font-weight: bold;">Float</span></a>
  ...
  <span style="color: green;">&#40;</span>:*:<span style="color: green;">&#41;</span>  :: Type a -&gt; Type b -&gt; Type <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>
  <span style="color: green;">&#40;</span>:-&gt;:<span style="color: green;">&#41;</span> :: Type a -&gt; Type b -&gt; Type <span style="color: green;">&#40;</span>a-&gt;b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>These GADTs (<code>E</code> and <code>Type</code>) are both non-regular, and so recursive functions over them will involve more than one argument type.</p>

<p>So how can we memoize?</p>

<h3>A first try</h3>

<p>Let&#8217;s consider a specific case of polymorphic functions:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> k :<span style="color: #5d478b; font-style: italic;">--&gt; v = ∀ a. HasType a =&gt; k a -&gt; v a</span></pre>
</div></p>

<p><code>HasType</code> is to <code>Typeable</code> as <code>Type</code> is to <code>TypeRep</code>.
The <code>memo</code> implementation I&#8217;m playing with relies on <code>HasType</code>.</p>

<p>The memoizer can have type</p>

<p><div>
<pre class="haskell">memo :: <span style="color: green;">&#40;</span>k :<span style="color: #5d478b; font-style: italic;">--&gt; v) -&gt; (k :--&gt; v)</span></pre>
</div></p>

<p>which uses rank 2 polymorphism (because of the argument type&#8217;s <code>∀</code>, which cannot be moved to the outside).</p>

<p>My implementation of <code>memo</code> is similar to the discussion in <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.1948" title="Paper by Simon Peyton Jones, Simon Marlow, and Conal Elliott">Stretching the storage manager: weak pointers and stable names in Haskell</a></em>, but modernized a bit and adapted for polymorphism.
It uses an <a href="http://hackage.haskell.org/packages/archive/containers/0.2.0.1/doc/html/Data-IntMap.html"><code>IntMap</code></a> of lists of pairs of stable names (akin to pointers) for arguments and values for results.
The idea is to first use <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/System-Mem-StableName.html#v:hashStableName"><code>hashStableName</code></a> to get an <code>Int</code> to use as an <code>IntMap</code> key, and then linearly traverse the resulting list of binding pairs, comparing stable keys for equality.
(<code>StableName</code> has <code>Eq</code> but not <code>Ord</code>.)
Although <code>hashStableName</code> can map different stable names to the same hash value, collisions are rare, so the <code>StableBind</code> lists rarely have more than one element and the linear search is cheap.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> SM k v = I.IntMap <span style="color: green;">&#91;</span>StableBind k v<span style="color: green;">&#93;</span>  <span style="color: #5d478b; font-style: italic;">-- Stable map</span>
&nbsp;
<span style="color: #050; font-weight: bold;">data</span> StableBind k v = ∀ a. HasType a =&gt; SB <span style="color: green;">&#40;</span>StableName <span style="color: green;">&#40;</span>k a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>v a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The reason for hiding the type parameter <code>a</code> in <code>StableName</code> is so that different bindings can be for different types.</p>

<p>The key tricky bit is managing static typing while searching for a particular <code>StableName a</code> in a <code>[StableBind]</code>.
Here&#8217;s my implementation:</p>

<p><div>
<pre class="haskell">blookup :: ∀ k v a. HasType a =&gt;
           StableName <span style="color: green;">&#40;</span>k a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#91;</span>StableBind k v<span style="color: green;">&#93;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span>v a<span style="color: green;">&#41;</span>
blookup stk = look
 <span style="color: #050; font-weight: bold;">where</span>
   look :: <span style="color: green;">&#91;</span>StableBind k v<span style="color: green;">&#93;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span>v a<span style="color: green;">&#41;</span>
   look <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> = Nothing
   look <span style="color: green;">&#40;</span>SB stk' v : binds'<span style="color: green;">&#41;</span> 
     | Just Refl &lt;- tya `tyEq` typeOf2 stk', stk == stk' = Just v
     | <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:otherwise"><span style="font-weight: bold;">otherwise</span></a>                                         = look binds'
   tya :: Type a
   tya = typeT</pre>
</div></p>

<p>The crucial magic bit is</p>

<p><div>
<pre class="haskell">tyEq :: Type a -&gt; Type b -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span>a :=: b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>where <code>a :=: b</code> represents a proof that the types <code>a</code> and <code>b</code> are the same type.
The proof type has a simple GADT representation:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>:=:<span style="color: green;">&#41;</span> :: * -&gt; * -&gt; * <span style="color: #050; font-weight: bold;">where</span> Refl :: a :=: a</pre>
</div></p>

<p>This simple type definition ensures that only valid type-equality proofs can exist.
Well, except for ⊥.
The guard&#8217;s pattern match with <code>Just Refl</code> will force evaluation, so that ⊥ can&#8217;t sneak by us.
That match also informs the type-checker that <code>stk</code> and <code>stk'</code> are stable names <em>of the same type</em> in that clause, which then makes <code>stk == stk'</code> be well-typed, and makes <code>Just v</code> have the required type, i.e., <code>Maybe (v a)</code>.</p>

<p>Finally, the <code>typeOf2</code> function is a simple helper that peels off two type constructors and extracts a <code>Type</code>:</p>

<p><div>
<pre class="haskell">typeOf2 :: HasType a =&gt; g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> -&gt; Type a</pre>
</div></p>

<h3>Wishing for more</h3>

<p>The type of <code>memo</code> above is too restrictive for my uses.
It only handles polymorphic functions of type <code>k a -&gt; v a</code>, and only with the single constraint <code>HasType a</code>.</p>

<p>The reason I want lazy memoization now is that I&#8217;m compressing expressions to maximize representation sharing, as John Hughes described in <em><a href="http://www.cs.chalmers.se/~rjmh/Papers/hughes_85_lazy.pdf" title="Paper by John Hughes">Lazy Memo-functions</a></em>.
Once sharing is maximized, pointer-based memoization works better, because equal values are pointer-equal.
To compress an expression, simply use a memoized copy function, as John suggested.</p>

<p><div>
<pre class="haskell">compress :: HasType a =&gt; E a -&gt; E a
compress e = mcopy e
 <span style="color: #050; font-weight: bold;">where</span>
   mcopy, copy :: HasType b =&gt; E b -&gt; E b
   <span style="color: #5d478b; font-style: italic;">-- Memo version</span>
   mcopy = memo copy
   <span style="color: #5d478b; font-style: italic;">-- Copier, with memo-copied components</span>
   copy <span style="color: green;">&#40;</span>u :^ v<span style="color: green;">&#41;</span>  = appM <span style="color: green;">&#40;</span>mcopy u<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>mcopy v<span style="color: green;">&#41;</span>
   copy <span style="color: green;">&#40;</span>Lam v b<span style="color: green;">&#41;</span> = lamM v <span style="color: green;">&#40;</span>mcopy b<span style="color: green;">&#41;</span>
   copy e         = e
   <span style="color: #5d478b; font-style: italic;">-- memoized constructors</span>
   appM :: <span style="color: green;">&#40;</span>HasType a, HasType b<span style="color: green;">&#41;</span> =&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; E a -&gt; E b
   appM = memo2 <span style="color: green;">&#40;</span>:^<span style="color: green;">&#41;</span>
   lamM :: <span style="color: green;">&#40;</span>HasType a, HasType b<span style="color: green;">&#41;</span> =&gt; V a -&gt; E b -&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span>
   lamM = memo2 Lam</pre>
</div></p>

<p>The <code>memo2</code> function is defined in terms of <code>memo</code>, using some some <code>newtype</code> trickery.
Its type:</p>

<p><div>
<pre class="haskell">memo2 :: HasType a =&gt;
         <span style="color: green;">&#40;</span>k a -&gt; l a -&gt; v a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>k a -&gt; l a -&gt; v a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>But, sigh, the <code>(:^)</code> and <code>Lam</code> constructors I&#8217;m trying to memoize do not have the required types.
My higher-order-polymorphic memo functions do not have flexible enough types.</p>

<p>And this is where I&#8217;m stuck.</p>

<p>I&#8217;d appreciate your ideas and suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>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), is not [...]]]></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>.
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 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 a 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 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>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/the-c-language-is-purely-functional/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<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>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 I&#8217;m looking [...]]]></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>

<!--
**Edits**:

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

<!-- 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 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>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/notions-of-purity-in-haskell/feed/</wfw:commentRss>
		<slash:comments>52</slash:comments>
		</item>
		<item>
		<title>Paper: Beautiful differentiation</title>
		<link>http://conal.net/blog/posts/paper-beautiful-differentiation/</link>
		<comments>http://conal.net/blog/posts/paper-beautiful-differentiation/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 08:05:10 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[beautiful code]]></category>
		<category><![CDATA[calculus on manifolds]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[linear map]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[paper]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=85</guid>
		<description><![CDATA[





I have another paper draft for submission to ICFP 2009.
This one is called Beautiful differentiation, 
The paper is a culmination of the several posts I&#8217;ve written on derivatives and automatic differentiation (AD).
I&#8217;m happy with how the derivation keeps getting simpler.
Now I&#8217;ve boiled extremely general higher-order AD down to a Functor and Applicative morphism.

I&#8217;d love to [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Paper: Beautiful differentiation

Tags: derivative, functor, applicative functor, beautiful code, calculus on manifolds, linear map, math, paper

URL: http://conal.net/blog/posts/paper-beautiful-differentiation/

-->

<!-- references -->

<!-- teaser -->

<p>I have another paper draft for submission to <a href="http://www.cs.nott.ac.uk/~gmh/icfp09.html" title="conference page">ICFP 2009</a>.
This one is called <em><a href="http://conal.net/papers/beautiful-differentiation" title="paper">Beautiful differentiation</a></em>, 
The paper is a culmination of the <a href="http://conal.net/blog/tag/derivative/">several posts</a> I&#8217;ve written on derivatives and automatic differentiation (AD).
I&#8217;m happy with how the derivation keeps getting simpler.
Now I&#8217;ve boiled extremely general higher-order AD down to a <code>Functor</code> and <code>Applicative</code> morphism.</p>

<p>I&#8217;d love to get some readings and feedback.
I&#8217;m a bit over the page the limit, so I&#8217;ll have to do some trimming before submitting.</p>

<p>The abstract:</p>

<blockquote>
  <p>Automatic differentiation (AD) is a precise, efficient, and convenient
  method for computing derivatives of functions. Its implementation can be
  quite simple even when extended to compute all of the higher-order
  derivatives as well. The higher-dimensional case has also been tackled,
  though with extra complexity. This paper develops an implementation of
  higher-dimensional, higher-order differentiation in the extremely
  general and elegant setting of <em>calculus on manifolds</em> and derives that
  implementation from a simple and precise specification.</p>
  
  <p>In order to motivate and discover the implementation, the paper poses
  the question &#8220;What does AD mean, independently of implementation?&#8221; An
  answer arises in the form of <em>naturality</em> of sampling a function and its
  derivative. Automatic differentiation flows out of this naturality
  condition, together with the chain rule. Graduating from first-order to
  higher-order AD corresponds to sampling all derivatives instead of just
  one. Next, the notion of a derivative is generalized via the notions of
  vector space and linear maps. The specification of AD adapts to this
  elegant and very general setting, which even <em>simplifies</em> the
  development.</p>
</blockquote>

<p>You can <a href="http://conal.net/papers/beautiful-differentiation" title="paper">get the paper and see current errata here</a>.</p>

<p>The submission deadline is March 2, so comments before then are most helpful to me.</p>

<p>Enjoy, and thanks!</p>

<!--
**Edits**:

* 2009-02-09: just fiddling around
-->
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/paper-beautiful-differentiation/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Denotational design with type class morphisms</title>
		<link>http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/</link>
		<comments>http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 02:34:08 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[arrow]]></category>
		<category><![CDATA[associated type]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[monoid]]></category>
		<category><![CDATA[paper]]></category>
		<category><![CDATA[semantics]]></category>
		<category><![CDATA[trie]]></category>
		<category><![CDATA[type class morphism]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=84</guid>
		<description><![CDATA[





I&#8217;ve just finished a draft of a paper called Denotational design with type class morphisms, for submission to ICFP 2009.
The paper is on a theme I&#8217;ve explored in several posts, which is semantics-based design, guided by type class morphisms.

I&#8217;d love to get some readings and feedback.
Pointers to related work would be particularly appreciated, as well [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Denotational design with type class morphisms

Tags: paper, semantics, type class morphism, monoid, functor, applicative functor, monad, arrow, associated type, trie

URL: http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/

-->

<!-- references -->

<!-- teaser -->

<p>I&#8217;ve just finished a draft of a paper called <em><a href="http://conal.net/papers/type-class-morphisms" title="paper">Denotational design with type class morphisms</a></em>, for submission to <a href="http://www.cs.nott.ac.uk/~gmh/icfp09.html" title="conference page">ICFP 2009</a>.
The paper is on a theme I&#8217;ve explored in <a href="http://conal.net/blog/tag/type-class-morphism/">several posts</a>, which is semantics-based design, guided by type class morphisms.</p>

<p>I&#8217;d love to get some readings and feedback.
Pointers to related work would be particularly appreciated, as well as what&#8217;s unclear and what could be cut.
It&#8217;s an entire page over the limit, so I&#8217;ll have to do some trimming before submitting.</p>

<p>The abstract:</p>

<blockquote>
  <p>Type classes provide a mechanism for varied implementations of standard
  interfaces. Many of these interfaces are founded in mathematical
  tradition and so have regularity not only of <em>types</em> but also of
  <em>properties</em> (laws) that must hold. Types and properties give strong
  guidance to the library implementor, while leaving freedom as well. Some
  of the remaining freedom is in <em>how</em> the implementation works, and some
  is in <em>what</em> it accomplishes.</p>
  
  <p>To give additional guidance to the <em>what</em>, without impinging on the
  <em>how</em>, this paper proposes a principle of <em>type class morphisms</em> (TCMs),
  which further refines the compositional style of denotational
  semantics. The TCM idea is simply that <em>the instance&#8217;s meaning is the
  meaning&#8217;s instance</em>. This principle determines the meaning of each type
  class instance, and hence defines correctness of implementation. In some
  cases, it also provides a systematic guide to implementation, and in
  some cases, valuable design feedback.</p>
  
  <p>The paper is illustrated with several examples of type, meanings, and
  morphisms.</p>
</blockquote>

<p>You can <a href="http://conal.net/papers/type-class-morphisms" title="paper">get the paper and see current errata here</a>.</p>

<p>The submission deadline is March 2, so comments before then are most helpful to me.</p>

<p>Enjoy, and thanks!</p>

<!--
**Edits**:

* 2009-02-09: just fiddling around
-->
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
