<?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>João Ferreira &#187; Programming</title>
	<atom:link href="http://www.joaoff.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joaoff.com</link>
	<description>Programming, Algorithms, and Mathematics</description>
	<lastBuildDate>Wed, 09 Jun 2010 10:41:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Probabilities in Proofreading</title>
		<link>http://www.joaoff.com/2009/09/14/probabilities-in-proofreading/</link>
		<comments>http://www.joaoff.com/2009/09/14/probabilities-in-proofreading/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 14:04:36 +0000</pubDate>
		<dc:creator>jff</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computing Science]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[calculational]]></category>
		<category><![CDATA[polya]]></category>
		<category><![CDATA[probabilities]]></category>
		<category><![CDATA[probability]]></category>
		<category><![CDATA[proof]]></category>
		<category><![CDATA[proofreading]]></category>
		<category><![CDATA[proofs]]></category>

		<guid isPermaLink="false">http://www.joaoff.com/?p=267</guid>
		<description><![CDATA[Suppose you write a program and you send the source code to two of your friends, and . Your two friends read the code and when they finish, A errors are detected by , B errors are detected by , and C errors are detected by both. So, in total, A+B-C errors are detected and [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you write a program and you send the source code to two of your friends, <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/c9f921b61d2fff93afaa651d353c1795.png' title="LaTeX Formula" alt="LaTeX Formula" /> and <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/42cf2846c21109921b17709ef41d2489.png' title="LaTeX Formula" alt="LaTeX Formula" />. Your two friends read the code and when they finish, <em>A</em> errors are detected by <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/c9f921b61d2fff93afaa651d353c1795.png' title="LaTeX Formula" alt="LaTeX Formula" />, <em>B</em> errors are detected by <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/42cf2846c21109921b17709ef41d2489.png' title="LaTeX Formula" alt="LaTeX Formula" />, and <em>C</em> errors are detected by both. So, in total, <em>A+B-C</em> errors are detected and can now be eliminated. We wish to <em>estimate</em> the number of errors that remain unnoticed and uncorrected.</p>
<p>The original version of this problem concerns manuscripts and proofreaders, instead of source code and programmers. It was posed and solved by George Polya and published in 1976 on <a href="http://www.maa.org/pubs/monthly.html" title="The American Mathematical Monthly Journal">The American Mathematical Monthly</a> under the name of <em>Probabilities in Proofreading</em>. Because the problem is interesting and Polya&#8217;s solution is short and elegant, I have decided to record and share it. Also, since code sharing and reading is a frequent activity in the software development world, estimating the desired value can be helpful for some readers of this blog.</p>
<h3>Estimating the number of unnoticed errors</h3>
<p>Let <em>E</em> be the number of all errors, noticed and unnoticed, in the source code. Our goal is to estimate the value of <em>E-(A+B-C)</em>. Let <em>p</em> be the probability that friend <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/c9f921b61d2fff93afaa651d353c1795.png' title="LaTeX Formula" alt="LaTeX Formula" /> notices any given error and <em>q</em> the analogous probability for friend <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/42cf2846c21109921b17709ef41d2489.png' title="LaTeX Formula" alt="LaTeX Formula" />. The expected number of errors that may be detected by <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/c9f921b61d2fff93afaa651d353c1795.png' title="LaTeX Formula" alt="LaTeX Formula" /> is <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/8cc3bbe573866e221e7058d5c94f4d29.png' title="LaTeX Formula" alt="LaTeX Formula" /> and by <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/42cf2846c21109921b17709ef41d2489.png' title="LaTeX Formula" alt="LaTeX Formula" /> is <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/7056be452630dd69988efc1014cb132a.png' title="LaTeX Formula" alt="LaTeX Formula" />. Assuming that these probabilities are independent, the expected number of errors that may be mutually detected by both friends is <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/11091e85ba69d9e4f9df5448109fab45.png' title="LaTeX Formula" alt="LaTeX Formula" />.</p>
<p>Because we are interested in an <em>estimate</em>, we can safely assume that the expected numbers are approximately equal to the number of errors detected, that is, <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/f6b99c84e75877cc3b7a90c154ac13d5.png' title="LaTeX Formula" alt="LaTeX Formula" />, <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/7f97b9bcffe46f8d265985d6c19c88c7.png' title="LaTeX Formula" alt="LaTeX Formula" />, and <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/2c4ca7fc8699851220220bd5da2ff535.png' title="LaTeX Formula" alt="LaTeX Formula" />. (We use the notation <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/39336a2ffd276833bc2af414ed460bfa.png' title="LaTeX Formula" alt="LaTeX Formula" /> to denote that two numbers are approximately equal.)</p>
<p>We now have all the ingredients to conclude the solution. Recall that our goal is to estimate the value of <em>E-(A+B-C)</em>. We calculate:</p>
<p><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/d5bab5f3311a1d1c63abbb8274ac53ac.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>This is the desired estimate!</p>
<p style="margin-top: 10px;"><strong>Related Articles:</strong></p><ul><li><a href="http://www.joaoff.com/2008/05/09/multiples-in-the-fibonacci-series/" rel="bookmark">Multiples in the Fibonacci series</a></li><li><a href="http://www.joaoff.com/2008/02/11/direct-proofs/" rel="bookmark">Calculational proofs are usually direct</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.joaoff.com/2009/09/14/probabilities-in-proofreading/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A Calculational Proof of the Handshaking Lemma</title>
		<link>http://www.joaoff.com/2009/04/07/calculational-proof-handshaking-lemma/</link>
		<comments>http://www.joaoff.com/2009/04/07/calculational-proof-handshaking-lemma/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 21:11:55 +0000</pubDate>
		<dc:creator>jff</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computing Science]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Methodology]]></category>
		<category><![CDATA[calculational]]></category>
		<category><![CDATA[goal-oriented]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[graph theory]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[proof]]></category>
		<category><![CDATA[quantifiers]]></category>
		<category><![CDATA[vertex]]></category>

		<guid isPermaLink="false">http://www.joaoff.com/?p=86</guid>
		<description><![CDATA[In graph theory, the degree of a vertex A, d.A, is the number of edges incident with the vertex A, counting loops twice. So, considering graph 0 below, we have d.A=3, d.B=3, d.C=1, d.D=3, and d.E=2. A well-known property is that every undirected graph contains an even number of vertices with odd degree. The result [...]]]></description>
			<content:encoded><![CDATA[<p>In graph theory, the degree of a vertex <em>A</em>, <em>d.A</em>, is the number of edges incident with the vertex A, counting loops twice. So, considering graph 0 below, we have <em>d.A=3</em>, <em>d.B=3</em>, <em>d.C=1</em>, <em>d.D=3</em>, and <em>d.E=2</em>.</p>
<div id="attachment_90" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.joaoff.com/wp-content/uploads/2009/03/graph.png"><img class="size-medium wp-image-90" title="Figure 0: Example of an undirected graph with five nodes" src="http://www.joaoff.com/wp-content/uploads/2009/03/graph.png" alt="Example of an undirected graph with five nodes" width="300" height="229" /></a><p class="wp-caption-text">Graph 0: Example of an undirected graph with five nodes</p></div>
<p>A well-known property is that every undirected graph contains an even number of vertices with odd degree. The result first appeared in <a href="http://math.dartmouth.edu/~euler/docs/originals/E053.pdf">Euler&#8217;s 1736 paper</a> on the <a href="http://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg">Seven Bridges of Königsberg</a> and is also known as <a href="http://en.wikipedia.org/wiki/Handshaking_lemma#Handshaking_lemma">the handshaking lemma</a> (that&#8217;s because another way of formulating the property is that the number of people that have shaken hands an odd number of times is even).</p>
<p>As we can easily verify, graph 0 satisfies this property. There are four vertices with odd degree (<em>A</em>,<em>B</em>,<em>C</em>, and <em>D</em>), and 4, of course, is an even number.</p>
<p>Although the proof of this property is simple, I have never seen it proved in a calculational and goal-oriented way. My aim with this post is to show you a development of a goal-oriented proof.<br />
<span id="more-86"></span><br />
Before we start, let me explain the notations that I use. I assume the existence of two predicates, <em>even</em> and <em>odd</em>, that test the parity of numbers. For example, <em>even.8</em> and <em>odd.3</em> are both <em>true</em>, and <em>even.5</em> and <em>odd.6</em> are both <em>false</em>. Also, I use the so-called Eindhoven notation for quantifiers; for example, to express the sum of all natural even numbers less than 50 I write <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/be3b25ccb6b37e8248904e21294b3ddf.png' title="LaTeX Formula" alt="LaTeX Formula" />, and instead of writing <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/11d37b46677a90b3273eef87d2744aca.png' title="LaTeX Formula" alt="LaTeX Formula" /> , I write <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/d33282de72be9e825fe5b452d1d0a305.png' title="LaTeX Formula" alt="LaTeX Formula" />.</p>
<p>Now, the first step in any goal-oriented solution is to express the goal. In other words, what do we want to prove or calculate? Using the notation just described and assuming that <em>V</em> is the set of all vertices, our goal is to determine the value of the following expression:</p>
<div class="latex-margin">
<img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/b3bb6964f72c90aa61c7427867a66568.png' title="LaTeX Formula" alt="LaTeX Formula" />
</div>
<p>Note that we are adding 1 (counting) for each node <em>a</em> in <em>V</em> with an odd degree. We then apply the predicate <em>even</em> to the result. If the result is <em>true</em>, there is an even number of vertices with odd degree; otherwise, there is an odd number. Our goal is thus to determine its value. (We know that it must evaluate to <em>false</em>, because the property is well-known. However, in general, when doing mathematics, we don&#8217;t know what is the final value; that is why goal-oriented and calculational proofs are important.)</p>
<p>We know that the predicate <em>even</em> distributes over addition, so we calculate:</p>
<p><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/4e8aa954abd05c14d92756a7ac227f40.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>This calculation shows that the parity of the number of vertices with odd degree is the same as the parity of the sum of all the degrees. But because each edge has two ends, the sum of all the degrees is simply twice the total number of edges. We thus have:</p>
<p><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/d52d70c3553e9e22ee975f34a594f8b6.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>And so we can conclude that every undirected graph contains an even number of vertices with odd degree.</p>
<h2>What is wrong with conventional solutions?</h2>
<p>Conventional solutions for this problem are usually very similar to the following one, taken from the book &#8220;Ingenuity in Mathematics&#8221; (p. 8), by Ross Honsberger:</p>
<blockquote><p>
The proof in general is simple. We denote by T the total of all the local degrees:</p>
<p>(1) T = d(A) + d(B) + d(C) + &#8230; + d(K) .</p>
<p>In evaluating T we count the number of edges running into A, the number into B, etc., and add. Because each edge has two ends, T is simply twice the number of edges; hence T is even.</p>
<p>Now the values d(P) on the right-hand side of (1) which are even add up to a sub-total which is also even. The remaining values d(P) each of which is odd, must also add up to an even sub-total (since T is even). This shows that there is an even number of odd d(P)&#8217;s (it takes an even number of odd numbers to give an even sum). Thus there must be an even number of vertices with odd local degree.
</p></blockquote>
<p>There is nothing <em>seriously</em> wrong with this solution. It clearly shows why the property holds. However, it is, in my view, oriented to verification: it starts by introducing the total sum of all the local degrees, observing that its value is even; then it analyses that sum to conclude the property. My question is: how can we teach students to come with the total sum of all the local degrees? In general, how can we teach students to come with seemingly unrelated concepts that will be crucial in the development of their arguments? I don&#8217;t think we can.</p>
<p>On the other hand, if we look at the goal-oriented proof, we see that the goal is simple to express. Furthermore, with some training, most students would write it correctly and would be able to calculate that the parity of the number of vertices with odd degree is the same as the parity of the sum of all the degrees. And then (and only then) the introduction of the total sum of all the degrees would make sense. In a way, goal-oriented calculations are like that famous <a href="http://en.wikipedia.org/wiki/Breaking_the_Magician%27s_Code">masked magician that reveals magic&#8217;s biggest secrets</a>, for they reveal how the rabbit got into the hat.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joaoff.com/2009/04/07/calculational-proof-handshaking-lemma/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>In Defense Of Computer Science</title>
		<link>http://www.joaoff.com/2008/01/30/in-defense-of-computer-science/</link>
		<comments>http://www.joaoff.com/2008/01/30/in-defense-of-computer-science/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 13:10:48 +0000</pubDate>
		<dc:creator>jff</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Links]]></category>

		<guid isPermaLink="false">http://www.joaoferreira.org/2008/01/30/in-defense-of-computer-science/</guid>
		<description><![CDATA[Very good post from Paper Trail: So why study computer science? The job prospects at the end are usually pretty good &#8211; because, if nothing else, you can become a pretty good software developer fairly quickly &#8211; but they are unknown. My argument is that the study of computer science is enough of an incentive [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://hnr.dnsalias.net/wp/?p=39" title="In Defense of Computer Science">Very good post</a> from <a href="http://hnr.dnsalias.net/wp" title="Paper Trail">Paper Trail</a>:</p>
<blockquote><p>So why study computer science? The job prospects at the end are usually pretty good &#8211; because, if nothing else, you can become a pretty good software developer fairly quickly &#8211; but they are unknown. My argument is that the study of computer science is enough of an incentive enough to make it worthwhile.</p></blockquote>
<p style="margin-top: 10px;"><strong>Related Articles:</strong></p><ul><li><a href="http://www.joaoff.com/2008/01/22/the-programmers-of-tomorrow/" rel="bookmark">The programmers of tomorrow</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.joaoff.com/2008/01/30/in-defense-of-computer-science/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The programmers of tomorrow</title>
		<link>http://www.joaoff.com/2008/01/22/the-programmers-of-tomorrow/</link>
		<comments>http://www.joaoff.com/2008/01/22/the-programmers-of-tomorrow/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 23:43:00 +0000</pubDate>
		<dc:creator>jff</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Methodology]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[university]]></category>

		<guid isPermaLink="false">http://www.joaoferreira.org/2008/01/22/the-programmers-of-tomorrow/</guid>
		<description><![CDATA[A recent article written by Dr. Robert B.K. Dewar and Dr. Edmond Schonberg (both from AdaCore Inc.) is generating some discussion on the state of Computer Science (CS) education in the United States. In &#8220;Computer Science Education: Where Are the Software Engineers of Tomorrow?&#8220;, Dewar and Schonberg claim that U.S. universities are training unqualified and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.stsc.hill.af.mil/CrossTalk/2008/01/0801DewarSchonberg.html" title="Computer Science Education: Where Are the Software Engineers of Tomorrow?">A recent article written by Dr. Robert B.K. Dewar and Dr. Edmond Schonberg</a> (both from AdaCore Inc.) is generating <a href="http://reddit.com/r/programming/info/665i5/comments/" title="Some comments about Dewar and Schonberg article on reddit.com">some</a> discussion on the state of Computer Science (CS) education in the United States. In &#8220;<a href="http://www.stsc.hill.af.mil/CrossTalk/2008/01/0801DewarSchonberg.html" title="Computer Science Education: Where Are the Software Engineers of Tomorrow?">Computer Science Education: Where Are the Software Engineers of Tomorrow?</a>&#8220;, Dewar and Schonberg claim that U.S. universities are training unqualified and easily replaceable programmers. </p>
<blockquote><p>&#8220;It is our view that Computer Science (CS) education is neglecting basic skills, in particular in the areas of programming and formal methods. We consider that the general adoption of Java as a first programming language is in part responsible for this decline. We examine briefly the set of programming skills that should be part of every software professional’s repertoire.&#8221;</p></blockquote>
<p>The comment about Java&#8217;s adoption annoyed some Java aficionados, but <a href="http://itmanagement.earthweb.com/career/article.php/11067_3722876_1" title="Who Killed the Software Engineer? (Hint: It Happened in College)">in a recent interview</a>, Robert Dewar adds that the problem goes far beyond the choice of Java as the first programming language. The real problem is that CS programs are being dumbed down, so that they become more accessible and popular. In result, they &#8220;are not rigorous enough and don&#8217;t promote in-depth thinking and problem solving&#8221;.</p>
<blockquote><p>“A lot of it is, ‘Let’s make this all more fun.’ You know, ‘Math is not fun, let’s reduce math requirements. Algorithms are not fun, let’s get rid of them. <i>Ewww</i> – graphic libraries, they’re <i>fun</i>. Let’s have people mess with libraries. And [forget] all this business about ‘command line’ – we’ll have people use nice visual interfaces where they can point and click and do fancy graphic stuff and have <i>fun</i>.&#8221;</p></blockquote>
<p>Although the paper is concerned with the American reality, I believe we have the same problem in Europe &mdash; at least, and as far as I know, in the UK and in Portugal. However, in my opinion, the problem starts before university. The maths&#8217;s programs in secondary schools are also being simplified (or dumbed down, if you prefer) and many important concepts, like logic and proofs, are being ignored.</p>
<p>In result, first-year students usually have a poor background on maths and problem solving. In fact, most of them have never seen a proof and don&#8217;t even understand the importance of mathematical reasoning. With poor reasoning abilities, they become intellectually less curious, accepting things as they are presented, and they have tremendous difficulties creating new algorithms, or convincing someone that their own algorithms are correct.</p>
<p>Moreover, once they are in the university, one of two things happens:</p>
<ol>
<li>they are not taught explicitly how to solve problems or how to derive algorithms from their formal specifications (this is the most common case);</li>
<li>or they are taught the above skills but their poor background doesn&#8217;t allow them to fully appreciate these subjects.</li>
</ol>
<p><span id="more-27"></span></p>
<h2>What can be done?</h2>
<p>Dewar and Schonberg suggest some skills to be taught:</p>
<blockquote><p>&#8220;Students need to be exposed to the tools to construct large-scale reliable programs, as we discussed at the start of this article. Topics of relevance are studying formal specification methods and formal proof methodologies, as well as gaining an understanding of how high-reliability code is certified in the real world. When you step into a plane, you are putting your life in the hands of software which had better be totally reliable. As a computer scientist, you should have some knowledge of how this level of reliability is achieved. In this day and age, the fear of terrorist cyber attacks have given a new urgency to the building of software that is not only bug free, but is also immune from malicious attack. Such high-security software relies even more extensively on formal methodologies, and our students need to be prepared for this new world.&#8221;</p></blockquote>
<p>I agree with every word in this paragraph! We really need professionals capable of creating certified software that works under all circumstances.</p>
<h3>Joel Spolsky&#8217;s proposal</h3>
<p>Joel Spolky&#8217;s wrote <a href="http://www.joelonsoftware.com/items/2008/01/08.html" title="Undergraduate programming, by Joel Spolsky">an interesting post</a> where he suggests the following:</p>
<blockquote><p>&#8220;I think the solution would be to create a programming-intensive BFA in Software Development&#8211;a Julliard for programmers. Such a program would consist of a practical studio requirement developing significant works of software on teams with very experienced teachers, with a sprinkling of liberal arts classes for balance. It would be a huge magnet to the talented high school kids who love programming, but can&#8217;t get excited about proving theorums.&#8221;</p></blockquote>
<p>Obviously, this is a much more pragmatic and &#8220;hackish&#8221; approach than what Dewar and Schonberg suggest. He also writes:</p>
<blockquote><p>&#8220;When I said BFA, Bachelor of Fine Arts, I meant it: software development is an art, and the existing Computer Science education, where you&#8217;re expected to learn a few things about NP completeness and Quicksort is singularly inadequate to training students how to develop software.&#8221;</p></blockquote>
<p>I think I understand what Joel means when he writes that programming is an art. However, I prefer to call it a science. You see, art is subjective: it exists in your mind. We can both look to the same photograph or painting, and express different interpretations. A program, however, can&#8217;t be seen subjectively. Given a program, we have to be able to say <em>objectively</em> if it is correct (according to some specification) or if it is efficient. It is not a matter of aesthetic principles.</p>
<h3>Other Comments</h3>
<p><a href="http://www.ekinoderm.com/wordpress/?p=27" title="Who did Kill the Software Engineer?">The author of Ekinoderm caught my attention when he wrote the following:</a></p>
<blockquote><p>&#8220;When I got my B.S. (in 2003), I can remember students in my classes constantly grousing about how topics like Scheme or Finite State Automata were “useless” and we should spend our time doing “useful” things like learning how to use VB to make a database app or something. Or, God forbid, writing web apps in PHP.&#8221;</p></blockquote>
<p>Interestingly, I had a very similar experience during my university degree. Many students in my classes didn&#8217;t like functional programming nor formal methods, because, according to them, these skills were useless to get a job; and they also didn&#8217;t like to encode the usual algorithms for trees or graphs in C, because their favourite language (usually Java or VB) already had libraries with the algorithms implemented.</p>
<p>On the contrary, I was interested in functional programming and formal methods. And I think I have benefited quite a lot from studying them. I also learned a lot implementing data structures and algorithms in C. Even so, and after several programming projects at the university, and a job programming Python for a company that offered <a href="http://www.plone.org" title="Plone website">Plone</a> services, I didn&#8217;t feel ready for delivering certified programs (with proofs of correctness included). That was basically the reason why I have embarked in a PhD: I felt the need of programming by construction.</p>
<h2>Final Notes</h2>
<p>In 1975, Edsger Dijkstra wrote a famous note named &#8220;<a href="http://www.cs.utexas.edu/~EWD/transcriptions/EWD04xx/EWD498.html" title="EWD498: How do we tell truths that might hurt?">How do we tell truths that might hurt?</a>&#8220;, where he enumerated some of the unpleasant truths about Computer Science. In the first paragraph, he basically explains why exposing truths (just like Dewar and Schonberg did) gives rise to so much discussion and discordance:</p>
<blockquote><p>&#8220;Sometimes we discover unpleasant truths. Whenever we do so, we are in difficulties: suppressing them is scientifically dishonest, so we must tell them, but telling them, however, will fire back on us. If the truths are sufficiently impalatable, our audience is psychically incapable of accepting them and we will be written off as totally unrealistic, hopelessly idealistic, dangerously revolutionary, foolishly gullible or what have you. (Besides that, telling such truths is a sure way of making oneself unpopular in many circles, and, as such, it is an act that, in general, is not without personal risks. Vide Galileo Galilei&#8230;..)&#8221;</p></blockquote>
<p>What is really amazing is that, 33 years later, some of the most unpleasant truths he enumerated are still true:</p>
<blockquote>
<ul>
<li>Programming is one of the most difficult branches of applied mathematics; the poorer mathematicians had better remain pure mathematicians.</li>
<li>The easiest machine applications are the technical/scientific computations.</li>
<li>The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities.</li>
<li>The problems of business administration in general and data base management in particular are much too difficult for people that think in IBMerese, compounded with sloppy English.</li>
<li>About the use of language: it is impossible to sharpen a pencil with a blunt axe. It is equally vain to try to do it with ten blunt axes instead.</li>
<li>Besides a mathematical inclination, an exceptionally good mastery of one&#8217;s native tongue is the most vital asset of a competent programmer.</li>
<li>Simplicity is prerequisite for reliability.</li>
<li>By claiming that they can contribute to software engineering, the soft scientists make themselves even more ridiculous. (Not less dangerous, alas!) In spite of its name, software engineering requires (cruelly) hard science for its support.</li>
</ul>
</blockquote>
<p>To conclude the post, allow me to add one to the list:</p>
<blockquote><p>As long as programming is considered a soft science and the formal derivation of algorithms is neglected, there will always be people enumerating unpleasant truths about Computer Science.
</p></blockquote>
<p style="margin-top: 10px;"><strong>Related Articles:</strong></p><ul><li><a href="http://www.joaoff.com/2007/01/15/on-programming-and-mathematical-methodology-part-ii/" rel="bookmark">On Programming and Mathematical Methodology -- Part II</a></li><li><a href="http://www.joaoff.com/2006/12/28/on-programming-and-mathematical-methodology/" rel="bookmark">On Programming and Mathematical Methodology</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.joaoff.com/2008/01/22/the-programmers-of-tomorrow/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>How to be more confident about your own programs: an example using Perl</title>
		<link>http://www.joaoff.com/2007/07/26/how-to-be-more-confident-about-your-own-programs-an-example-using-perl/</link>
		<comments>http://www.joaoff.com/2007/07/26/how-to-be-more-confident-about-your-own-programs-an-example-using-perl/#comments</comments>
		<pubDate>Thu, 26 Jul 2007 00:09:16 +0000</pubDate>
		<dc:creator>jff</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.joaoferreira.org/2007/07/26/how-to-be-more-confident-about-your-own-programs-an-example-using-perl/</guid>
		<description><![CDATA[Programming is one of the most difficult branches of applied mathematics; the poorer mathematicians had better remain pure mathematicians. It was Edsger W. Dijkstra, the famous computer scientist, who wrote these words in his note named &#8220;How do we tell truths that might hurt?&#8220;. I am sure that many people didn&#8217;t like to read them, [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><i>Programming is one of the most difficult branches of applied mathematics; the poorer mathematicians had better remain pure mathematicians.</i></p></blockquote>
<p><img align="right" height="144" width="158" src='http://www.joaoferreira.org/wp-content/uploads/2008/01/camel.gif' alt='Perl Camel' /></p>
<p>It was Edsger W. Dijkstra, the famous computer scientist, who wrote these words in his note named &#8220;<a href="http://www.cs.utexas.edu/~EWD/transcriptions/EWD04xx/EWD498.html" title="How do we tell truths that might hurt?">How do we tell truths that might hurt?</a>&#8220;. I am sure that many people didn&#8217;t like to read them, and didn&#8217;t understand what he meant.</p>
<p>Although it is not my intention to discuss his words, I want to present a simple example that demonstrates how mathematics can be used to program better and to make you more confident about your own programs.</p>
<p>This post includes a fair amount of mathematical definitions and concepts, but they should not be difficult to understand. After the mathematical discussion, I present an example using the Perl programming language (it also applies to languages like C or Java).</p>
<h3>The Problem</h3>
<p>The problem I am going to deal with, involves the ceiling and floor functions. If you don&#8217;t remember, the floor of a real number <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> is (usually) written as <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/8a4145c35dfd071975de295cafa007a5.png' title="LaTeX Formula" alt="LaTeX Formula" /> and it is defined as the greatest integer that is at most <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" />. Similarly, the ceiling of a real number <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> is written as <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/1ed839f9b3696f28230c4de8aca1a6ff.png' title="LaTeX Formula" alt="LaTeX Formula" /> and it is defined as the smallest integer that is at least <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" />.</p>
<p>The goal is to implement the ceiling function supposing that our programming language only provides the floor function to round numbers. Formally, given a real <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" />, we want to calculate an expression <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/8cd34385ed61aca950a6b06d09fb50ac.png' title="LaTeX Formula" alt="LaTeX Formula" /> such that:</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/b7a7964256cd15d952f026eca7c543be.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p><span id="more-23"></span></p>
<h3>The ceiling and floor functions</h3>
<p><!--adsense-->As I said before, the floor of a real number <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> is the greatest integer that is at most <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" />. Formally, we capture this notion in the following definition:</p>
<p class="display">For all real <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" />, <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/8a4145c35dfd071975de295cafa007a5.png' title="LaTeX Formula" alt="LaTeX Formula" /> is an integer such that, for all integers <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" />,</p>
<p style="margin-left: 4em" class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/ce6a99028a514367daa096ae5bc9f220.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>This very compact and elegant definition allows us to derive some properties very quickly. If, for instance, we instantiate <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" /> to be <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/8a4145c35dfd071975de295cafa007a5.png' title="LaTeX Formula" alt="LaTeX Formula" />,  we get:</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/99c8a8d15f37084344f355970a58fd13.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>Since the left side of the equivalence is true, the right side is also true. We thus have our first property:</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/40890b129d3b0f9b69c24d503121677e.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>Please note that since <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" /> is an integer by definition, we can only replace it by integer values. That is why we can replace it by <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/8a4145c35dfd071975de295cafa007a5.png' title="LaTeX Formula" alt="LaTeX Formula" />, which is an integer, but we can&#8217;t  instantiate it to be <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> because <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> is a real number. However, we can replace <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> by <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" />, since all integers are also reals. Replacing <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> by <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" />, we get:</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/0086b79881d2d48babd28d0c903172fd.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>which simplifies to</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/2b3dcf7a7f703ac22083d0a2d92ec855.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>Now, instantianting <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> to <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" /> in (0), we can also conclude that</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/89008ce3314c6e4171e5b2887a363091.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>Finally, from (1) and (2), we conclude that</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/be2c5c4df2dfcaff6721e110d7b5c764.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>We already knew this property: the floor of an integer number <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" /> is <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" />.</p>
<p>We can do a very similar analysis for the ceiling function. We&#8217;ve seen that the ceiling of a real <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> is the smallest integer that is at least <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" />. So, formally, we have:</p>
<p class="display">For all real <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" />, <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/1ed839f9b3696f28230c4de8aca1a6ff.png' title="LaTeX Formula" alt="LaTeX Formula" /> is an integer such that, for all integers <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" />,</p>
<p style="margin-left: 4em" class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/b1ae4112566715a1d5adefa65549059a.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>Note that we can apply the contrapositive rule (that&#8217;s when we negate both sides) to this definition. Doing that, we get the equivalent definition:</p>
<p class="display">For all real <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" />, <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/1ed839f9b3696f28230c4de8aca1a6ff.png' title="LaTeX Formula" alt="LaTeX Formula" /> is an integer such that, for all integers <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" />,</p>
<p style="margin-left: 4em" class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/928b099836a37475a478c960356be1b2.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>I leave as an exercise to prove the dual properties of the ones we&#8217;ve seen for the floor function.</p>
<h3>Indirect equality and the solution to the problem</h3>
<p>Ok, so now that we have seen the formal definitions of the ceiling and floor functions, we can proceed. Note that these definitions have the aproppriate shape to use the proof technique named Indirect Equality. Briefly, the rule of Indirect Equality claims that two numbers <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> and <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/deceeaf6940a8c7a5a02373728002b0f.png' title="LaTeX Formula" alt="LaTeX Formula" /> are equal if, for all numbers <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" /> of the same type as <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> and <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/deceeaf6940a8c7a5a02373728002b0f.png' title="LaTeX Formula" alt="LaTeX Formula" />, the following holds:</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/9cd7893364c6bb18e78bccf53722701f.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>Recall that we want to calculate an expression <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/8cd34385ed61aca950a6b06d09fb50ac.png' title="LaTeX Formula" alt="LaTeX Formula" />, such that, for a given real <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" />:</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/82a1b26717c1fdf93c7b4b0bee6a77f9.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>To use indirect equality, we will prove, for all <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/55a049b8f161ae7cfeb0197d75aff967.png' title="LaTeX Formula" alt="LaTeX Formula" />, the following:</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/51cfde2448b557cd96f9be01204de7c8.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>The detailed proof is as follows:</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/857755b69ca4ecce959008ccc2a2e016.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>We have thus proved that:</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/68c6feed41b7e3a9a237af4b8f8e24c6.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>Note that we still have a problem: our floor expression still involves an application of the ceiling function! However, since</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/98bc519358d8112a87e969c47ebf2188.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>we can easily determine the value of <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/68db9f2504c3a663c3c7c588baf8d8e9.png' title="LaTeX Formula" alt="LaTeX Formula" /> by a simple case analysis. If <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/332cc365a4987aacce0ead01b8bdcc0b.png' title="LaTeX Formula" alt="LaTeX Formula" /> is an integer, then <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/4806c2dd0af808fd41747aa6abbe3cab.png' title="LaTeX Formula" alt="LaTeX Formula" /> , else <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/7f0cfa556c9f48a1935b17a7f7b913de.png' title="LaTeX Formula" alt="LaTeX Formula" />. Hence,</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/5b6fcc7ea9fffc5016a06e1ac2c81d73.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<h3>An example in Perl</h3>
<p>An example of a language that does not provide a native function ceiling is Perl. The only native function related with rounding numbers (as far as I know) is the function <em>int</em>. This function is a mixture of the floor and ceiling function: for positive numbers it behaves as the floor function and for negative numbers it behaves as the ceiling function. This means that to solve the problem in Perl, we just have to worry about positive arguments!</p>
<p>Therefore, from our previous analysis, we can write a <strong>correct</strong> implementation of the ceiling function as follows:</p>
<pre>
sub correct_ceil {
  my $r = shift;
  my $frac = $r - int($r);
  if($frac&lt;=0) {
    return int($r);
  } else {
    return 1+int($r);
  }
}
</pre>
<p>And that&#8217;s it! We have a real proof that this function is correct! Although the problem is simple, it is very common to make mistakes. As an example, I found the following wrong ceiling implementation in a <a href="http://codingforums.com/archive/index.php?t-13775.html" title="Error in solution presented at programming forum">programming forum</a>:</p>
<p><code><br />
# ceil() funtion to use instead of loading in<br />
# the POSIX module each time<br />
{<br />
local ($amount) = @_;<br />
$floor = int($amount);<br />
$ceil = int($amount + 0.5);<br />
if ($ceil == $floor)<br />
{<br />
$ceil++;<br />
}<br />
return $ceil;<br />
}<br />
</code></p>
<p>Clearly, this function does not work for integer arguments! I just hope it is not being used anywhere <img src='http://www.joaoff.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Note</strong>: in Perl you can import the module <em>Posix</em> and use the function ceil, but our goal was to write the ceiling function knowing that the floor function is defined.</p>
<h3>Conclusions</h3>
<p>The main goal of this post was to show how mathematical arguments can be used to give us confidence in our own programs. I&#8217;m aware that, although the ceiling and floor functions are widely used (in financial applications, for instance), this example could be presented in a different way. The most obvious drawback is that the main proof is quite long; it could be shortened, but I would have to omit some intermediate steps.</p>
<p>On the positive side, once we adopt the definitions given, most part of the proofs involving the ceiling and floor functions become similar, and thus systematic. This allows us to reuse the proof techniques over and over again.</p>
<p>I hope you have enjoyed the post and I&#8217;d be glad to get some comments!</p>
<h3>A much better solution by Rob Mayoff</h3>
<p><a href="http://dqd.com/~mayoff/" title="Rob Mayoff">Rob Mayoff</a> proposed a much better solution (see comments) to this problem! A proof of his solution can be:</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/c65b9e447fd1b336badacf20c58d7ac6.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>Therefore,</p>
<p class="display"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/3648520a3f5833d1c456f2204c4a8678.png' title="LaTeX Formula" alt="LaTeX Formula" />.</p>
<p>Thanks Rob!</p>
<p style="margin-top: 10px;"><strong>Related Articles:</strong></p><ul><li><a href="http://www.joaoff.com/2007/01/15/on-programming-and-mathematical-methodology-part-ii/" rel="bookmark">On Programming and Mathematical Methodology -- Part II</a></li><li><a href="http://www.joaoff.com/2006/12/28/on-programming-and-mathematical-methodology/" rel="bookmark">On Programming and Mathematical Methodology</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.joaoff.com/2007/07/26/how-to-be-more-confident-about-your-own-programs-an-example-using-perl/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Swapping the values of two variables</title>
		<link>http://www.joaoff.com/2007/07/11/swapping-the-values-of-two-variables/</link>
		<comments>http://www.joaoff.com/2007/07/11/swapping-the-values-of-two-variables/#comments</comments>
		<pubDate>Wed, 11 Jul 2007 15:08:24 +0000</pubDate>
		<dc:creator>jff</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.joaoferreira.org/2007/07/11/swapping-the-values-of-two-variables/</guid>
		<description><![CDATA[In my previous post I have promised that I would put here some of my technical notes (JFFs). Today I am posting JFF1, which presents a very well-known problem in a non-traditional way. The problem is how to swap the values of two variables without using another temporary one. I start by presenting the properties [...]]]></description>
			<content:encoded><![CDATA[<p><!--adsense#pequeno-->In my previous post I have promised that I would put here some of my technical notes (JFFs). Today I am posting JFF1, which presents a very well-known problem in a non-traditional way. The problem is how to swap the values of two variables without using another temporary one.</p>
<p>I start by presenting the properties involved in the traditional solution and then I generalize it for arbitrary operators. I finish the note with a simple refinement. If you have any nice example instantiation for the last set of assumptions, I would be glad to know it. Other than that, comments or corrections are more than welcome!<br />
The note is available in PDF. Click the following link to get it:</p>
<p><a href="http://www.joaoferreira.org/wp-content/uploads/2007/07/jff1.pdf" id="p21">JFF1 &#8211; Swapping the value of two variables</a></p>
<p style="margin-top: 10px;"><strong>Related Articles:</strong></p><ul><li><a href="http://www.joaoff.com/2007/01/15/on-programming-and-mathematical-methodology-part-ii/" rel="bookmark">On Programming and Mathematical Methodology -- Part II</a></li><li><a href="http://www.joaoff.com/2006/12/28/on-programming-and-mathematical-methodology/" rel="bookmark">On Programming and Mathematical Methodology</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.joaoff.com/2007/07/11/swapping-the-values-of-two-variables/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>I am still alive!</title>
		<link>http://www.joaoff.com/2007/06/15/i-am-still-alive/</link>
		<comments>http://www.joaoff.com/2007/06/15/i-am-still-alive/#comments</comments>
		<pubDate>Fri, 15 Jun 2007 00:00:00 +0000</pubDate>
		<dc:creator>jff</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.joaoferreira.org/2007/06/15/i-am-still-alive/</guid>
		<description><![CDATA[That&#8217;s true: the last post was exactly 5 months ago, but I&#8217;m still alive! A lot of new stuff happened during these 5 months. Two days after writing the last post, I went with my group (Foundations of Programming) to a very nice hotel in Ruddington, where, during two days, each member had to present [...]]]></description>
			<content:encoded><![CDATA[<p>That&#8217;s true: the last post was exactly 5 months ago, but I&#8217;m still alive! A lot of new stuff happened during these 5 months. Two days after writing the last post, I went with my <a href="http://www.cs.nott.ac.uk/Research/fop/" title="Foundations of Programming Group">group</a> (Foundations of Programming) to a very nice hotel in Ruddington, where, during two days, each member had to present something about his/her work. I&#8217;ve talked about a result I&#8217;ve derived related with distributivity through the Greatest Common Divisor. <a href="http://www.cs.nott.ac.uk/~nhn/FoPAD2007/Talks/jff-FoPAD2007.pdf" title="My slides at the FoP Away Day 2007">My slides</a> are available at the <a href="http://www.cs.nott.ac.uk/~nhn/FoPAD2007/" title="FoP Away Day 2007">event&#8217;s webpage</a> and I will put online a note with all the details.</p>
<p>A few days later Alexandra got ill with some strange pain in the abdominal area. The following weeks were really hard, since she had to go to the hospital emergency services. So that you have an idea of how strange the whole thing was, the doctors still don&#8217;t know what the problem is! Now, she has occasional pain, but it seems to be much more controlled.</p>
<p>Anyway, more or less at the same time I started to read a very nice article (a Functional Pearl) written by Jeremy Gibbons, Richard Bird and David Lester named &#8220;<a href="http://web.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/rationals.pdf" title="Enumerating the Rationals article">Enumerating the Rationals</a>&#8220;. The paper presents some algorithms encoded in Haskell to enumerate the positive rational numbers. In particular, it presents algorithms to construct the famous <a href="http://www.cut-the-knot.org/blue/Stern.shtml" title="Stern-Brocot tree">Stern-Brocot</a> and <a href="http://www.math.upenn.edu/~wilf/website/recounting.pdf" title="Recounting the Rationals article, where Calkin-Wilf tree is presented">Calkin-Wilf</a> trees. It also presents a very efficient algorithm to enumerate the rationals in Calkin-Wilf order just by using as current state the previous rational (i.e., two integer numbers). However, the authors claim that &#8220;it is not at all obvious&#8221; how to create a similar efficient algorithm for enumerating the rationals in Stern-Brocot order. Well, after reading it, Roland (my supervisor) and me found a way of doing it. The key idea is that rational numbers are pairs of coprime numbers (numbers which greatest common divisor is 1) and thus, we can use Euclid&#8217;s algorithm as a basis for enumerating these pairs. By using the Extended Euclid&#8217;s algorithm written using matrix multiplication, we were able to derive both Calkin-Wilf and Stern-Brocot enumerations from the same algorithm. We wrote a <a href="http://cs.nott.ac.uk/~jff/papers/RecountingRationalsTwice.pdf" title="Recounting the Rationals: Twice!">paper named &#8220;Recounting the Rationals: Twice!&#8221;</a> that was submitted to the journal <a href="http://www.maa.org/pubs/monthly.html" title="American Mathematical Monthly">American Mathematical Monthly</a>.</p>
<p><span id="more-20"></span>I&#8217;ve also presented the algorithm at the <a href="http://sneezy.cs.nott.ac.uk/fun/may-07" title="Fun in the Afternoon, Cambridge, May 2007">&#8220;Fun in the Afternoon&#8221; event in Cambridge</a>, last May. I think the talk was clear enough and, although it has nothing to do with functional programming, its methodological aspect should be interesting enough to any (functional) programmer. I&#8217;ve had very positive feedback on the talk, which is a good sign. <a href="http://sneezy.cs.nott.ac.uk/fun/may-07/EnumeratingRationalsTwice.pdf" title="Slides of my talk at Fun in the Afternoon, May 2007, Cambridge">The slides of the talk</a> are available in the event&#8217;s webpage.</p>
<p>In April I&#8217;ve attended to the <a href="http://www.cs.nott.ac.uk/~nhn/MGS2007/" title="Midlands Graduate School 2007, Nottingham">Midlands Graduate School</a> (MGS Spring School). It was a very nice event organised by our group (in particular, I have to congratulate <a href="http://www.cs.nott.ac.uk/~nhn" title="Henrik Nilsson">Henrik</a> and <a href="http://www.cs.nott.ac.uk/~gmh/" title="Graham Hutton">Graham</a> for their excellent work!) with very nice talks as well. My favourite courses were Category Theory, Algorithmic Problem Solving and Operational Semantics, but I&#8217;ve also enjoyed the others I&#8217;ve attended. The best thing about this kind of event is the social interaction that you have with other students and researchers. And again, as happened before in other similar events, I always get that cold sensation that I don&#8217;t know nothing about anything! The good news is that this feeling is a constructive one, since I get motivated to learn more!</p>
<p>And since we&#8217;re talking about learning, I&#8217;m currently studying <a href="http://www.cut-the-knot.org/blue/GeneratingFunctions.shtml" title="Generating Functions at cut-the-knot.org">Generating Functions</a>, which are formal power series that encode information about sequences. Generating Functions are a very powerful tool: they can be used to prove identities very easily, to find nice and closed formulas to recurrence relations, to work with datatypes in order to find isomorphisms and, more importantly to my work, they can be used to solve counting problems in a very effective way. I&#8217;m writing a note about what I&#8217;m learning and I&#8217;ll probably post it here.</p>
<p>To finish the news about work, I&#8217;m currently preparing two JFFs (JFFs are my personal technical notes). The first one deals with the very known and simple problem that consists in swapping the value of two variables without using another temporary one. The second shows how to construct a very famous algorithm. I promise that I will post the notes here, as soon as they are available!</p>
<p>Besides work, I guess that the only relevant thing to talk about is that we went on holidays to Halkidiki in Greece in the last week of May. It was excellent and Alexandra and me will post a somewhat detailed description on our <a href="http://www.furiousmind.org" title="Furiousmind: Cinema, Places and Mathematics">joint blog Furiousmind.org</a>.</p>
<p><center><!--adsense#middleblock--></center>Other thing I did that I consider important enough to be mentioned, was to install Linux in my Powerbook G4. I was unhappy with Mac OS for quite a long time and decided to stop complaining and do something about it. The installation process was simple and the wireless connection worked with no further configuration (I was really surprised)! I&#8217;m now a much happier guy: I can still do what I need to do and I&#8217;m not using any proprietary software at all!To conclude the post, let me just tell you my plans for the next month:</p>
<ol>
<li>Post JFF0, JFF1 and JFF2 (these are the notes on distributivity and the greatest common divisor, how to swap values of variables without using another temporary one and the construction of an algorithm that is well-known, respectively);</li>
<li>Prepare a note I have written together with my supervisor about Number Theory to be distributed;</li>
<li>Prepare things to the <a href="http://asimod.in.tum.de/" title="Summer School in Marktoberdorf for Formal Logic Methods for System Security and Correctness">Summer School in Marktoberdorf</a> (Formal Logical Methods for System Security and Correctness). It&#8217;s true! I&#8217;ve been accepted to participate in the school and I already got the plane tickets. This means that I&#8217;ll be in Germany from the 31st July to the 12th August. Then I fly directly to Portugal and I return to Nottingham on the 29th August. So, if you want something from Germany or Portugal, let me know <img src='http://www.joaoff.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>Since I&#8217;m talking about trips, Alexandra and me are going to Amsterdam this next weekend. We&#8217;re really looking forward for it! And if you want something from there, please let us know.</li>
</ol>
<p>Thanks for reading and see you in the next post!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joaoff.com/2007/06/15/i-am-still-alive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On Programming and Mathematical Methodology &#8212; Part II</title>
		<link>http://www.joaoff.com/2007/01/15/on-programming-and-mathematical-methodology-part-ii/</link>
		<comments>http://www.joaoff.com/2007/01/15/on-programming-and-mathematical-methodology-part-ii/#comments</comments>
		<pubDate>Mon, 15 Jan 2007 23:00:33 +0000</pubDate>
		<dc:creator>jff</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Methodology]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[research]]></category>

		<guid isPermaLink="false">http://www.joaoferreira.org/2007/01/15/on-programming-and-mathematical-methodology-part-ii/</guid>
		<description><![CDATA[In the last post I have presented some historical context about programming and mathematical methodology. If you read it, then you should have an idea when and why programmers started to investigate on mathematical methodology. However, I haven&#8217;t mentioned any aspects of mathematical methodology that can help us to improve our programming or mathematical skills. [...]]]></description>
			<content:encoded><![CDATA[<p>In the last post I have presented some <a href="http://www.joaoferreira.org/2006/12/28/on-programming-and-mathematical-methodology/" title="On Programming and Mathematical Methodology">historical context about programming and mathematical methodology</a>. If you read it, then you should have an idea when and why programmers started to investigate on mathematical methodology. However, I haven&#8217;t mentioned any aspects of mathematical methodology that can help us to improve our programming or mathematical skills.</p>
<p>In this post, I&#8217;ll talk about mathematical proofs. And what&#8217;s the relevance of this topic to programmers? Well, computer programs are mathematical formulae, with a precise formal meaning and embodying constructive theorems about the systems they implement (as well-written in &#8220;<a href="http://www.cs.nott.ac.uk/~rcb/MPC/Inaugural.pdf" title="Mathematics and Programming - A Revolution in the Art of Effective Reasoning">Mathematics and Programming &#8211; A Revolution in the Art of Effective Reasoning</a>&#8220;, by Roland Backhouse). The difference between theorems embodied by computer programs and the ones usually studied in mathematics is that they are applied by an unforgiving machine, with the effect that the smallest error can cause a huge damage. This means that computer programmers must create trustworth designs, i.e., the constructive theorems embodied by their programs must be programmed correctly.</p>
<h2>Mathematical Proofs</h2>
<p>Mathematicians job is to do mathematics, i.e., to design and present theorems, arguments, algorithms and in some cases whole theories. However, the traditional mathematical curriculum is more concerned with teaching mathematical facts &#8212; existing theories and concepts &#8212; than with the doing of mathematics. And even when design and presentation get some attention, they are treated separately: design of solutions is viewed as a psychological issue, while presentation is viewed as a matter of personal style (words from <a href="http://www.cs.utexas.edu/users/EWD/ewd10xx/EWD1059.PDF" title="Mathematical Methodology - Preface - by Edsger Dijkstra">this Dijkstra&#8217;s note on Mathematical Methodology</a>).<br />
<span id="more-7"></span></p>
<p>A proof of a theorem should demonstrate, using certain facts (also known as axioms) or previously proved theorems, why it is true. Additionally, a good proof should explain clearly how the facts are combined and it should express the design considerations so that readers can understand it better, explain it to others and prove other theorems in a similar fashion. Now, look at the following conventional proof of <img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/ffaba8eddc9de3fede781e842ffa46ff.png' title="LaTeX Formula" alt="LaTeX Formula" />,  which is similar to the majority of proofs we can  find in math textbooks (the proof is actually from a math textbook, but it was extracted from [GS95]).</p>
<p><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/f383200c25544fe0bb779f5e6cd36e99.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p><!--adsense#middleright-->Proofs are usually like this one: written using natural language, which, by nature, admits ambiguity. These proofs, also called informal proofs, place a large burden on the reader since it is difficult to see precisely how facts interact. In order to achieve the crispness we aspire to, we must deviate from informal proofs. That is why most of our mathematical reasoning is carried out in what is known as calculational method. This method reduces proof obligations to targets to be reached by formula manipulation. Although there are some criticisms and objections to the method, the experience shows that arguments become more clear and systematic, since we are (usually) restricting ourselves to a very modest repertoire and to simple syntactic manipulations. Besides, with the development of the method, some heuristics have been created, allowing the writer to prefer certain decisions (almost according with the principle &#8220;There is really only one thing one can do.&#8221;).</p>
<h2>Proof Format</h2>
<p>Note that calculational arguments usually start with a boolean expression that, by value-preserving transformations, is massaged according to our needs (we can evaluate it to true or false, or we can transform it into an equivalent boolean expression). So, if we want to evaluate a boolean expression A to true and it takes 3 steps, then, for general expressions B and C, we would have:</p>
<p>the first step would establish A   is B ;<br />
the second step would establish B   is C;<br />
the third step would establish C   is true .</p>
<p>This means that we would have the intermediate expressions B and C repeated twice. In general, B and C can be long expressions, so we need a proof format that allows us to omit intermediate expressions. We use Wim Feijen&#8217;s proof format (described in detail in <a href="http://www.cs.utexas.edu/users/EWD/ewd09xx/EWD999.PDF" title="EWD999: Our Proof Format">EWD999: Our Proof Format</a>, <a href="http://portal.acm.org/citation.cfm?id=77545" title="Predicate Calculus and Program Semantics">Predicate Calculus and Program Semantics</a> and in Chapter 3 of <a href="http://eu.wiley.com/WileyCDA/WileyTitle/productCd-0470848820.html" title="Program Construction: Calculating Implementations from Specifications (by Roland Backhouse)">Program Construction: Calculating Implementations from Specifications</a>), which for this small example would render:</p>
<p align="center"><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/b36e3b257126351c5fa40ea8f7cd64db.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>The advantages of this format are more than just brevity. It allows us to conclude immediately that A   is true without reading the intermediate expressions and we also can use any transitive relation between the steps. Also, the use of a systematic proof format allows us to compare two different proofs of the same theorem more effectively. Now, look how the proof presented above in an informal style can be rewritten in a calculational style:</p>
<p><img class="lateximg" src='http://www.joaoferreira.org/wp-content/plugins/wp-latexrenderer/pictures/dff587d3d55a60ef49bfbfb227f8a3a8.png' title="LaTeX Formula" alt="LaTeX Formula" /></p>
<p>In this second version, the proof format makes all the ingredients much more evident; every step is well identified and we can see it uses a common strategy: it eliminates the union and disjunction operators using their definition, performs some manipulation and reintroduces the operators. Also, this second argument eliminates the &#8220;if&#8221; and &#8220;only if&#8221; argument.</p>
<h2>Final notes</h2>
<p>To conclude I&#8217;d just like to quote a paragraph from Roland&#8217;s Backhouse article I&#8217;ve mentioned above:</p>
<blockquote><p><em>&#8220;The vast complexity of computer software makes its unreliability understandable, but not excusable. The implementation of reliable computer systems, carrying a guarantee of fitness-for-purpose, imposes major intellectual challenges that can only be met by a science of computing whose hallmark is the avoidance of error.&#8221;</em></p></blockquote>
<p style="margin-top: 10px;"><strong>Related Articles:</strong></p><ul><li><a href="http://www.joaoff.com/2006/12/28/on-programming-and-mathematical-methodology/" rel="bookmark">On Programming and Mathematical Methodology</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.joaoff.com/2007/01/15/on-programming-and-mathematical-methodology-part-ii/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>On Programming and Mathematical Methodology</title>
		<link>http://www.joaoff.com/2006/12/28/on-programming-and-mathematical-methodology/</link>
		<comments>http://www.joaoff.com/2006/12/28/on-programming-and-mathematical-methodology/#comments</comments>
		<pubDate>Thu, 28 Dec 2006 23:00:16 +0000</pubDate>
		<dc:creator>jff</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Methodology]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[research]]></category>

		<guid isPermaLink="false">http://www.joaoferreira.org/2006/12/26/on-programming-and-mathematical-methodology/</guid>
		<description><![CDATA[First of all, welcome to my new blog. Being this my first post, I will present myself, give you some background on what I am doing and explain what are my intentions about this blog. My name is João Fernando Ferreira and I am a research student at the Foundations of Programming research group at [...]]]></description>
			<content:encoded><![CDATA[<p>First of all, welcome to my new blog. Being this my first post, I will present myself, give you some background on what I am doing and explain what are my intentions about this blog. My name is João Fernando Ferreira and I am a research student at the <a href="http://www.cs.nott.ac.uk/Research/fop/" title="Foundations of Programming research group">Foundations of Programming</a> research group at the <a href="http://www.nottingham.ac.uk" title="University of Nottingham">University of Nottingham</a> (visit the <a href="http://www.joaoferreira.org/about" title="About João Ferreira">About</a> page to find out more).</p>
<p>My research is on algorithmic problem solving and its main goal is to develop calculational problem-solving techniques resulting in educational material supporting the use of a calculational     approach to algorithmic problem solving. The focus of the project will be on the dynamics of     problem solving &#8211; the processes of mathematical modelling and effective calculation in the     formulation of concise and precise algorithmic methods.</p>
<p>As any other researcher, I spend most of my time reading and writing. That is one of the reasons I have created this blog: to organise my written notes. Using a blog system brings me some advantages like:</p>
<ul>
<li>I can tag my notes and use the system search capabilities to find them;</li>
<li>I can access and change them from any place in the world, as long as I have Internet access;</li>
<li>I can add new content from any place in the world, as well;</li>
<li>I can share my notes and get feedback from my colleagues, supervisors, friends or anyone who is just interested in the same topics.</li>
</ul>
<p>Other important reason to share my notes and thoughts is that I think they may be of interest for programmers. Since this is my first post, and (probably) most of you don&#8217;t really understand what I am doing, I will start by presenting some historical facts (mixed with personal opinions) and motivations. It is my hope that these facts will help you understand the relation and importance of mathematical methodology to programming. Please note that to read the entire post, you may need to click the &#8220;<em>Read more</em>&#8221; link.</p>
<h4>Historical context</h4>
<p>In the 1960s, programmers started recognising that there were serious problems in the programming  field and that it was necessary to prove the correctness of programs. At the time, software engineering was facing a software crisis and programming was not very well understood. Many software projects ran over budget and schedule and some of them even caused property damage and loss of life (see the <a href="http://catless.ncl.ac.uk/Risks" title="RISKS-FORUM Digest">RISKS-FORUM Digest</a> for some examples).</p>
<p>To solve these problems, computer scientists focused on programming methodology and on ways to build programs in a systematic way. A common consensus was that programs should be proved correct, and in the late 1960s, some important articles had an important impact on the  field. In 1968, Edsger W. Dijkstra published an <a href="http://www.acm.org/classics/oct95/" title="Go To Statement Considered Harmful">article</a> on the harmfulness of the Go To statement, where he claims that its use makes it impossible to determine the progress of a program. Also, one year later, Tony Hoare published a seminal <a href="http://portal.acm.org/citation.cfm?id=363259" title="An Axiomatic Basis for Computer Programming">article</a> where he introduces the Hoare triples and an axiomatic approach to language definition.<br />
<span id="more-1"></span></p>
<p><!--adsense#pubmiddle-->Although Hoare&#8217;s theory had a great impact, it was quite difficult to use it to prove existing programs correct, since one was forced to  find an invariant for each loop. Programmers started studying alternatives, and the most plausible one was to develop the program together with its proof. Some years later, in 1975, Edsger W. Dijkstra published a <a href="http://doi.acm.org/10.1145/360933.360975" title="Guarded commands, nondeterminacy and formal derivation of programs">paper</a> where he introduced weakest preconditions. One year later he published the book &#8220;<a href="http://www.amazon.co.uk/Discipline-Programming-Automatic-Computation/dp/013215871X/sr=8-9/qid=1167330453/ref=sr_1_9/026-5905523-7560424?ie=UTF8&amp;s=books" title="A Discipline of Programming"><em>A Discipline of Programming</em></a>&#8221; and showed how to use weakest preconditions as a &#8220;calculus for the derivation of programs&#8221;. Programmers were now able to build programs in a more reliable and systematic way, and the <em>art</em> of programming became more and more a <em>discipline</em> of programming.</p>
<p>From here, Dijkstra and others, dedicated themselves to the &#8220;mathematization&#8221; of programming and they learned to derive programs from their specifications:  first simple ones, then more complicated programs. As the programs were becoming more and more complicated, the solutions were becoming less and less simple and beautiful. The reason was the &#8220;standard mathematical reasoning patterns&#8221; (see this <a href="http://www.mathmeth.com/wf/files/wf0xx/wf088.pdf" title="On Programming and Mathematical Reasoning">note</a>, page 9), which were not suitable for the task at hands. The conclusion was that computer scientists would have to learn how to construct proofs more effectively, in order to solve more ambitious problems. This was the beginning of a new period: computer scientists started to investigate ways of streamlining the mathematical argument. Mathematics and mathematicians were now faced with this new side of Computer Science.</p>
<p>During this new period, several problems were identified with traditional mathematics. One of the  first problems was that mathematicians hardly manipulate their formulas: they interpret them; and one of the reasons is that the notation they use is not adequate for manipulation. This and other conclusions were also presented in the <a href="http://www.springer.com/uk/home/generic/search/results?SGWID=3-40109-22-1353672-0" title="On the Shape of Mathematical Arguments">PhD thesis</a> of Netty van Gasteren &#8220;<em>On the shape of mathematical arguments</em>&#8220;, where she presents a very interesting study about proofs (proofs of correctness of programs included).</p>
<p>My project is basically a continuation of Netty&#8217;s study, but while she did a breadth study, I am concerned specifically with algorithmic problems: construction of new algorithms and usage of algorithmic techniques to simplify mathematical invention.</p>
<h4>Final notes</h4>
<p>Algorithms have been studied and developed since the beginning of civilisation, but, over the last decades, the unprecedented scale of programming problems and the consequent demands on precision and concision have made computer scientists hone their algorithmic problem-solving skills to a very fine degree.</p>
<p>Even so, and although much of mathematics is algorithmic in nature, the skills needed to formulate and solve algorithmic problems do not form an integral part of mathematics education; also, the teaching of computer-related topics at pre-university level focuses on enabling the student to be an effective user of information technology, rather than equip them with the skills to develop new applications or to solve new problems.</p>
<p>I believe that making clear the algorithmic nature of problems can help students understand what&#8217;s going on, and more importantly, they will be able to solve new problems more effectively. And since tomorrow&#8217;s programmers are today&#8217;s students, it is important that we learn how to streamline the mathematical argument, how to demystify the process of mathematical invention and how to turn the design of crisp arguments into a <strong>teachable</strong> discipline.</p>
<p>Thank you very much for reading my first post. It is already too long, so I will leave concrete examples and more detailed explanations &#8212; including derivation of programs from their specification &#8212; for later. I intend to write similar articles in the future, so if you like this one, consider subscribing this blog.</p>
<p style="margin-top: 10px;"><strong>Related Articles:</strong></p><ul><li><a href="http://www.joaoff.com/2007/01/15/on-programming-and-mathematical-methodology-part-ii/" rel="bookmark">On Programming and Mathematical Methodology -- Part II</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.joaoff.com/2006/12/28/on-programming-and-mathematical-methodology/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
