<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Nerdland</title>
	
	<link>http://nerdland.net</link>
	<description>Computer Science, Programming, and All Points Beyond</description>
	<lastBuildDate>Sun, 11 Jul 2010 19:23:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/nerdland" /><feedburner:info uri="nerdland" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Nerdland is Moving</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/05K85FXhwKo/</link>
		<comments>http://nerdland.net/2010/07/nerdland-is-moving/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 22:15:54 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Site News]]></category>
		<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[Hosting]]></category>
		<category><![CDATA[User Accounts]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=511</guid>
		<description><![CDATA[Update 11 July 2010: The move was a lot more painless than I had anticipated, and is already done. If you&#8217;re reading this, then your DNS has updated and you are accessing the new server. Hooray! See below about user accounts if you missed the original announcement. Sometime later this month, Nerdland will be moving. [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Update 11 July 2010:</strong> The move was a lot more painless than I had anticipated, and is already done. If you&#8217;re reading this, then your DNS has updated and you are accessing the new server. Hooray! See below about user accounts if you missed the original announcement.</p></blockquote>
<p>Sometime later this month, Nerdland will be moving. The content and URL of the site won&#8217;t change; only the hosting provider will. The new host will be a <a href="http://www.rackspacecloud.com/cloud_hosting_products/servers">Rackspace Cloud Server</a>. This probably doesn&#8217;t affect you directly unless you are one of the people to whom I gave a Nerdland &#8220;<a href="http://users.nerdland.net/">user account</a>&#8221; with web hosting space and e-mail over the past eleven years. If you are one of those people, please read the next few paragraphs. </p>
<p>As part of the move, I&#8217;m going to take the opportunity to clean out a lot of cruft that&#8217;s been building up on the Nerdland server over the past half-decade since the last hosting change. Most of the user accounts that I provided for friends and relatives aren&#8217;t being used anymore, and aren&#8217;t linked from anywhere on the Internet, so there&#8217;s no reason to migrate them. Rest assured, <em>nothing will be permanently deleted</em>. I&#8217;m an incorrigible data pack-rat, so I&#8217;m of course going to archive and back-up everything, including what I don&#8217;t move to the new server. If you had data stored on Nerdland, you can always contact me in the future, and I will gladly send you a copy of your old files and unretrieved e-mail, or restore your data and account to the server.</p>
<p><span id="more-511"></span></p>
<p><strong>If you do still actively use your Nerdland web hosting space and/or e-mail address, please <a href="mailto:tyler@nerdland.net">contact me</a> as soon as possible and tell me that</strong>, and I will ensure that all of your data is moved and set you up with an account on the new server. Otherwise, I will only be migrating data which is linked from elsewhere on the Internet (according to Google&#8217;s index) or has been accessed recently (according to the server logs). </p>
<p>The primary reason for this move is that there is a project that I&#8217;m currently working on (that I will post about soon) for which I am going to need a server, and the shared hosting service that Nerdland is currently running on is not up to the task. In particular, it requires custom software (which isn&#8217;t possible on a shared host to which you don&#8217;t have SSH access, let alone root access), and it requires faster response times than this frankly oversold server is capable of. But since my project&#8217;s server is not going to require all of the resources available to me on a Rackspace Cloud Server <a href="http://en.wikipedia.org/wiki/Virtual_private_server">VPS instance</a>, I figured I may as well save a bit of money by hosting Nerdland on the instance as well. Hopefully, as a result, Nerdland itself will also load and respond faster.</p>
<p>To be honest, I probably would have done something like this a long time ago, if only just to play with it, had I been aware of the <a href="http://www.rackspacecloud.com">Rackspace Cloud</a> before last month. Until I began researching hosting providers for this service that I&#8217;m writing, the only dynamic VPS provider that I was cognizant of was <a href="http://aws.amazon.com/ec2/">Amazon EC2</a>. Not that there&#8217;s anything wrong with EC2, but <a href="http://aws.amazon.com/ec2/instance-types/">it&#8217;s minimum instance size</a> is 1.7 GiB of memory and 160 GB of disk space, which is far more than I would require for experimentation or a personal project, and it comes with <a href="http://aws.amazon.com/ec2/pricing/">a price to match</a>. Rackspace&#8217;s VPS instances can start as small as 256 MiB of memory and 10 GB of space, at <a href="http://www.rackspacecloud.com/cloud_hosting_products/servers/pricing">very reasonable prices</a>, which will allow me to pay for more as I need it without having to lay out a fortune just to start.</p>
<p>Aside from having the resources I need to host my project and my website, it will be a lot of fun to have root access to an always-up server with a fixed, public IP once again. It already reminds me of the bad old days of the late 1990s and early 2000s, when I hosted Nerdland from Osric, a spunky little computer under my desk connected to the <a href="http://en.wikipedia.org/wiki/@Home_Network">@Home Network</a> (I still remember the IP: 24.3.98.206). Before @Home collapsed and I lost my static IP, I experimented with installing and running just about every sort of server under the sun on that dusty old box. </p>
<p>Dynamic DNS just wasn&#8217;t the same, and these days, electricity costs alone make it a bit silly to run a separate computer on a consumer-grade broadband connection rather than just paying for a real server. So finally, with the advent of affordable cloud-based VPS hosting, I can regain all the benefits of a real server once again. And now that I&#8217;m a more educated and accomplished programmer, I can instead experiment with writing my own software to run on my shiny new (virtual) box.</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/05K85FXhwKo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2010/07/nerdland-is-moving/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://nerdland.net/2010/07/nerdland-is-moving/</feedburner:origLink></item>
		<item>
		<title>“Code”: Mass Noun versus Count Noun</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/ol1A-WiDfDI/</link>
		<comments>http://nerdland.net/2010/06/code-mass-noun-versus-count-noun/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 17:45:37 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Human Language]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=506</guid>
		<description><![CDATA[In English, there are generally two ways of describing quantities: mass nouns, and count nouns. Count nouns are used when the quantity in question is a set of discrete items. &#8220;There are twelve bananas.&#8221; Here, &#8216;bananas&#8217; functions as a count noun, which is appropriate because you can clearly tell where one banana ends and the [...]]]></description>
			<content:encoded><![CDATA[<p>In English, there are generally two ways of describing quantities: <a href="http://en.wikipedia.org/wiki/Mass_noun">mass nouns</a>, and <a href="http://en.wikipedia.org/wiki/Count_noun">count nouns</a>. Count nouns are used when the quantity in question is a set of discrete items. &#8220;There are twelve bananas.&#8221; Here, &#8216;bananas&#8217; functions as a count noun, which is appropriate because you can clearly tell where one banana ends and the next begins. In contrast, mass nouns are used when the quantity is continuous and not divisible into countable units. &#8220;There are twelve peanut butters,&#8221; for example, does not make sense. What constitutes a single &#8220;peanut butter&#8221;? Unlike with a count noun, a unit is required to refer to a specific quantity of a mass noun, even if the unit is implicit, such interpreting the previous example to mean &#8220;There are twelve <em>jars of</em> peanut butter.&#8221;</p>
<p>This brings me to the word &#8220;code&#8221;. </p>
<p><span id="more-506"></span></p>
<p>Used in its computer programming sense, &#8220;code&#8221; is always a mass noun in English: &#8220;I stayed up writing code until midnight.&#8221; &#8220;I had to read through four thousand lines of code to find the bug.&#8221; &#8220;Dammit, what is wrong with all of this code now?&#8221; Code is thought of as an continuous mass. Code is what you pour into the engine of your computer to make it run. Aside from the very first few instructions that run as part of your system&#8217;s bootstrap, no code stands alone, and you can&#8217;t divide code without encompassing it in some larger concept. You can have &#8220;two programs&#8221; but you can&#8217;t have &#8220;two codes.&#8221;</p>
<p>This sense of the word &#8220;code&#8221; likely derives from the older sense in which &#8220;code&#8221; is a synonym for &#8220;protocol&#8221;, or &#8220;structured communications mechanism&#8221;, e.g. &#8220;<a href="http://en.wikipedia.org/wiki/Hamming_code">Hamming code</a>&#8220;, or &#8220;<a href="http://en.wikipedia.org/wiki/Morse_code">Morse code</a>&#8220;. Code is the protocol which the programmer uses to communicate with the computer. It has a usually rigid syntax, and its purpose is to give the programmer a mechanism for specifying what he or she wants done in a manner that a dumb box of circuits can make sense of. Code is a series of very clear, unambiguously meaningful instructions.</p>
<p>But the word &#8220;code&#8221; has other definitions. One is a synonym for &#8220;laws&#8221; or &#8220;rules&#8221;, but the other, <a href="http://www.merriam-webster.com/dictionary/code">as listed in Merriam-Webster online</a>, is</p>
<blockquote><p>
3 b: a system of symbols (as letters or numbers) used to represent assigned and often secret meanings
</p></blockquote>
<p>Of all the meanings of the word &#8220;code&#8221;, this is the only one that functions as a count noun: &#8220;What is the code to open this lock?&#8221; &#8220;During World War II, British spy agencies cracked dozens of German codes.&#8221; &#8220;The teenage girls devised four different codes to use in passing notes.&#8221;</p>
<p>This is why it always disturbs me just a little when I read programming questions or discussion in which someone uses &#8220;code&#8221; as a count noun: &#8220;Please send me the codes.&#8221; &#8220;What are the codes for displaying a context menu?&#8221; &#8220;What is wrong with these C++ codes?&#8221; I&#8217;m sure many of these are simply non-native English speakers who are doing their best to work in this baroque tongue of ours, but I can&#8217;t help but shake the feeling that some of these people are native English speakers who are conflating the two senses of the word &#8220;code&#8221;.</p>
<p>The reason that this disturbs me is not because it&#8217;s some grammar peeve of mine. Rather, I suspect that native or fluent English speakers who use &#8220;code&#8221; as a count noun are subtly revealing their perception of code as something secret and unknowable. For example, the aspiring programmer who asks &#8220;What are the codes for displaying a context menu?&#8221; might be thinking that there is some particular set of magical incantations hidden somewhere in a dusty, forgotten tome which need to be recited precisely in order to conjure up a context menu. Similarly, one who requests that I, &#8220;please send [him] the codes,&#8221; is more than likely not interested in understanding how to accomplish the task he is asking about, but is instead interested in obtaining what he sees as an opaque sequence of characters that, when compiled, will make the computer do what he wants.</p>
<p>In short, the use of &#8220;code&#8221; as a count noun, rather than a mass noun, and when not explainable by imperfect mastery of the English language, is a potential red flag for impending <a href="http://en.wikipedia.org/wiki/Cargo_cult_programming">cargo cult programming</a> (which I consider one of the biggest issues in Computer Science education, and <a href="http://nerdland.net/2009/06/ill-tell-you-when-youre-older/">have complained about before</a>).</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/ol1A-WiDfDI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2010/06/code-mass-noun-versus-count-noun/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://nerdland.net/2010/06/code-mass-noun-versus-count-noun/</feedburner:origLink></item>
		<item>
		<title>Understanding the Five Aspects of Cryptographic Security</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/3yAumSKazKY/</link>
		<comments>http://nerdland.net/2010/03/understanding-the-five-aspects-of-cryptographic-security/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 18:00:28 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[Cryptography]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=476</guid>
		<description><![CDATA[Encryption on the Internet has come a long, long way from the oft-ignored little yellow key in the lower left corner of your Netscape Navigator status bar. Today, cryptography is a vital part of all of our Internet lives, whether we realize it or not. Now, if you&#8217;re reading this article on Nerdland, chances are [...]]]></description>
			<content:encoded><![CDATA[<p>Encryption on the Internet has come a long, long way from the oft-ignored little yellow key in the lower left corner of your Netscape Navigator status bar. Today, cryptography is a vital part of all of our Internet lives, whether we realize it or not. Now, if you&#8217;re reading this article on Nerdland, chances are that you&#8217;re well aware of that, and I don&#8217;t need to explain why you need to be sure your online banking is done over an HTTPS connection, and why connecting your laptop to an open, unsecured wireless network is usually a bad idea. </p>
<p>But the little stuff can trip you up just as easily, and if you don&#8217;t have a solid understanding of the different facets of cryptography, you may well think that a system meets your security requirements when it does not. After all, modern cryptography is just mathematics. There&#8217;s no inherent application for it. Security isn&#8217;t a tangible property either; it&#8217;s an umbrella term for a whole class of goals. Rather, privacy, authentication, identification, trust, and verification &mdash; mechanisms of <em>applied</em> cryptography &mdash; are what <em>provide</em> the most commonly desired types of security. Understanding what these terms really mean, how they are implemented, and how they are different is essential to a true understanding of how encryption works to assure your security on the Internet, and even within a single computer.</p>
<p><span id="more-476"></span></p>
<p>This article assumes you are familiar with the fundamentals of cryptography: that you know what constitutes encryption, that you know what a key is, and that you know the basic difference between <a href="http://en.wikipedia.org/wiki/Symmetric-key_algorithm">symmetric key cryptography</a> and <a href="http://en.wikipedia.org/wiki/Public-key_cryptography">public key cryptography</a>. I am concerned with describing and clearing up some misconceptions about the practical applications of cryptography to modern computing.</p>
<h3>1. Privacy</h3>
<p>Privacy (or &#8220;secrecy&#8221;) is the cornerstone of applied cryptography. A commonly desired form of security is making data readable only by certain intended recipients. Whether symmetric or public key cryptography is in use, a person (or machine) proves that they are an intended recipient by possessing the key that can be used to decrypt the message. In the case of simply achieving privacy, it really doesn&#8217;t matter whether symmetric or public key encryption is used; public key encryption is very slow, so in practice, it&#8217;s only used to encrypt a symmetric key that is used to encrypt the rest of the data.</p>
<p>Privacy is commonly desired when sensitive data is being transmitted. In the case of web browsing, this is one of the purposes of the <a href="http://en.wikipedia.org/wiki/HTTP_Secure">Secure HTTP (HTTPS)</a> protocol. When communicating with, for example, your bank&#8217;s website, it is important that the information being transacted is private. It is highly undesirable for any other person, even a professional network administrator at your ISP, who happens to control a computer on the Internet through which the data between you and your bank passes, to be able to look at your account numbers and balances. </p>
<p>Similarly, if you store sensitive corporate information or highly personal documents on a laptop, you would want to make sure that these documents remain private if the laptop were ever lost or stolen. For this, you would encrypt the files (or better yet the entire hard drive) and either keep the decryption key outside of the laptop, or keep it protected with a strong passphrase. In the latter case, the passphrase itself is the key to a cryptographic algorithm will provide the unencrypted version of the decryption key for your files or hard drive, and the passphrase is ideally stored only in your head.</p>
<p>This is privacy: <strong>no third parties can read your data</strong>. No more, and no less. A common problem is that users, even technically savvy users, often make the false assumption that privacy implies authentication and verification. While the ability to create privacy is a prerequisite for authentication and verification, and they are often used in conjunction, it is not the case that obtaining privacy implies that the other two types of security have also been obtained.</p>
<h3>2. Authentication</h3>
<p>Authentication is the act of proving who you are, or challenging someone else to prove who they are. The underlying technology for modern authentication schemes is public key cryptography. I said earlier that I was assuming familiarity with public key cryptography, but let me reiterate the most salient aspect of it for the purposes of authentication: In public key cryptography, only Alice&#8217;s private key is able to decrypt messages that have been encrypted with Alice&#8217;s public key, and only Alice&#8217;s private key is able to create encrypted messages that can be decrypted by Alice&#8217;s public key. Specifically, a message encrypted with any other private key will produce different (usually meaningless) unencrypted data if Bob attempts to decrypt it using Alice&#8217;s public key.</p>
<p>The fundamentals of authentication consist of a challenge-response exchange. If Bob presents (&#8220;challenges&#8221;) Alice with a piece of arbitrary data, and Alice responds with a piece of encrypted data that decrypts to Bob&#8217;s original arbitrary data when decrypted using Alice&#8217;s public key, this proves that Alice possesses Alice&#8217;s private key. Nobody else other than the person who possesses Alice&#8217;s private key (presumably only Alice) could produce encrypted data that would decrypt back to Bob&#8217;s initial data using Alice&#8217;s public key. If Bob presented Mallory with arbitrary data, and Mallory wanted to impersonate Alice, he could not; without Alice&#8217;s private key, he would not be able to produce the expected response that Bob was looking for.</p>
<p>It is clear from this, however, that authentication is only useful if you already know the public key of the person you are hoping to communicate with. One common application of cryptographic authentication on computer networks is <a href="http://en.wikipedia.org/wiki/Secure_Shell">Secure Shell (SSH)</a> logins. Commonly, a user will install his or her public key on a server that they wish to log into via SSH, and will keep his or her private key on a personal machine. When logging into the server, the server challenges the client to prove that it holds the private key corresponding to the username that the client is trying to log in as. If the client satisfies the challenge with an appropriate response, the login is allowed without requiring a password for the user. </p>
<p>This is more secure and often more convenient than prompting for a password, since the private key is much harder to steal or guess than a password, and the same public key can be used on multiple servers with none of the security risks that apply to re-using the same password in multiple places. The same sort of thing can be done with web servers using something a little more complicated called a client-side certificate (see below about certificates), although these are uncommon on the public Internet and more often used on corporate intranets.</p>
<p>This is authentication: <strong>you can know with certainty who you are talking to</strong>. That is all; no more, no less. Note that this carries no implication of privacy. It is perfectly possible to authenticate your counterpart in a conversation and then proceed to have a non-private conversation. That wouldn&#8217;t be a common choice, but there&#8217;s nothing that prevents it.</p>
<p>More importantly, it is perfectly possible to have a private conversation without authenticating your counterpart. This is where a danger of a false sense of security lies. Bob could be talking over a perfectly private, encrypted connection, but if the person on the other end is Mallory and not Alice, Bob would never know that he is sending his sensitive data to, or receiving critical information from, a different and potentially malicious person. </p>
<p>In other words, just because you are sending your credit card number over a private, encrypted connection, doesn&#8217;t mean you aren&#8217;t unknowingly sending it directly to a criminal.</p>
<h3>3. Identification</h3>
<p>Identification is the aspect of applied cryptography that addresses the flaw in the above-described authentication process wherein you must know <em>a priori</em> the public key of the person you wish to communicate with. Perhaps surprisingly, this is the most complex common application of cryptography to security. If Alice and Bob wish to authenticate each other over the Internet, they must first exchange public keys. But they can&#8217;t just send them to each other over the Internet! If Bob received a message that purports to be from Alice and to contain Alice&#8217;s public key, he has no way to authenticate that the message actually came from Alice (and not from Mallory pretending to be Alice) without already knowing Alice&#8217;s public key. It&#8217;s a chicken-and-egg problem. </p>
<p>The direct solution to the problem is for Alice and Bob to exchange public keys off-line; to meet at Starbucks and hand each other CDs with their respective public keys on them. But this is not practical if Alice and Bob live thousands of miles apart, it is not practical if Alice is a banking institution and not a person, and it is still not practical if Alice and Bob do not already know each other. </p>
<p>If Alice and Bob are strangers (but still wish to authenticate one another) meeting to exchange CDs at Starbucks still, even if physically feasible, still isn&#8217;t secure. Mallory could show up at Starbucks a few minutes before Alice and, pretending to be Alice, give her public key to Bob, and now Bob will authenticate Mallory as Alice in future conversations. A way to fix this loophole is to have Bob check Alice&#8217;s driver&#8217;s license before accepting the CD. This is identification: <strong>you can know that a public key purporting to belong to a particular person or entity actually does</strong>.</p>
<p>Now, meeting in person and checking driver&#8217;s licenses is a human solution to a computing problem. There are of course computer-based solutions to this same problem that will also avoid the impracticalities of first having to meet in person with everyone whom you wish to authenticate later. But these solutions are based on the same principle as the driver&#8217;s license check: <em>trust</em>. The reason that Bob is willing to accept Alice&#8217;s driver&#8217;s license as proof that Alice is who she says she is because Bob <em>trusts</em> that the state government would not issue a license in a false name or with a false photograph (ignoring for the moment the possibility that the license itself is a fake and not issued by the state). Computational identification is based on the same notion of trust.</p>
<h3>4. Trust</h3>
<p>Ultimately, to accept that a public key belongs to the person it claims to, you must trust that it does. Trust can be simple, if for example the key was given to you in person by your friend Charlie who you are sure is not being impersonated by a shape-shifting alien. Trust can also be more indirect. If Charlie gives you his brother Dan&#8217;s public key, and you trust your that Charlie is honest and has good reason himself to trust that the key legitimately belong to Dan, then you can accept Charlie&#8217;s assertion that the key belongs to Dan as <em>identification</em> of Dan&#8217;s public key.</p>
<p>Computationally, this identification process is based on <em>signatures</em> and <em>certificates</em>. A certificate is like a driver&#8217;s license: it identifies a public key as belonging to a named individual, entity, company, or organization. The fundamentals of a certificate are simple. The person wishing to be certified generates a file with their identifying information (in a standardized format), and appends to it their public key. That&#8217;s all. But, of course, this certificate is worthless without trust. If a stranger just handed me a card saying &#8220;I am Alice, my public key is &#8230;&#8221;, I would not accept that as their identification, would you?</p>
<p>To be worth anything, certificates must be signed. I&#8217;ll get to the mechanics of signatures in the next section, but suffice to say that the goal of a cryptographic signature is to use a private key to produce a non-forgeable endorsement. If Dan produces a certificate for himself, and Charlie signs the certificate using his own private key, this functions as an assertion by Charlie that the contents of Dan&#8217;s certificate are accurate. Then, since I already trust my friend Charlie, Dan can simply present me with the signed certificate containing his public key to identify himself to me. I can check Charlie&#8217;s signature against Charlie&#8217;s public key (which I already have), and from that know that Charlie asserts that Dan&#8217;s certificate is accurate, and therefore that Dan&#8217;s purported public key actually belongs to him.</p>
<p>This is trust: <strong>you can know that a public key belongs to who it purports to by means of endorsement by a third party</strong>. What&#8217;s important is that this can all be done without ever actually contacting Charlie, beyond once to obtain and identify his public key in the first place.</p>
<p>Further yet, let&#8217;s say that Erin presents a certificate with her public key to me and this certificate is signed by Dan. If I trust that Charlie would only sign Dan&#8217;s certificate if Dan himself were trustworthy, then I can trust that Erin&#8217;s certificate is valid as well. This sort of peer-to-peer trust acquisition, where an identity certificate can be signed by any number of other individuals who trust the holder (with varying levels of expressed trust), is known as a <a href="http://en.wikipedia.org/wiki/Web_of_trust">web of trust</a>, and is commonly used for personal communications amongst security-sensitive Internet users.</p>
<p>But most Internet users never encounter a web of trust explicitly, and don&#8217;t really need to know how it works. What they do encounter frequently, however, is the similar notion of a public key infrastructure. This is used to establish Secure HTTP (or, more generally, <a href="http://en.wikipedia.org/wiki/Transport_Layer_Security">TLS</a>) connections. When establishing a secure connection to, say, Bank of America, it really does no good just to make the connection private. You must authenticate that the server you are communicating with really does belong to Bank of America. The server will send your browser its public key for authentication, but in order for the authentication to mean anything, the public key itself must first be identified. To facilitate identification, the server will send you a certificate.</p>
<p>In order to be identifiable, the certificate will be signed by a &#8220;<a href="http://en.wikipedia.org/wiki/Certificate_authority">certificate authority</a>&#8220;. A certificate authority is a company who sells certificate endorsements and who has the responsibility to do whatever is necessary to assure that the contents of the certificates they are signing is truthful. Part of this process may be to ask for a faxed-in copy of a driver&#8217;s license, or to call the company&#8217;s well-known phone number and check with their IT department. The price of the endorsement can itself be a means of ensuring that an applicant is not fraudulent; a large company will have no problem paying over a thousand dollars annually for an endorsement, but to a small-time impersonator, this might be prohibitive.</p>
<p>A <a href="http://en.wikipedia.org/wiki/Public_key_infrastructure">public key infrastructure (PKI)</a> differs from a web of trust in two major ways. First, in a PKI, a certificate is signed by only one endorser, while in a web of trust a certificate may have multiple endorsers. Second, while in a web of trust a user is interested in tracing the endorsement chain back to someone that he or she knows personally, in a PKI the browser is interested in tracing the endorsement chain back to a &#8220;root&#8221; Certificate Authority. What makes a certificate authority a functional &#8220;root&#8221; in the context of HTTPS is that the root authorities&#8217; certificates and public keys are pre-installed in the browser, and signed only by themselves. And so, ultimately, you are trusting that the manufacturer of your browser (Microsoft, the Mozilla foundation, Apple, Google, Opera, etc) is pre-installing root certificates only for trustworthy certifying authorities.</p>
<p>By now, you should know enough about privacy, authentication, and identification to understand what those HTTPS certificate error messages you receive from your browser mean. A browser error or warning message about an HTTPS certificate almost always indicates that a problem was encountered while attempting to use the certificate to identify the remote server (the actual authentication or encryption of the data almost never fails). The most common errors encountered are that a certificate has expired, or that a certificate&#8217;s chain of endorsements cannot be traced back to a known root certifying authority. A special case of the latter is a self-signed certificate, which is not signed by any certifying authority, root or otherwise. </p>
<p>These errors are important because they mean that the certificate presented by the server <em>cannot be trusted as identification</em>. You should afford them the same level of trust as identification that you would afford the &#8220;I am Alice&#8221; card that was handed to you; that is to say, none. And without identification of the public key, any authentication you attempt to perform on the remote server is equally worthless. The person handing you the &#8220;I am Alice&#8221; card could easily be Mallory and you would never know the difference. Note, however, that this says nothing about the compromise of privacy. </p>
<blockquote><p>An HTTPS (or TLS) connection using an expired, self-signed or otherwise untrusted certificate allows for <em>private</em> communication, but does not provide <em>authenticated</em> communication.</p></blockquote>
<p>That is, your data is protected against third-party snoopers on its transit through the Internet, but it is most certainly not protected against your counterpart being a malicious imposter. </p>
<p>I took so much space writing about trust and certificates largely to get to that point, because it is perhaps the most widespread and dangerous misconception about cryptography on the Internet. It is perfectly possible to have a cryptographically private conversation with a cryptographically unauthenticated, unidentified, and untrusted server. Just because you have obtained the &#8220;privacy&#8221; form of security does not imply that you have all of these other forms of security that you may also desire, so you shouldn&#8217;t assume that you do.</p>
<h3>5. Verification</h3>
<p>This will almost seem like a post-script considering how simple it is compared to identification and trust, and really it should logically appear between identification and trust, since it is the basis for signatures, but I didn&#8217;t want to break up the narrative.</p>
<p>Above, I glossed over the fact that a person (in a web of trust) or a certifying authority (in a public key infrastructure) is able to endorse a certificate by &#8220;signing&#8221; it. But what does that mean, exactly? Cryptographic signatures provide verification, the final common form of cryptographic security in modern computing. </p>
<p>Suppose that Bob writes a will leaving half his estate to Alice and half to Charlie, and disinheriting Mallory. Suppose then that Mallory sneaks into Bob&#8217;s home office, finds his will in his desk drawer, and modifies it such that it now leaves the entire estate to Mallory and disinherits Alice and Charlie. When Bob dies and the will is read, how can the executor <em>verify</em> that the will is what Bob wrote and has not been tampered with? In this non-computing situation, the will will have been signed by a witness or a notary public, and the executor will trust the witness or notary to inform him if the document differs from the document that they signed. </p>
<p>In computing, things work essentially the same way. If an e-mail (or document, or certificate) needs to be verified as having not been tampered with, it will be cryptographically signed, and the public key of the signer will be used to verify that the contents of the e-mail, document, or certificate have not changed since the signature was applied. This is verification: <strong>you have assurance that the data has not changed since a trusted party signed it</strong>. Again, don&#8217;t infer that this means more than it does. The document need not be private, and it is important that the signature be authenticated with an identified, trusted key in order to mean anything.</p>
<p>The mechanics of a cryptographic signature are simple. First, a <a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function">cryptographically secure hash function</a> is applied to the document to obtain a relatively short sequence of bytes. Normally, the function used today is <a href="http://en.wikipedia.org/wiki/SHA-1">SHA-1</a>. The important part about the sequence of bytes produced is that it would be incredibly difficult to create a meaningful document with different contents which would generate the same sequence when the cryptographic hash is applied to it. The output of the cryptographic hash is then encrypted using the signer&#8217;s private key and attached to the document.</p>
<p>The recipient can then use an identified and trusted public key belonging to the signer to decrypt the output of the cryptographic hash. If the recipient re-computes the hash on the data and compares it to the decrypted hash output, he can be assured that the document was not tampered with if the outputs match. In the case of certificates, the certifying authority&#8217;s signature of the certificate verifies that the identifying information contained within the certificate has not been altered since the time at which the certifying authority validated that the information was true.</p>
<p>In cases other than certificates, for example documents and e-mails, data is usually signed by its own author. For example, Alice sends an e-mail to Bob and signs it with her own private key. Then, presuming Bob already has an identified, trusted copy of Alice&#8217;s public key, he can not only <em>verify</em> that the message has not been tampered with, but he can also <em>authenticate</em> Alice as the author of the message, since no one but Alice could have produced a signature that would decrypt properly using Alice&#8217;s public key. If the message or document were signed by someone other than Alice, Bob would have to <em>trust</em> that the signer was being honest when endorsing that the message came from Alice.</p>
<p>What&#8217;s important to note is that if Alice simply sends a <em>private</em> message to Bob, this provides neither verification that the message has not been altered nor authentication that the message is actually from Alice. When Alice sends a private message to Bob, she encrypts it using Bob&#8217;s public key. This provides privacy and ensures that only the intended recipient (Bob) can read the message. But to provide verification and authentication, Alice must also sign the message with her own private key.</p>
<h3>Summary</h3>
<p>Hopefully, this article has helped the reader understand the similarities, differences, and interrelations between the five most common applications of cryptography to modern computing. To wrap up, I&#8217;ll repeat the most salient points about each:</p>
<dl>
<dt>Privacy</dt>
<dd>No third parties can read your data. Nothing is implied about the identity or trustworthiness of you or your counterpart. Neither you nor your counterpart can know that messages are not being altered or replaced in transit.</dd>
<dt>Authentication</dt>
<dd>You know with certainty that your counterpart possesses a particular private key. Nothing is implied about the identity or trustworthiness of your counterpart. The conversation may not be private, and neither you nor your counterpart can know that messages are not being altered or replaced in transit.
 </dd>
<dt>Identification</dt>
<dd>You know (somehow) that a particular private key corresponds to a particular identity. There is no &#8220;conversation&#8221; involved.
 </dd>
<dt>Trust</dt>
<dd>Due to an endorsement by an already-identified and already-trusted third party, you know that a particular private key corresponds to a particular identity. There is no &#8220;conversation&#8221; involved, but trust can be securely conveyed over insecure computer networks.</dd>
<dt>Verification</dt>
<dd>You know with certainty that messages between you and your counterpart are not being altered or replaced in transit. The conversation may not be private, and nothing is implied about the identity or trustworthiness of your counterpart.</dd>
</dl>
<p>Ideally, you want all of these things at once, and that&#8217;s exactly what HTTPS (or other protocols on top of TLS) give you. That&#8217;s why it&#8217;s completely secure to give your credit card number and personal details to a bank or other merchant over the Internet, so long as you are using HTTPS and you are not otherwise worried that the bank or merchant will misuse or mishandle this information in some way completely unrelated to having transmitted it over the Internet. </p>
<p>The certificate given by the web server is <em>trusted</em> by your browser because it is <em>identified</em> by a certificate which has its contents <em>verified</em> by a certifying authority&#8217;s signature. Thus, the certificate can be used to <em>authenticate</em> that you are communicating with the server that the certificate describes. Once all of that that is ascertained, the cryptographic key in the certificate is used to ensure that the conversation between you and the web server is <em>private</em> with respect to third parties along the route of data transit.</p>
<p>But, of course, to be truly secure, <em>all</em> of these aspects must be present, and a savvy Internet user must recognize that an HTTPS error displayed by the browser indicates that that is not the case. Moreover, when using or devising security systems that are not as well automated as TLS, one must be sure that each desired aspect of security is in place, and not make the assumption that one aspect implies the others, which is most certainly not the case.</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/3yAumSKazKY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2010/03/understanding-the-five-aspects-of-cryptographic-security/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://nerdland.net/2010/03/understanding-the-five-aspects-of-cryptographic-security/</feedburner:origLink></item>
		<item>
		<title>Designing Painless Protocols</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/F3sWdAgeKE0/</link>
		<comments>http://nerdland.net/2009/12/designing-painless-protocols/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 03:28:03 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Protocols]]></category>
		<category><![CDATA[Software Design]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=444</guid>
		<description><![CDATA[Of the several programming jobs I&#8217;ve had in my (still relatively short) career, the one thing I&#8217;ve had to do the most frequently is implement networking protocols. I&#8217;ve implemented standard protocols defined by RFCs, I&#8217;ve implemented in-house proprietary protocols, and I&#8217;ve implemented experimental protocols for academic research. I&#8217;ve yet to be asked to design my [...]]]></description>
			<content:encoded><![CDATA[<p>Of the several programming jobs I&#8217;ve had in my (still relatively short) career, the one thing I&#8217;ve had to do the most frequently is implement networking protocols. I&#8217;ve implemented standard protocols defined by RFCs, I&#8217;ve implemented in-house proprietary protocols, and I&#8217;ve implemented experimental protocols for academic research. I&#8217;ve yet to be asked to design my own from the ground up, but I have developed a good feel, from an implementer&#8217;s perspective, of what works and what doesn&#8217;t when it comes to legible, extensible, and robust protocol design.</p>
<p>The point of this post is not to give a guideline for how to design protocols that work well, or are efficient. That is a topic of much larger scope, and if you&#8217;re interested in that, <a href="http://tools.ietf.org/html/rfc3117">RFC 3117</a> is a good jumping-off point. Rather, this post aims to give a set of suggestions for how to design protocols that will be the least painful for yourself and other programmers to implement, debug, and apply. There is an underlying assumption here that you have already spent the time to decide what your protocol is trying to accomplish and that you have found a way to make it work well (assuming that it is implemented correctly).</p>
<p><span id="more-444"></span></p>
<h3>1. Don&#8217;t Re-invent the Wheel</h3>
<p>If you can get away with using an existing protocol, do so. &#8220;Don&#8217;t re-invent the wheel&#8221; is not exactly a protocol-design axiom; it&#8217;s more or less a computer programming axiom in general. The premise is that, even if it doesn&#8217;t fit your purpose to a T, a well-worn (and therefore well-tested and well-debugged) existing protocol, hopefully with an existing implementation, will save you more in time and avoided headaches than rolling your own would ever save you in efficiency.</p>
<p>The corollary to this rule is that if you can&#8217;t use an existing protocol, at least use an existing framework. In particular, if your protocol takes the form of a remote procedure call, there are a wealth of solutions including <a href="http://en.wikipedia.org/wiki/SOAP">SOAP</a> and <a href="http://code.google.com/apis/protocolbuffers/">Google&#8217;s Protocol Buffers</a> that can ease the effort.</p>
<h3>2. Prefer determinism</h3>
<p>I initially wanted to title this point &#8220;prefer simplicity&#8221;, but on reflection I realized that that isn&#8217;t really what I meant to say. Some protocols are inherently complicated, and there isn&#8217;t much that can be done about that. But even complicated protocols can have their complexity made tractable by proper modularization, and modularization is almost impossible without determinism.</p>
<p>The most familiar example of modularization in software is the encapsulation principle in object-oriented programming. The idea is that data is packaged into objects which can only be accessed through well-defined interfaces. This protects against unexpected operations on the data, and leads to less uncertainty (more determinism) about the state of the data and what could potentially happen to it next.</p>
<p>A fantastic example of a complex yet highly deterministic protocol is one half of the backbone of the modern Internet, TCP. The Transmission Control Protocol underlies most of the Internet communications we rely on daily. It provides ordered, reliable delivery with adaptive flow control to handle varying network conditions. Without TCP, hundreds of higher-level protocols would have to waste effort and add complexity by re-implementing some subset of its features. Yet, for all its power and complexity, its macro-operation can be accurately summarized in the following diagram:</p>
<div id="attachment_456" class="wp-caption aligncenter" style="width: 310px"><a href="http://nerdland.net/wp-content/uploads/2009/12/1000px-Tcp_state_diagram_fixed.svg_.png" rel="thumbnail"><img src="http://nerdland.net/wp-content/uploads/2009/12/1000px-Tcp_state_diagram_fixed.svg_-300x226.png" alt="" title="TCP State Diagram" width="300" height="226" class="size-medium wp-image-456" /></a><p class="wp-caption-text">TCP State Diagram.  (see footer for credits)</p></div>
<p>The main thing to note about this diagram is that, given any state, there is usually only one, sometimes two, and at most three other states that can be transitioned to. Any other action is illegal. A <code>SYN</code> packet received in the <code>FIN WAIT 2</code> state doesn&#8217;t have to be interpreted meaningfully.</p>
<p>In contrast, a poorly designed protocol might have the concept that &#8220;anything can happen at any time&#8221;, forcing an implementer to litter his or her code with complex state tracking and excessive case checks. Worse, non-determinism forces the implementer to consider how the implementation must respond in bizarre and unusual situations. In a deterministic protocol, these situations would either have their reactions explicitly defined, or be declared illegal and free an implementation from the requirement to make a meaningful response. In a non-deterministic protocol, failing to consider some obscure case or another becomes a sure source of bugs, incompatibilities, and potential exploits in young implementations.</p>
<p>Yes, an anything-goes non-deterministic protocol is quicker to design, but by doing so you shift all the effort of thinking out the implications of your design choices to the potentially numerous implementers, all of whom become encumbered with the burden of thinking out consistent reactions to every possibility, and none of whom have the luxury of being able to change the protocol&#8217;s design when a failing becomes apparent. They are forced simply to throw more code at the problem, leading to fragile, buggy, and generally poor implementations.</p>
<h3>3. Prefer human readability</h3>
<p>Premature optimization is the root of all evil. Unless your protocol needs, for some reason, to be blazing fast, and you know from testing that squeezing a few extra bytes of of each message will give you the speed you need, just use plain, simple text. </p>
<p>There are a number of advantages to a text protocol over a binary protocol. First is debugging. When your protocol implementation is behaving unexpectedly, it is much easier to read a captured stream of data and be able to immediately understand its contents than it is to be forced to write a protocol dissector that may have its own bugs getting in your way.</p>
<p>Second is discover-ability. A human-readable protocol is much easier for others to understand and learn. Unless you are attempting to intentionally impair interoperability (which is usually a misguided idea), this is a benefit. In lieu of, but preferably in addition to, formal documentation, a discover-able protocol is one for which someone wishing to write their own implementation can easily observe the behavior of two existing implementations, and learn how they communicate. Here, clear commands like <code>LOGIN</code> or <code>GOODBYE</code> make things a lot easier than raw numeric codes, in the absence of a protocol dissector.</p>
<p>Now, this <em>doesn&#8217;t</em> mean &#8220;don&#8217;t also be machine-readable&#8221;. You can combine the two approaches by using very structured syntax and/or keywords that can be machine-parsed with minimal effort, but which can still be read directly by humans. </p>
<p>One final note: if the human-readable part of your protocol is going to contain free-form text (i.e. strings that are not spelled out explicitly in the protocol specification), your protocol really should use <a href="http://en.wikipedia.org/wiki/Unicode">Unicode</a>. Next week we will be in the second decade of the twenty-first century, and <a href="http://www.internetworldstats.com/stats7.htm">English is most definitely not the only language on the Internet anymore</a>.</p>
<h3>4. Insist on network byte ordering</h3>
<p>This one is not a suggestion; it&#8217;s a rule. If your protocol uses multi-byte binary-encoded numbers, and it does not use <a href="http://en.wikipedia.org/wiki/Network_byte_order#Endianness_in_networking">network byte ordering</a>, then you have committed a mortal sin. Network byte ordering is called what it is for a reason &#8211; so that machines communicating on a network have a common standard for which byte ordering to use when interchanging data with one another. If new protocols don&#8217;t adhere to this standard, then what&#8217;s the point of having a standard?</p>
<p>The unfortunate part about network byte ordering is that it is big-endian, and as logical as big endianness is, the fact is that most of us program on little-endian machines. So the catch is that if we programmers don&#8217;t stop and think about what we&#8217;re sending out into the network, our numbers will more likely than not end up with little-endian byte ordering. The reason that this is a problem is that there are standard, portable functions that convert native byte ordering (whatever that may be) to big-endian, but there are no such things for native to little-endian. Speaking as someone who has more than once had to write an implementation of a little-endian protocol that will run on either architecture, this bites, so please be kind and use the standard.</p>
<p>Oh, and this relates to point 9 below as well, but please specify your protocol&#8217;s byte ordering in your documentation, even if it is little-endian for some (hopefully historical) reason. Otherwise, you will end up with people who write implementations that don&#8217;t do any conversion at all from their native ordering, and then then devices with different endianess can&#8217;t even talk over your protocol at all.</p>
<h3>5. Make magic numbers meaningful</h3>
<p>Let&#8217;s say your protocol can send some fixed set of message types. The simplest thing to do is just number them 1, 2, 3, 4, 5, etc, and be done with it. But you could be doing so much more with your numbers! Remember point 3, &#8220;Prefer Human Readability.&#8221; You can strike a happy compromise between human and machine readability by making your numbers meaningful, and then sparing a few extra bytes to encode them in text rather than as raw binary.</p>
<p>What do I mean by meaningful? Take for example the protocol that you probably just used to read this post, <a href="http://tools.ietf.org/html/rfc2616">HTTP</a>. Every HTTP response comes equipped with a numeric status code. These codes are not arbitrary. The code that every Internet denizen is most familiar with is 404 (File Not Found). The meaning embedded in this code is the first digit. HTTP response codes beginning with 4 indicate a client error. This contrasts with the &#8217;1&#8242; prefix indicating information, &#8217;2&#8242; indicating content, &#8217;3&#8242; indicating redirection, and &#8217;5&#8242; indicating a server error.</p>
<p>The <a href="http://tools.ietf.org/html/rfc959">FTP protocol</a> goes even farther than this. The second digit gives an analogous summary of the response&#8217;s purpose, and the first digit gives an indication of which state the protocol instance should be in as a result. For example a response code beginning with 20 indicates that the client may now send more commands (2) and that the previous command failed due to some kind of syntax error (0). A code beginning with 12 indicates that another response message is forthcoming (1), and that the response content pertains to server status (2).</p>
<p>This method of classifying machine-readable numeric representations of information into meaningful numeric ranges yields two important advantages. First, it is much easier for a human debugging the protocol to remember what the numerically-indicated semantic categories are than to remember each of a few dozen arbitrarily enumerated values individually. Second, it gives logically similar values numerically similar numbers. In other words, you&#8217;re not going to end up assigning &#8220;Vanilla Ice Cream&#8221; to 3, and then when you realize later that you forgot about chocolate, assigning &#8220;Chocolate Ice Cream&#8221; to 417, right between &#8220;Cyanide&#8221; and &#8220;Adult Diapers&#8221;.</p>
<p>Which brings me to my next point&#8230;</p>
<h3>6. Design for expansion</h3>
<p>If your protocol is worth anything, it will be revised. And it will be revised again and again. In fact, the more useful you protocol is, the more likely it is to have multiple revisions and numerous extensions. Prepare for this from the state.</p>
<p>There are two ways to go about this, and you can do both. One is to reserve space for expansion. One form of this is to assign meaningful numbers as described above. If you decree that all numbers beginning with 3 mean &#8220;A Type of Ice Cream&#8221;, then you&#8217;ll have plenty of room to add Pistachio and Rocky Road when you remember them in version 2.0. A similar idea is to explicitly assign some values (or bit positions, in the case of flags) as &#8220;reserved&#8221; initially, and assign them to something meaningful when the time comes and they are needed.</p>
<p>Another way to go is to explicitly indicate your protocol version. I&#8217;d say that it&#8217;s probably always a good idea to do this. This way, one end of the connection (or both) can announce its version, and if the versions mismatch, the implementations can either decide not to communicate, or better, agree on a highest commonly-supported version to use. </p>
<p>The benefit to this is that from the point of agreement forward, the implementations don&#8217;t have to worry about backwards-compatibility, even for very basic things such as message structure. Reserving numbers and bits is nice, but once you run out of numbers or bits, you have to change the message format in a way that can&#8217;t be pre-determined. To get maximum benefit out of agreeing on a version to use, the version negotiation (or announcement) should occur as soon as possible in the exchange. If possible, the version should be the very first byte (or bytes) sent so that everything else about the protocol can be changed with wild abandon if desired. The other backbone protocol of the Internet, <a href="http://tools.ietf.org/html/rfc791">IP</a>, does exactly this, and that helps makes <a href="http://www.ipv6.org/specs.html">IPv6</a> possible.</p>
<h3>7. Don&#8217;t be stingy with information</h3>
<p>Except to the extent that it becomes a security concern, one end of your protocol should never hide relevant information from the other. In other words, each end of the connection should have within the protocol a mechanism for querying the other end for information. Relying on assumptions about the properties or state of the connection parter will only lead to increased fragility and more backwards-compatibility headaches. In fact, the version announcement mechanism described in the previous point is almost a special case of this. Each end of the connection preemptively answers the implicit query &#8220;which version(s) of the protocol do you support?&#8221;</p>
<p>For example, consider a case where you are a client trying to retrieve a piece of data from a remote server. The protocol first makes you select a data set, and then the client may either retrieve a specific datum by key, or retrieve the entire set. There are several practical queries that the server should be prepared to respond to, and which the protocol should make possible: </p>
<ul>
<li>Which data sets exist on the server?</li>
<li>Which data sets does this client have permission to access?</li>
<li>Which data set is currently selected?</li>
<li>Which keys are available in this data set?</li>
</ul>
<p>In the absence of these queries, the client may have to behave inefficiently, or be unable to operate at all. If there is no query to determine the available data sets, the protocol has made the assumption that the client and the server will always both know and agree upon the names of the available datasets, which makes it hard to modify the data structure without modifying both sides of the protocol. Similarly, if there is no mechanism to query for keys, a client may in some circumstances need to retrieve the entire data set when it could have gotten away with much less traffic. The ability to determine the currently selected set, while seemingly unnecessary, may allow a simplified client implementation with less explicit state tracking, depending on the details of this imaginary protocol.</p>
<p>In general these sorts of things are dead simple to include, and (again aside from security concerns) confer only benefits.</p>
<h3>8. Document your protocol precisely</h3>
<p>Code programs computers, and specifications program programmers. Much like you cannot expect a computer to do what you want it to do in the absence of specific, precise code explaining exactly what it should do, you cannot expect a programmer to do what you want him or her to do in the absence of a specific, precise specification explaining exactly what you want him or her to do.</p>
<p>If you ever hope to have other programmers write implementations of your protocol that are interoperable with yours, your protocol had better have good documentation. The documentation needs not only to specify what the packet layout or command syntax is, but also what the observable semantics of the messages should be. </p>
<p>For example, it is a really bad idea to create a flag in one of your packets named &#8220;restart connection&#8221;, and give no further documentation on what should happen when a packet with that flag set is received. You may know what you mean by that, but another programmer working only from your documentation, will not know whether this requires a confirmation packet, whether this is a request or a command, what will happen if another packet is sent without restarting the connection, what state the protocol should be in upon being reset, whether there should be a limit to the number of consecutive restarts before giving up.</p>
<p>Better documentation for such a flag (using <a href="http://tools.ietf.org/html/rfc2119">RFC 2119</a> keywords) would be:</p>
<blockquote><p><strong>Restart Connection Flag.</strong> If a packet with the Restart Connection Flag set is received, the receiver MUST NOT send any further packets to the remote host. If any further packets with this connection ID are received by the remote host, they will be rejected with an Invalid Connection ID error. The host receiving the Restart Connection command SHOULD immediately restart the connection by sending a Hello packet to the remote host. No state information from the current connection will be retained in the new connection.</p></blockquote>
<p>This makes it clear that this is a command, not a request, and does not require (and in fact forbids) a response. It makes clear what will happen if the request is ignored, and tells you exactly how to take the action that the command requires. It also implies (by using SHOULD rather than MUST), that an implementation may choose not to restart and instead just cease communication. Permitting this behavior allows an implementation to, for example, escape from an infinite loop of restarts without violating the protocol semantics.</p>
<h3>9. Follow the Robustness Principle</h3>
<p>Also known as Postel&#8217;s Law, the <a href="http://en.wikipedia.org/wiki/Robustness_principle"Robustness Principle</a> states: &#8220;be conservative in what you do, be liberal in what you accept from others.&#8221; This was originally coined in <a href="http://tools.ietf.org/html/rfc761">RFC 761</a>, the document specifying TCP. This is a very important, and widely known, yet also widely misunderstood aphorism.</p>
<p>The most notorious misapplication of this principle was in the implementation of early HTML parsers. Based on this idea, the parsers would take in any old junk that vaguely resembled HTML and try as hard as possible to make something legible out of it when displaying the results. The result of this extreme laxity was more than a decade of the nightmare known as &#8220;tag soup&#8221; which is only now beginning to abate.</p>
<p>The real meaning of the Robustness Principle is not that erroneous input should be accepted as valid, but that erroneous input should not cause catastrophic failure, that any valid parts of a partially-erroneous input should be accepted if possible, and that diagnostics should be given for erroneous input when feasible. An HTML parser implementation that properly followed this rule would, upon receiving &#8220;tag soup&#8221; HTML, produce a warning message that the HTML was invalid, hopefully display some sort of information about what about it was wrong (e.g. unclosed anchor tag, missing doctype, etc), and only then try to (or give the option to) display the parser&#8217;s best approximation of what the author meant.</p>
<p>An HTML parser is not a protocol, but a protocol should behave similarly. Given an illegal command or request, or a set of conflicting options, what a protocol implementation should not do is: crash, silently ignore the problematic messages, or arbitrarily choose one of the conflicting options to ignore. Instead, the implementation should respond with a diagnostic indicating why the message could not be fully processed, and should process any part of the message that was valid if it can be done safely and unambiguously. </p>
<p>Keep in mind that the diagnostics need not be completely machine readable. A set of mutually exclusive options specified together could be responded to with a message like &#8220;573 Illegal Options &#8211; Foo and Bar cannot be specified at the same time&#8221;, where 573 is a machine-readable code indicating that the message was not processed due to an error in the options field, and the rest is something that can be understood by a human programmer when debugging his or her implementation.</p>
<p>The only significant exception to the applying robustness principle is in avoiding denial-of-service attacks. Responding verbosely to malformed input may exacerbate the effects of a denial-of-service attack, and so it is for example reasonable to cease this behavior in the face of repeated offenses originating from the same host in a short period of time.</p>
<h3>10. Design for security from the start</h3>
<p>In the nine points above I have referenced numerous protocols from the standard Internet protocol suite. I did this not just because they are well-known but also because they are for the most part well-designed (otherwise they would not have survived the explosion in the size of the Internet in the past twenty years).</p>
<p>However, one shortcoming is common to many of them, and we live with its detrimental effects every day. These protocols, designed when the Internet was in its infancy as an academic and governmental experiment, were not designed with security in mind. This is what facilitates spam, denial-of-service, phishing, privacy invasion, and all other sorts of malfeasance on the Internet. </p>
<p>Today, however, the Internet is relatively mature and very, very public, so there is absolutely no excuse to design a new, security-free protocol. Neither is it acceptable to defer the addition of security features to a later version of the protocol. Another vital lesson that we have learned from the Internet protocol suite is that it is incredibly difficult to adopt secure protocol enhancements after a protocol has been widely deployed. If this were trivial, then the entire Internet would be running over IPsec already.</p>
<p>This isn&#8217;t to say that all traffic should be encrypted. Of course it is acceptable to forgo encryption if the traffic isn&#8217;t sensitive, but things that are completely unacceptable in the modern Internet include sending passwords in the clear, using predictable sequence numbers, and assuming good behavior from the other end of the connection. If you simply avoid those security pitfalls and make use of some established mechanism (e.g. <a href="http://tools.ietf.org/html/rfc5246">TLS</a>) for producing a cryptographic layer when you need to transmit sensitive information, your protocol will be much, much better off for it.</p>
<p>Note well, though, that to be secure does not mean to be obscure. Encrypting your protocol is quite different from making it incomprehensible. Encryption should be a layer. Once the encryption layer is removed, the protocol should continue to adhere to the design principles articulated above, including human-readability, discover-ability, meaningful magic numbers, etc.</p>
<p><em>TCP State Diagram © 2009 <a href="http://commons.wikimedia.org/wiki/File:Tcp_state_diagram_fixed.svg"> Sergiodc2</a>  and <a href="http://commons.wikimedia.org/wiki/File:Tcp_state_diagram_fixed.svg">Marty Pauley</a>. <a href="http://creativecommons.org/licenses/by-sa/3.0/">Some rights reserved.</a></em></p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/F3sWdAgeKE0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/12/designing-painless-protocols/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/12/designing-painless-protocols/</feedburner:origLink></item>
		<item>
		<title>The Markovian State Space of War</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/u5iecHKtzRA/</link>
		<comments>http://nerdland.net/2009/08/the-markovian-state-space-of-war/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 01:25:51 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Markov Chains]]></category>
		<category><![CDATA[Probability]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=423</guid>
		<description><![CDATA[When I was a kid, one of my favorite card games was War. In retrospect, I don&#8217;t really understand why I got so much enjoyment out of it, given that there is absolutely no strategy to the game whatsoever. If you happened to miss this game during your childhood, the rules are simple: The deck [...]]]></description>
			<content:encoded><![CDATA[<p>When I was a kid, one of my favorite card games was War. In retrospect, I don&#8217;t really understand why I got so much enjoyment out of it, given that there is absolutely no strategy to the game whatsoever. If you happened to miss this game during your childhood, <a href="http://en.wikipedia.org/wiki/War_(card_game)">the rules are simple</a>:</p>
<blockquote><p>
The deck is divided evenly between the players face-down. Each player reveals his top card, and the player with the higher card puts both the cards on the bottom of his deck. If the cards are of equal value, each player plays three face-down cards and a fourth face-up card, and the higher-valued card wins all the cards on the table. This is known as a war. In the case of another tie, the process is repeated until there is no tie.</p>
<p>A player wins by collecting all the cards. If a player runs out of cards while dealing the face-down cards of a war, he may play the last card in his deck face-up and still have a chance to stay in the game.
</p></blockquote>
<p>As you can see, since the player has no knowledge of which cards are in their initial hand, and no choice in which cards to play, this game could just as easily be played by a properly trained parakeet. The mechanical gameplay and lack of strategy, however, makes certain questions about the game mathematically interesting. </p>
<p>The other day when this game popped into my head, one of the first things I remembered about it was how many games I left unfinished due to their sheer length. I suddenly became curious about the expected number of turns required to finish a game of War.</p>
<p>It turns out that the answer is &#8220;about 277&#8243; (which is considerably less than I expected). You see, people on the Internet tend to be pretty big nerds, and certainly <a href="http://www.rajgiri.net/index.php?page=6">I wasn&#8217;t the first one to consider writing a War simulation</a> to figure out these kind of statistics. What I didn&#8217;t see discussed, though, is any treatment of the <em>structure</em> of a game of war.</p>
<p><span id="more-423"></span></p>
<p>The vital step to the game turns out to be re-integrating your captured cards back into your active deck. If this re-integration is entirely deterministic, the progress and outcome of the game is completely determined by the initial deal. However, as is mentioned on any page discussing War simulation, if the re-integration is deterministic, it can (and with surprising frequency <em>does</em>) lead to games of War which will never terminate.</p>
<p>There are essentially three major non-deterministic ways to re-integrate captured cards into the active deck:</p>
<ol>
<li>Add the captured cards to the bottom of the active deck in random order</li>
<li>Add the captured cards to random positions in the active deck</li>
<li>Pool the captured cards in a &#8220;capture deck&#8221; until the active deck is empty, then shuffle the capture deck, and make it the new active deck.</li>
</ol>
<p>When I used to play War, I played using method 3, and I&#8217;m not sure anyone actually uses method 2 (it would be very prone to bias and deliberate cheating in the hands of a human rather than a simulation), but it is interesting to think about. I&#8217;ll refer to 1 as Standard War, 2 as Continuous-Shuffle War, and 3 as Pooled-Capture War.</p>
<p><strong>It turns out that, regardless of the re-integration method, a game of war can be accurately and completely modeled as a Markov chain.</strong> This isn&#8217;t actually all that surprising. A <a href="http://en.wikipedia.org/wiki/Markov_chain">Markov chain</a> is really just a technical name for a series of states where the next event depends probabilistically only on the previous state and no states prior to that. Since children&#8217;s games often intentionally minimize strategy and state memory, it turns out that several well-known and popular children&#8217;s games are in fact nothing more than brightly-colored Markov chains; for example, Candyland, <a href="http://findarticles.com/p/articles/mi_qa3997/is_200312/ai_n9338086/">Chutes and Ladders, and Hi-Ho Cherry-O</a>. War stands out among these, though, because the state space for War is absolutely enormous.</p>
<p>The smallest state space belongs to Continuous-Shuffle War. In this game, a state can be uniquely identified by specifying which player has which cards. Order is irrelevant. Therefore, the number of possible states is <img src='http://nerdland.net/wp-content/latex/e0f/e0fd298db4e738eaa181d1db96464f16-T-000000-0.png' alt='p+\sum_{q=2}^{p} q!\binom{q}{p}{n \brace p}' title='p+\sum_{q=2}^{p} q!\binom{q}{p}{n \brace p}' class='latex' /> where the brace notation in within the sum denotes a <a href="http://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind">Stirling number of the second kind</a>, with parameters <img src='http://nerdland.net/wp-content/latex/7b8/7b8b965ad4bca0e41ab51de7b31363a1-T-000000-0.png' alt='n' title='n' class='latex' /> the number of cards, and <img src='http://nerdland.net/wp-content/latex/838/83878c91171338902e0fe0fb97a8c47a-T-000000-0.png' alt='p' title='p' class='latex' /> the number of players. This is just shorthand for asking &#8220;How many different ways can we divide <img src='http://nerdland.net/wp-content/latex/7b8/7b8b965ad4bca0e41ab51de7b31363a1-T-000000-0.png' alt='n' title='n' class='latex' /> items into <img src='http://nerdland.net/wp-content/latex/838/83878c91171338902e0fe0fb97a8c47a-T-000000-0.png' alt='p' title='p' class='latex' /> non-empty groups?&#8221; The factor of <img src='http://nerdland.net/wp-content/latex/294/294db25a677c9b8b563e179477d9385b-T-000000-0.png' alt='q!' title='q!' class='latex' /> is there because Stirling number does not take order into account, and in this game two states where the players&#8217; decks are swapped around are not equivalent. The sum is necessary because Stirling numbers of the second kind only count divisions into <em>non-empty</em> groups, and so we have to account for a <img src='http://nerdland.net/wp-content/latex/838/83878c91171338902e0fe0fb97a8c47a-T-000000-0.png' alt='p' title='p' class='latex' />-player game devolving into a <img src='http://nerdland.net/wp-content/latex/fc8/fc86261874dd92078554f3ac92efbca8-T-000000-0.png' alt='(p-1)' title='(p-1)' class='latex' />-player game when one player runs out of cards. The <img src='http://nerdland.net/wp-content/latex/bcf/bcf2945a802acba888c8f7028947b161-T-000000-0.png' alt='\binom{q}{p}' title='\binom{q}{p}' class='latex' /> exists to take into account <em>which</em> players have run out of cards in a reduced game. The additional <img src='http://nerdland.net/wp-content/latex/838/83878c91171338902e0fe0fb97a8c47a-T-000000-0.png' alt='p' title='p' class='latex' /> is for the number of finishing states in which one player has all the cards. How many states are there in this chain, then? For a typical game with a 52-card deck and 2 players, there are 4 503 599 627 370 496.</p>
<p>Next, there is Pooled-Capture War. Here, we still don&#8217;t have to concern ourselves with order, but each player essentially maintains two decks &#8211; the active deck and the capture deck. The reason we don&#8217;t concern ourselves with order is that the active deck is always essentially drawn from in random order, owing to the fact that the player plays repeatedly plays through an entire freshly-shuffled active deck, rather than adding cards to the active deck as he goes. In this case, there is really no difference between shuffling the capture deck when it becomes active, and drawing from the active deck at random. Therefore the number of states for Pooled-Capture War is almost the same as for four-player Continuous-Shuffle War, but not quite. The difference is that since each player has two decks, the number of finishing states increases from <img src='http://nerdland.net/wp-content/latex/838/83878c91171338902e0fe0fb97a8c47a-T-000000-0.png' alt='p' title='p' class='latex' /> to <img src='http://nerdland.net/wp-content/latex/06a/06ac336eada31c82219312ea68d9b7cd-T-000000-0.png' alt='p ({n \brace 2} + 2)' title='p ({n \brace 2} + 2)' class='latex' />, because when one player has all the cards, they can be divided between the active and capture decks in <img src='http://nerdland.net/wp-content/latex/333/3339a7c53caf4da481f0dbe54e2c08bd-T-000000-0.png' alt='{n \brace 2} + 2' title='{n \brace 2} + 2' class='latex' /> different ways. Therefore, the number of possible states in Pooled-Capture War is <img src='http://nerdland.net/wp-content/latex/5bf/5bffb9eaa2f4e879d2085a77206a1854-T-000000-0.png' alt='p ({n \brace 2} + 2) +\sum_{q=2}^{p} q!\binom{q}{p}{n \brace 2p}' title='p ({n \brace 2} + 2) +\sum_{q=2}^{p} q!\binom{q}{p}{n \brace 2p}' class='latex' />. For a typical game, this works out to 1 690 198 646 610 354 052 103 573 055 990 states.</p>
<p>Finally, for Standard War, a state can only be uniquely identified by specifying the active decks of both players, where order is this time relevant. This means that the total number of states is expressed as a sum of of all possible permutations of hands over the different quantities of cards each player might have. In other words,<br />
<img src='http://nerdland.net/wp-content/latex/22c/22c5ea9c8f7d7784bd2ffaa30b43136e-T-000000-0.png' alt='\sum_{i_1=0}^{n} i_1! \sum_{i_2 = 0}^{n-i_1} i_2! \sum_{i_3 = 0}^{n-i_1-i_2} i_3! \cdots \sum_{i_{p-1} = 0}^{n-\sum_{j=0}^{p-2} i_j} i_{p-1}! \left(n - \sum_{j=0}^{p-1} i_j\ \right)!' title='\sum_{i_1=0}^{n} i_1! \sum_{i_2 = 0}^{n-i_1} i_2! \sum_{i_3 = 0}^{n-i_1-i_2} i_3! \cdots \sum_{i_{p-1} = 0}^{n-\sum_{j=0}^{p-2} i_j} i_{p-1}! \left(n - \sum_{j=0}^{p-1} i_j\ \right)!' class='latex' /><br />
(If anyone can think of a more compact way to write this, please let me know.) This formula essentially takes all choices of how to assign <img src='http://nerdland.net/wp-content/latex/7b8/7b8b965ad4bca0e41ab51de7b31363a1-T-000000-0.png' alt='n' title='n' class='latex' /> cards to <img src='http://nerdland.net/wp-content/latex/838/83878c91171338902e0fe0fb97a8c47a-T-000000-0.png' alt='p' title='p' class='latex' /> players, and then sums the distinct permutations within each of these possibilities. This works out to right around 164 548 210 943 005 434 162 687 272 511 830 757 530 229 530 638 988 932 546 560 000 000 000 distinct states for a typical game.</p>
<p>In order to work out the answer to my initial question of the expected number of rounds in a game of War and other mathematical questions about War, the approach would be to compute a transition table <img src='http://nerdland.net/wp-content/latex/b9e/b9ece18c950afbfa6b0fdbfa4ff731d3-T-000000-0.png' alt='T' title='T' class='latex' /> where cell <img src='http://nerdland.net/wp-content/latex/439/43955adb3ad48ac67c934b03ed85e1e0-T-000000-0.png' alt='T_{S_1,S_2}' title='T_{S_1,S_2}' class='latex' /> answers the question: Given that you are now in state <img src='http://nerdland.net/wp-content/latex/9f1/9f15cdedd8d76e4abb50732f5727065b-T-000000-0.png' alt='S_1' title='S_1' class='latex' />, what is the probability that after the next turn you will be in state <img src='http://nerdland.net/wp-content/latex/a3d/a3de00c1597600a387128a7add5b354f-T-000000-0.png' alt='S_2' title='S_2' class='latex' />? The difficulty of working out this table is easiest for Standard War, and hardest for Pooled-Capture War.</p>
<p>In Standard War, we know based on the state alone which cards will be played, and therefore exactly what the result of the upcoming turn will be. Probability only comes into play when deciding in what order the captured cards will be added to the winner&#8217;s deck. This probability is also simple; each integration order is equally likely, and so has probability <img src='http://nerdland.net/wp-content/latex/94c/94cbb8aa727c946df67dc0cedcf39ed5-T-000000-0.png' alt='\frac{1}{n_c!}' title='\frac{1}{n_c!}' class='latex' /> where <img src='http://nerdland.net/wp-content/latex/ebc/ebce0f627b43a4894b355cc1d6811dab-T-000000-0.png' alt='n_c' title='n_c' class='latex' /> is the number of cards captured. The number of states that you can reach in any one turn is relatively small, and so the matrix <img src='http://nerdland.net/wp-content/latex/b9e/b9ece18c950afbfa6b0fdbfa4ff731d3-T-000000-0.png' alt='T' title='T' class='latex' /> for Standard War, while huge, is very sparse.</p>
<p>In Continuous-Shuffle War, we don&#8217;t know which cards will be played just by inspecting the state. This isn&#8217;t by itself that complicating since we can assume each player will draw from their cards with equal probability of drawing any given card. The complicating factor is that because &#8220;wars&#8221; can go on for an arbitrary number of draws, any given state can reach almost any other state in a single turn, albeit with very small probability for most of them. Still, since these probabilities are nonzero, the smaller matrix <img src='http://nerdland.net/wp-content/latex/b9e/b9ece18c950afbfa6b0fdbfa4ff731d3-T-000000-0.png' alt='T' title='T' class='latex' /> for Continuous-Shuffle War is very densely populated.</p>
<p>In Pooled-Capture War, we have all the problems of Continuous-Shuffle War, plus when computing the probabilities, one has to deal with the fact that the capture deck can become the active deck in the middle of a turn. This doesn&#8217;t make the matrix <img src='http://nerdland.net/wp-content/latex/b9e/b9ece18c950afbfa6b0fdbfa4ff731d3-T-000000-0.png' alt='T' title='T' class='latex' /> any more densely populated, but it does severely complicate the probability calculation algorithm.</p>
<p>Were we able to compute <img src='http://nerdland.net/wp-content/latex/b9e/b9ece18c950afbfa6b0fdbfa4ff731d3-T-000000-0.png' alt='T' title='T' class='latex' />, we could answer my original question in this manner: In a Markov chain, the <img src='http://nerdland.net/wp-content/latex/e35/e358efa489f58062f10dd7316b65649e-T-000000-0.png' alt='t' title='t' class='latex' />th power of <img src='http://nerdland.net/wp-content/latex/b9e/b9ece18c950afbfa6b0fdbfa4ff731d3-T-000000-0.png' alt='T' title='T' class='latex' />, <img src='http://nerdland.net/wp-content/latex/a0f/a0f817629c0124bffb9b3b7827eabb2f-T-000000-0.png' alt='T^t' title='T^t' class='latex' />, gives the transition probabilities of ending up in particular states after <img src='http://nerdland.net/wp-content/latex/e35/e358efa489f58062f10dd7316b65649e-T-000000-0.png' alt='t' title='t' class='latex' /> turns. We then compose a begin row-vector <img src='http://nerdland.net/wp-content/latex/0f4/0f4c4ce0863d100a12c90c114fd9abeb-T-000000-0.png' alt='\vec{b}' title='\vec{b}' class='latex' /> where probabilities are distributed uniformly among the states which could correspond to an initial deal. Then, for each turn <img src='http://nerdland.net/wp-content/latex/e35/e358efa489f58062f10dd7316b65649e-T-000000-0.png' alt='t' title='t' class='latex' />, we can compute <img src='http://nerdland.net/wp-content/latex/6f8/6f8d36c849c5d2e0c142d5336a96e07d-T-000000-0.png' alt='\vec{b} T^t' title='\vec{b} T^t' class='latex' /> and add up the probabilities of all final states to get <img src='http://nerdland.net/wp-content/latex/960/96025d2a1ae8909422807b1a2cd996ee-T-000000-0.png' alt='P[e_t]' title='P[e_t]' class='latex' />, the probability that the game has ended by turn <img src='http://nerdland.net/wp-content/latex/e35/e358efa489f58062f10dd7316b65649e-T-000000-0.png' alt='t' title='t' class='latex' />. The expected number of turns the game will last is then <img src='http://nerdland.net/wp-content/latex/6d2/6d20c05db396945deea9caec50f6ffc0-T-000000-0.png' alt='\sum_{t=0}^{\infty} t (1 - P[e_t])' title='\sum_{t=0}^{\infty} t (1 - P[e_t])' class='latex' />. We could also use <img src='http://nerdland.net/wp-content/latex/0f4/0f4c4ce0863d100a12c90c114fd9abeb-T-000000-0.png' alt='\vec{b}' title='\vec{b}' class='latex' /> vectors that specify a single particular initial deal to compute the probabilities that each player will win, given that initial deal.</p>
<p>However, my conclusion after thinking all of this through is that it probably isn&#8217;t worth it to try to write a program to compute these kinds of things. The time required to iterate through the states of any of these methods, let alone the space required for <img src='http://nerdland.net/wp-content/latex/b9e/b9ece18c950afbfa6b0fdbfa4ff731d3-T-000000-0.png' alt='T' title='T' class='latex' /> (which has size the <em>square</em> of the number of states, times the size of the datatype used to store the probability value), makes this endeavor completely intractable for all but pathetically small decks of cards (around 10 or so). I figure that while the simulation-discovered answer of 277 is somewhat less accurate than a probabilistically computed answer would be, it&#8217;s likely to be accurate enough for any future application of this information.</p>
<p>(Thanks to <a href="http://www.wolframalpha.com/">Wolfram Alpha</a> for making possible the giant scary numbers in this post.)</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/u5iecHKtzRA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/08/the-markovian-state-space-of-war/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/08/the-markovian-state-space-of-war/</feedburner:origLink></item>
		<item>
		<title>Unstumping the Internet and New Interesting Links</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/LWRKNNAzjA0/</link>
		<comments>http://nerdland.net/2009/08/unstumping-the-internet-and-new-interesting-links/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 01:33:06 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Site News]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[New Features]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=419</guid>
		<description><![CDATA[Two new items have been added to Nerdland today. First, on the left hand side you will see a link to a new section entitled &#8220;Unstumping the Internet&#8220;. The purpose of this section, as its index page explains, is This set of pages is for cataloging relatively brief answers to questions that I had to [...]]]></description>
			<content:encoded><![CDATA[<p>Two new items have been added to Nerdland today.</p>
<p>First, on the left hand side you will see a link to a new section entitled &#8220;<a href="http://nerdland.net/unstumping-the-internet/">Unstumping the Internet</a>&#8220;. The purpose of this section, as its index page explains, is</p>
<blockquote><p>
This set of pages is for cataloging relatively brief answers to questions that I had to figure out myself after being unable to find the answer on the Internet. [...] When I encounter a question that I cannot find an answer for on the Internet, and especially if in my searching I notice that several other people have asked this question with no satisfactory answer, I will post the answer here when I discover it. The hope is that next time someone searches the Internet for this question, they will find my answer.
</p></blockquote>
<p>So this section is not something that I expect anyone to read frequently, or even at all. I&#8217;m not going to be posting to the front page when I add new articles there, as the whole point of this section is to not clutter the front page with items of limited interest and minimal depth. Instead, I hope that these pages will visited primarily as the results of search engine queries.</p>
<p>Secondly, on the right hand side, there is a new section of links entitled &#8220;Interesting Items Elsewhere&#8221;. This is a listing of the last few items from other weblogs (or other sorts of feeds) that I have found most interesting. This is in fact tied to my <a href="http://www.google.com/reader">Google Reader</a> account, and displays items that I have &#8220;shared&#8221;, so these may not always be completely serious or computer-related. You can click on the &#8220;more&#8221; link at the bottom to see everything I&#8217;ve shared, as opposed to just the few most recent items.</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/LWRKNNAzjA0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/08/unstumping-the-internet-and-new-interesting-links/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/08/unstumping-the-internet-and-new-interesting-links/</feedburner:origLink></item>
		<item>
		<title>Islanded in a Stream of Chars</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/UO8wWjGZr8k/</link>
		<comments>http://nerdland.net/2009/08/islanded-in-a-stream-of-chars/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 01:38:45 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[iostreams]]></category>
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=389</guid>
		<description><![CDATA[From the &#8220;things that really shouldn&#8217;t be difficult, but for some reason are anyway&#8221; department comes the following. Do you think you know how to program in C++? Familiar with objects and polymorphism and templates and everything? Then this should be dead easy. Should, I said. Problem: Write a function that takes in a std::istream [...]]]></description>
			<content:encoded><![CDATA[<p>From the &#8220;things that really shouldn&#8217;t be difficult, but for some reason are anyway&#8221; department comes the following. Do you think you know how to program in C++? Familiar with objects and polymorphism and templates and everything? Then this should be dead easy. Should, I said.</p>
<blockquote><p><strong>Problem: </strong>Write a function that takes in a <a href="http://www.cplusplus.com/reference/iostream/istream/"><code>std::istream</code></a> and a size <code>n</code> and returns a <a href="http://www.cplusplus.com/reference/string/string/"><code>std::string</code></a>. The string should contain the first <code>n</code> characters of the input stream, with all formatting (whitespace, newlines, etc) preserved. </p></blockquote>
<p>You can ignore all concerns about multi-byte characters for the sake of this problem. Sounds simple, right? You&#8217;d be able to crank this out in ten seconds if someone asked you this in an interview, right? Okay, now try it with this caveat.</p>
<blockquote><p><strong>Caveat: </strong>You must do this in a purely C++ &#8220;style&#8221;. To be precise, you must do this without using any character variables or character arrays. Use only a <code>std::string</code> object (or some other memory-managed object in the standard library) as your input buffer.</p></blockquote>
<p>For as much as the C++ STL tries to encourage you to use <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization">RAII</a>-oriented containers instead of raw arrays, this seemingly trivial task requires some surprisingly baroque coding. If you want to test yourself, try writing the function before you click <em>more</em>.</p>
<p><span id="more-389"></span><br />
As much as we&#8217;d all like it to be, the following is <em>not</em> the right answer:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">std::<span class="me2">string</span> extractStr<span class="br0">&#40;</span>std::<span class="me2">istream</span>&amp; in, std::<span class="me2">streamsize</span> n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; std::<span class="me2">string</span> str;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; in.<span class="me1">get</span><span class="br0">&#40;</span>str, n<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">return</span> str;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The main reason that this is so much harder than it needs to be is that the <a href="http://www.cplusplus.com/reference/iostream/istream/get/"><code>istream::get()</code></a> function does not provide an overload that reads directly into a string. You have only three choices if you go that route. You may either read character-by-character, or you may read into a character array, or you may read into a <a href="http://www.cplusplus.com/reference/iostream/streambuf/"><code>streambuf</code></a> object. No strings for you.</p>
<p>A <code>streambuf</code>, you say! Aha! Well you may happen to remember that there is a standard class called <a href="http://www.cplusplus.com/reference/iostream/stringbuf/"><code>std::stringbuf</code></a> which derives from <code>streambuf</code>, and you could read into that and then extract the string. The problem with this, though, is that unlike the <code>istream::get()</code> overloads that use character arrays, the overloads that use <code>streambuf</code>s conveniently leave out an optional size parameter. If you want to read from the stream with a <code>stringbuf</code>, you are obligated to read everything it has to give you, up to some delimiter. The <code>istream</code> class&#8217;s other unformatted data-reading functions, <a href="http://www.cplusplus.com/reference/iostream/istream/read/"><code>read()</code></a> and <a href="http://www.cplusplus.com/reference/iostream/istream/readsome/"><code>readsome()</code></a>, don&#8217;t give you any choice other than character arrays. So using an <code>istream</code> member function is right out.</p>
<p>What to do instead, then? We can turn to every C++ programmer&#8217;s best buddy, the iterator. <code>istream</code> objects can do more than stream extraction. Much like everything else in the C++ standard library, they have iterators. A really brute-force way to write this function is then to do this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">std::<span class="me2">string</span> extractStr<span class="br0">&#40;</span>std::<span class="me2">istream</span>&amp; in, std::<span class="me2">streamsize</span> n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; std::<span class="me2">string</span> str;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">for</span><span class="br0">&#40;</span>std::<span class="me2">istreambuf_iterator</span>&lt;char&gt; i<span class="br0">&#40;</span>in<span class="br0">&#41;</span>; in &amp;&amp; str.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &lt; n; ++i<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; str += *i;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> str;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Note the end-of-stream check in the conditional section of the for statement. Incrementing the end-of-stream iterator is not valid, so we have to check the <code>istream</code> at each iteration to make sure that it is still readable. Recall that <code>istream</code> objects can be implicitly converted to <code>bool</code> (<a href="http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.4">by means of a conversion to <code>void*</code></a>), which indicates whether or not they are still good to read from. </p>
<p>We could add a call to <a href="http://www.cplusplus.com/reference/string/string/reserve/"><code>string::reserve()</code></a> to make the above slightly more efficient, but efficiency aside, the above function is aesthetically gross. How might we make this look a bit more elegant, and be more expressive of <em>what</em> we&#8217;re trying to accomplish (initializing a string with the first <code>n</code> characters of a stream) and no so explicitly expressive of the mechanics of <em>how</em> that gets done?</p>
<p>You might remember that <code>std::string</code> has a constructor which takes two input iterators and uses them to construct the string. This is a really easy way to initialize a string with the whole contents of a stream, for example if your <code>istream</code> in is really an <a href="http://www.cplusplus.com/reference/iostream/ifstream/"><code>ifstream</code></a> and you want the entire file read into a string.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">std::<span class="me2">string</span> str<span class="br0">&#40;</span><span class="br0">&#40;</span>std::<span class="me2">istreambuf_iterator</span>&lt;char&gt;<span class="br0">&#40;</span>in<span class="br0">&#41;</span><span class="br0">&#41;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">istreambuf_iterator</span>&lt;char&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Two notes on the above: First, the parentheses around the first argument are, unfortunately, necessary. This is to prevent the parser from mis-parsing this as line a function declaration, much like how <a href="http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19">you cannot use empty parenthesis for default-constructing a variable without <code>new</code></a>. Second, the second argument, a default-constructed <a href="http://www.cplusplus.com/reference/std/iterator/istreambuf_iterator/"><code>istreambuf_iterator</code></a> is a special value which represents end-of-stream for any input stream. This specialness is why this pattern, while it works great for reading the whole stream, doesn&#8217;t work at all for reading only a fixed number of characters. What happens when you try to use this string constructor to solve the problem I initially posed?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">std::<span class="me2">string</span> str<span class="br0">&#40;</span><span class="br0">&#40;</span>std::<span class="me2">istreambuf_iterator</span>&lt;char&gt;<span class="br0">&#40;</span>in<span class="br0">&#41;</span><span class="br0">&#41;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">istreambuf_iterator</span>&lt;char&gt;<span class="br0">&#40;</span>in<span class="br0">&#41;</span> + n<span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>The compiler doesn&#8217;t like that. It will tell you that there is no <code>operator+</code> defined for <code>istream_iterator</code>s, and it will be right. Remember, <code>istream_iterator</code>s are not models of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">random-access iterators</a>. Okay, so why don&#8217;t we just then use the old standby <a href="http://www.cplusplus.com/reference/std/iterator/advance/"><code>std::advance</code></a>, even if it might be a little inefficient?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">std::<span class="me2">string</span> extractStr<span class="br0">&#40;</span>std::<span class="me2">istream</span>&amp; in, std::<span class="me2">streamsize</span> n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; std::<span class="me2">istreambuf_iterator</span>&lt;char&gt; begin<span class="br0">&#40;</span>in<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; std::<span class="me2">istreambuf_iterator</span>&lt;char&gt; end<span class="br0">&#40;</span>in<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; std::<span class="me2">advance</span><span class="br0">&#40;</span>end, n<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> std::<span class="me2">string</span><span class="br0">&#40;</span>begin, end<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>A little prettier, but unfortunately it doesn&#8217;t work. It does compile, but it will give you an empty string at best and a segmentation fault at worst. The use of <code>begin</code> after we have called <code>std::advance</code> on <code>end</code> is undefined behavior. This is because <code>istreambuf_iterator</code>s are not just not models of random-access iterators, they aren&#8217;t even models of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">forward iterators</a>. They are only models of <a href="http://www.sgi.com/tech/stl/InputIterator.html">input iterators</a>. That means that you can only move forward, and once you move forward you can never go back, even if you&#8217;ve saved a previous iterator like we did above. Input iterators only guarantee that you may pass over the range once, and that makes sense given the nature of streams.  </p>
<p>If you look around at other standard library functions that might fit the bill instead of <code>string</code>&#8216;s constructor, similar problems arise. The string object&#8217;s <a href="http://www.cplusplus.com/reference/string/string/append/"><code>append()</code></a> method requires a forward iterator. The <a href="http://www.cplusplus.com/reference/algorithm/copy/"><code>std::copy()</code></a> function can work with input iterators, but requires an explicit end iterator, which we can&#8217;t provide except for the special end-of-stream iterator. For some reason, unlike <a href="http://www.cplusplus.com/reference/algorithm/fill/"><code>std::fill()</code></a> / <a href="http://www.cplusplus.com/reference/algorithm/fill_n/"><code>std::fill_n()</code></a> and <a href="http://www.cplusplus.com/reference/algorithm/generate/"><code>std::generate()</code></a> / <a href="http://www.cplusplus.com/reference/algorithm/generate_n/"><code>std::generate_n()</code></a>, there is no such function as <code>std::copy_n()</code>. It&#8217;s almost as if the authors of the standard library are teasing us!</p>
<p>Just to spite the standards authors, here&#8217;s something clever you could do</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">std::<span class="me2">string</span> extractStr<span class="br0">&#40;</span>std::<span class="me2">istream</span>&amp; in, std::<span class="me2">streamsize</span> n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; std::<span class="me2">string</span> str;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; str.<span class="me1">resize</span><span class="br0">&#40;</span>n<span class="br0">&#41;</span>; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; in.<span class="me1">read</span><span class="br0">&#40;</span>&amp;str<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, n<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> str;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This <em>will</em> actually work, except that, strictly speaking, it is also undefined behavior. Every modern C++ compiler makes <code>std::string</code>&#8216;s internal storage contiguous, so unlike the string constructor example, this will probably work in practice. But, rather surprisingly, <code>std::string</code> is not <em>required</em> to have contiguous internal storage by the current C++ standard. This will required of <code>std::string </code>in C++0x, and so the above will be legal in C++0x, but while it is convenient, it is currently not standards-conforming.</p>
<p>Sadly, as far as I can tell, there is absolutely no standards-conforming way to write this function without raw character arrays, other than by explicitly writing out the nuts and bolts of a character-by-character iteration over the <code>istream</code> or doing something even more long-winded like writing a wrapper around <code>istreambuf_iterator</code> that returns an end-of-stream iterator after a fixed number of advances. I&#8217;ll repeat the &#8220;brute force&#8221; solution (with the small optimization included) below, and if anyone can find a more elegant way to accomplish this seemingly trivial task, please post it in the comments. It&#8217;s things like this that sometimes make me think that all the C++ nay-sayers out there might be on to something.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">std::<span class="me2">string</span> extractStr<span class="br0">&#40;</span>std::<span class="me2">istream</span>&amp; in, std::<span class="me2">streamsize</span> n<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; std::<span class="me2">string</span> str;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; str.<span class="me1">reserve</span><span class="br0">&#40;</span>n<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">for</span><span class="br0">&#40;</span>std::<span class="me2">istreambuf_iterator</span>&lt;char&gt; i<span class="br0">&#40;</span>in<span class="br0">&#41;</span>; in &amp;&amp; str.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &lt; n; ++i<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; str += *i;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> str;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This post was inspired by <a href="http://stackoverflow.com/questions/1262715/reading-stdstring-from-binary-file/">this question on StackOverflow</a>. My answer to this question in part encourages the questioner to use C++-style I/O rather than C-style I/O, but shortly after posting I realized that I did not quite know how to do what he wanted in a truly &#8220;C++ style&#8221;. I still don&#8217;t.</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/UO8wWjGZr8k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/08/islanded-in-a-stream-of-chars/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/08/islanded-in-a-stream-of-chars/</feedburner:origLink></item>
		<item>
		<title>Alas, Concepts, We Hardly Knew Ye</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/SUbs7rMJLYE/</link>
		<comments>http://nerdland.net/2009/07/alas-concepts-we-hardly-knew-ye/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 16:19:14 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Language Comparison]]></category>
		<category><![CDATA[Templates]]></category>
		<category><![CDATA[Type Systems]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=380</guid>
		<description><![CDATA[While catching up on recent postings to Lambda the Ultimate, I was rather surprised to discover that last week concepts were, by a vote of the committee members, completely removed from the C++0x draft specification. On Monday, July 13th 2009 Concepts were dramatically voted out of C++0x during the C++ standards committee meeting in Frankfurt. [...]]]></description>
			<content:encoded><![CDATA[<p>While catching up on recent postings to <a href="http://lambda-the-ultimate.org/">Lambda the Ultimate</a>, I was rather surprised to discover that last week <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1758.pdf">concepts</a> were, by a vote of the committee members, <a href="http://www.informit.com/guides/content.aspx?g=cplusplus&#038;seqNum=441">completely removed from the C++0x draft specification</a>.</p>
<blockquote><p>
On Monday, July 13th 2009 Concepts were dramatically voted out of C++0x during the C++ standards committee meeting in Frankfurt. [...] When I first heard the news, I couldn&#8217;t believe it. Concepts were supposed to be the most important addition to core C++ since 1998. Tremendous efforts and discussions were dedicated to Concepts in every committee meeting during the past five years. After dozens of official papers, tutorials and articles &#8212; all of which were dedicated to presenting and evangelizing Concepts &#8212; it seemed very unlikely that just before the finishing line, Concepts would be dropped in a dramatic voting.
</p></blockquote>
<p>C++0x is the next version of the C++ language standard. It should more properly be called C++1x, since the chances of it being released within the year are like the chances that <a href="http://blogs.zdnet.com/microsoft/?p=3433">Microsoft would contribute code to the Linux kernel</a> &#8212; er, bad example. Anyway, the point is that it&#8217;s not going to be done this year. </p>
<p>While I&#8217;ve been following the development of C++0x with interest, I haven&#8217;t been immersing myself in the implementation details. In essence, I want to know what&#8217;s coming up, but I&#8217;m not too interested in committing to memory the nuts and bolts of a specification that is likely to be revised several more times before it is finalized. I&#8217;m not totally abstaining from C++0x until it&#8217;s done. I do use some features where they are useful and already implemented, for example I am using <a href="http://msdn.microsoft.com/en-us/library/bb983026.aspx">unordered_map</a>, the standard library&#8217;s version of a hash table, in the latest iteration of <a href="http://nerdland.net/the-lottery-problem/">my work on the lottery problem</a>. However, my understanding of the majority of C++0x features has been relatively, for lack of a better word, &#8220;conceptual&#8221;.</p>
<p>The layman&#8217;s understanding that I had of the C++0x concepts feature led me to react with shock to learning of it&#8217;s removal. It sounded incredibly useful, and I didn&#8217;t see what could be such a big problem that it would have to be ripped out entirely. That changed when I read more about concepts as they (were going to) exist in C++0x.</p>
<p><span id="more-380"></span></p>
<p>According to the InformIT article that I linked above, the straw that broke the camel&#8217;s back was a paper by <a href="http://www.research.att.com/~bs/">Bjarne Stroustrup</a> entitled <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2906.pdf">&#8220;Simplifying the Use of Concepts&#8221;</a>. This paper, ironically, was intended to champion the retention of the concepts feature, and to propose solutions for some of the feature&#8217;s outstanding problems. According to that article, this paper &#8220;scared folks.&#8221; That&#8217;s a description that&#8217;s hard to interpret until you read the paper itself, which if you have any interest in C++0x or any curiosity about why concepts were removed, I strongly suggest you do. Let me give my reaction to it.</p>
<p>First, some background. The current C++ standard library already has concepts, and uses them heavily. The most easily demonstrable example of concepts in the current C++ standard library is iterators. You have <a href="http://www.sgi.com/tech/stl/ForwardIterator.html"><code>ForwardIterator</code>s</a>, <a href="http://www.sgi.com/tech/stl/BidirectionalIterator.html"><code>BidirectionaIterator</code>s</a>, <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html"><code>RandomAccessIterator</code>s</a>, etc, etc. These each specify behavior semantics and usage syntax for different kinds of iteration. For example, a <code>ForwardIterator</code> must (among other things) support operator++ and be re-usable over the same collection. Yet if you go looking for a class called <code>ForwardIterator</code> to use as a superclass of your iterator, you will find nothing.</p>
<p>The fundamental difference between the concepts currently in the standard library and the concepts that were until recently part of the C++0x draft is that the compiler is totally ignorant of concepts as they exist in C++ today. Today, concepts exist only in the documentation for the standard library; the compiler is completely ignorant of them. The only ways for a programmer to be sure that he or she has conformed to a required concept is to either read the documentation, to read the implementation code and infer the concept requirements, or to guess what the concept requirements are and to refine that guess as a result of each compiler error that arises. C++0x concepts aimed to give a language-supported and compiler-understood mechanism for defining the requirements of a concept in code.</p>
<p>Years ago, my first reaction to concepts, both in C++98 and C++0x forms, was &#8220;that&#8217;s neat, but why don&#8217;t they just use interfaces?&#8221; After all, <a href="http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf">generics in Java accomplish this rather elegantly</a> through the use of interfaces, wildcards, and wildcard binding. The answer is that interfaces are inherently an object-oriented construct, and <a href="http://www.research.att.com/~bs/oopsla.pdf">C++ is not just an object-oriented programming language</a>.</p>
<p>Sure, interface-based template restriction would work fine for classes, but not <em>requiring</em> the use of classes when it is not necessary is a fundamental part of the multi-paradigm nature of C++. In fact, even though Java is object-oriented, its interface-based generics implementation has an obvious and annoying artifact in the completely unintuitive rule that <a href="http://stackoverflow.com/questions/996135/how-are-java-generics-different-from-c-templates-why-cant-i-use-int-as-a-para">you cannot use fundamental types (such as <code>int</code>) for generic type parameters</a>. In C++, aside from wanting to avoid Java-like autoboxing, it is essential that code like this be legal:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> numbers<span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#123;</span><span class="nu0">42</span>, <span class="nu0">27</span>, <span class="nu0">117</span>, <span class="nu0">1024</span>, <span class="nu0">34</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">std::<span class="me2">sort</span><span class="br0">&#40;</span>numbers, numbers + <span class="kw3">sizeof</span><span class="br0">&#40;</span>numbers<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>This uses the templated <code>std::sort</code> function to sort a raw C-style array. The arguments to <code>std::sort</code> are RandomAccessIterators, which is a concept, not an interface. If RandomAccessIterator were an interface, then we would need either need to convert <code>numbers</code> into a random access container object (e.g. <code>std::vector</code>), or we would need to wrap the pointers that we passed to <code>std::sort</code> in iterator objects that implemented the RandomAccessIterator interface. Instead, since RandomAccessIterator is a concept, passing two <code>int*</code>s to <code>std::sort</code> works just fine with no behind-the-scenes magic, since pointers into an array support all the syntax that RandomAccessIterators must support, and they have equivalent semantics.</p>
<p>The big idea to take away from this set-up is that C++ templates differ from other similar systems (like Java generics) because they rely on <a href="http://en.wikipedia.org/wiki/Structural_type_system">structural typing</a>, rather than <a href="http://en.wikipedia.org/wiki/Nominative_type_system">nominal typing</a>. That is, template arguments are validated based on whether or not they support the required syntax (operators and/or method calls). Not only is this different from other languages, it is radically different from <em>other parts of C++</em>, such as function arguments, which are validated based on whether or not they derive from the appropriate class. Because of this, the idea of adding formal concepts to C++0x was necessarily more than just a way to bring documentation into code that the compiler could understand and enforce. <strong>Concepts in C++0x were supposed to unify structural and nominal typing in C++.</strong></p>
<p>Formal concepts are a way to give symbolic names to structural properties. Given this, a templated function or class can specify the symbolic name(s) for the collection of structural properties that it requires of its template argument(s). These names can then by looked up by the compiler, or the programmer, or the programmer&#8217;s IDE, to determine if a given type is appropriate as a template argument to that function or class. However, concepts are not nominal types, so there is no need for the type of the template argument to itself know anything about the concept that it is structurally conforming to.</p>
<p>The problem is that unifying the almost diametrically opposite notions of structural and nominal typing, of course, quite hard, which implies that formalized concepts are going to be quite difficult to define in just the right way. </p>
<p>What Bjarne addresses in his paper (linked above) is a series of problems that are caused by what the C++0x comittee had been referring to as &#8220;explicit concepts&#8221;. These are concept definitions which types must explicitly declare their support for, rather than implicitly supporting by means of providing the appropriate strucutral properties. An explicit concept is a nominal type, no two ways about it. It&#8217;s very much like an interface, except that there is a mechanism called a concept map which can be used to specify that a type supports the explict concept without requiring the type (which may not even be a class) to modify its own definition to declare support, as one would do when inheriting from an interface.</p>
<p>Concept maps have other uses, but the problem in using them in this particular way is that the extremely loose coupling between a concept map and the type that it is acting on leads to tricky problems of ambiguity and inconsistency. You can read Bjarne&#8217;s paper to understand this in more detail. Furthermore, writing concept maps is frankly a chore, and the more explicit concepts there are, the more often the average programmer is going to face the choice of put up with this annoyance or avoiding concepts altogether. </p>
<p>It becomes clear from the paper that there are several showstopper-level design bugs in the then-current C++0x draft specification of concepts. The core of Bjarne&#8217;s proposed solution is to eliminate explicit concepts altogether, and instead rely only on explicit or implicit relationships <em>between</em> concepts to resolve situations where wholly implicit concepts are inappropriate.</p>
<p>The example that Bjarne uses is the relationship between the concepts of <code>RandomAccessIterator</code> and a derivative concept called <code>ContiguousIterator</code>, which is a <code>RandomAccessIterator</code> guaranteed to use contiguously-allocated memory storing only plain data. They have exactly the same semantics, and so the compiler would have no way of knowing (without a concept map) whether or not a given iterator that matches these semantics actually conforms to the additional logical requirement of being a <code>ContiguousIterator</code>. The existing solution was to make them both explicit concepts, but this leads to a proliferation of concept maps to distinguish between the two concepts. Bjarne&#8217;s proposed fix is to make the <em>derivation</em> of <code>ContiguousIterator</code> from <code>RandomAccessIterator</code> explicit, with the effect that all types which match their common syntax are treated only as <code>RandomAccessIterators</code> unless there is a concept map stating otherwise.</p>
<p>While Bjarne&#8217;s proposed fix for this problem would work just fine, what I think is missed is this: Concepts as they were defined for C++0x were effective at capturing structural properties and assigning symbolic names to them. However, in a nominal type system, the class and interface hierarchy provides more than just symbolic names that capture a collection of method signatures. A nominal type represents a semantic contract that goes above and beyond the structure of the type. For example, a <code>Shape</code> class and a <code>Cowboy</code>, may both have a <code>draw()</code> method, but the two methods have completely different semantics. Bjarne mentions this shortcoming and dismisses it:</p>
<blockquote><p>
[W]e have lived happily with class hierarchies and templates for decades without [this issue] reaching anyone’s top 100 list of traps and pitfalls, so it is not on my top 100 list of C++ problems needing solution. One reason &#8220;accidental match&#8221; hasn&#8217;t been a major problem is that we rely on simultaneous matches of both names and types. For example, only if the <code>Cowboy</code>’s <code>draw()</code> has the same argument and return types as <code>Shape</code>’s <code>draw()</code> and the same holds for every other function in the concept/type can the problem slip past the compiler (to be caught be the simplest testing). Also, unrelated types tends to get muddled only when they are used for similarly named functions, so that overload resolution often catches the mistakes as ambiguities.
</p></blockquote>
<p>But the disturbing fact that remains is that <strong>without explicit concepts, formal concepts become unable to fully capture the notion of a semantic contract in a design-oriented manner</strong>. Explicit concepts provide a way to say &#8220;this concept has an important semantic contract&#8221;, whereas explicitly <em>derived</em> concepts provide only the much weaker and much more language-technical statement of &#8220;this concept&#8217;s semantic contract differs in some important way from that of its nominal parent&#8221;. That loss of expressiveness is what scared me when I read his paper. The two statements are similar from a language perspective (i.e. getting your program to compile), but from the more important design perspective (i.e. expressing the meaning of your code in your code) it doesn&#8217;t let you express things that you will very likely want to express.</p>
<p>Because of that deficiency, it really can&#8217;t be said that implicit concepts alone accomplish the goal of unifying structural and nominal typing. Bjarne&#8217;s &#8220;simplified&#8221; concepts are not a meaningful advancement over symbolic names for a collection of syntax requirements. Symbolic syntax naming is nice, and a mild improvement over the status quo, but it fails to fill the whole gap that concepts in C++0x were supposed to fill.</p>
<p>Furthermore, since they don&#8217;t fully capture semantic contracts, the explicit relationships between concepts that Bjarne proposes to avoid problems like the <code>ContiguousIterator</code> issue are basically just fix-ups for conflicts between concept rules and other existing C++ rules like overload resolution. This part of &#8220;simplified&#8221; concepts just doesn&#8217;t feel like an elegantly designed solution. Instead, it feels like what it is: an effort to save as much as possible of the original C++0x concept proposal while rather unceremoniously hacking off the parts of that design that led to the unacceptable explicit concept bugs.</p>
<p>Don&#8217;t get me wrong, I&#8217;m not defending explicit concepts. Aside from the bugs, I think they and their associated concept maps are a rather tedious way to express semantic contracts. And neither am I claiming to be smarter than Dr. Stroustrup or that I know a better way to express semantic contracts in concepts. What I mean to say is that in the end, I have to agree with the C++0x committee that concepts probably need some kind of fundamental redesign, and that a poorly-designed or incomplete concepts implementation which fails to truly unify structural and nominal typing would be markedly worse for the C++ language than maintaining, for the time-being at least, the status quo of having no formal concepts at all.</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/SUbs7rMJLYE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/07/alas-concepts-we-hardly-knew-ye/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/07/alas-concepts-we-hardly-knew-ye/</feedburner:origLink></item>
		<item>
		<title>Wheel Generation Success and Failure</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/OJsfBonpHNY/</link>
		<comments>http://nerdland.net/2009/07/wheel-generation-success-and-failure/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 00:34:22 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Article Announcements]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Computational Complexity]]></category>
		<category><![CDATA[Computer Science]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=372</guid>
		<description><![CDATA[In the final installment of the portion of The Lottery Problem article series that has to do with the simple greedy wheel generator, I give a brief complexity analysis of my generation algorithm, and I report on the successes and the failures of the implemented generator: On a 2.66 GHz Intel Core 2 Duo (though [...]]]></description>
			<content:encoded><![CDATA[<p>In the final installment of the portion of The Lottery Problem article series that has to do with the simple greedy wheel generator, I give a brief complexity analysis of my generation algorithm, and I report on the successes and the failures of the implemented generator:</p>
<blockquote><p>On a 2.66 GHz Intel Core 2 Duo (though only using a single core), the generator consumed a peak of 1.87 GB of RAM and ran for approximately 36 hours before returning its wheel for 3-matches in a 6-from-49 lottery. [...] The wheel that it generated for my target 6-from-49 3-match lottery was 325 tickets long. While this is in my opinion a pretty good wheel, it is not optimal.
</p></blockquote>
<p><a href="http://nerdland.net/the-lottery-problem/results-and-analysis-of-the-greedy-implementation/">You can read the whole article here</a>. The long and the short of it is that the generator is indeed as efficient as I hoped it would be, but that it is not an optimal generator, as I suspected it would not be. The above-linked article includes a link to download the code for my implementation of this generator, if you wish to download it to play around with it or try to improve upon it.</p>
<p>My hunch is still that this problem is NP-Hard, and so I do not expect that any such simple generator will consistently produce optimal wheels. However, this is not the end of my series of articles on this problem. I do have hope that I can improve the generated wheels substantially by including a heuristic function that avoids the pitfalls of my current generator. Ultimately I would like to produce a generator that produces wheels that are guaranteed to be <a href="http://en.wikipedia.org/wiki/Approximation_algorithms">within a small approximation factor of optimal</a>, or a <a href="http://en.wikipedia.org/wiki/Randomized_algorithms">randomized generator</a> that is optimal with a reasonably high probability. </p>
<p>One small confession is that while I have posted the six articles in the current Lottery Problem series over the course of about six weeks, I had in fact written the entire greedy generator and knew its results before I even began writing the articles. For the heuristic and/or randomized generator that I am considering, I have so far done little more than pondering, so it may well be more than a week or two until the next article is posted.</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/OJsfBonpHNY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/07/wheel-generation-success-and-failure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/07/wheel-generation-success-and-failure/</feedburner:origLink></item>
		<item>
		<title>Detecting C++ Libraries with Autotools</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/AHgAK1X59iI/</link>
		<comments>http://nerdland.net/2009/07/detecting-c-libraries-with-autotools/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 09:27:16 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Autotools]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=302</guid>
		<description><![CDATA[Autotools, more properly known as the GNU Build System is a set of shell script-based utilities that automate parts of the configuration and compilation process for software that is distributed as source. The autotools are, in my opinion, a bit over-complex and fragile, but they remain the most portable and standardized way of allowing software [...]]]></description>
			<content:encoded><![CDATA[<p>Autotools, more properly known as the <a href="http://en.wikipedia.org/wiki/GNU_build_system">GNU Build System</a> is a set of shell script-based utilities that automate parts of the configuration and compilation process for software that is distributed as source. The autotools are, in my opinion, a bit over-complex and fragile, but they remain the most portable and standardized way of allowing software to be compiled on  UNIX-like (and especially Linux) systems.</p>
<p>One of the parts of autotools, called autoconf, is responsible for creating the &#8220;configure script&#8221; for a source package. If you&#8217;ve ever built a piece of free software from source, you have almost certainly encountered the following venerable triumvirate of commands:</p>
<blockquote><p><code>./configure<br />
make<br />
sudo make install</code></p></blockquote>
<p>That first command runs the script that is the output of autoconf, and it is responsible for detecting the capabilities and details of the system that the software is being compiled on. It, for example, will check that a sufficiently-recent compiler is available, and what system headers are necessary for various semi-standardized functions that the program uses. One other major function of the configure script is to detect whether or not libraries that the software depends on are available on the system.</p>
<p>The library detection mechanism, however, is biased heavily towards libraries written in C. Since C is of course the <em>lingua franca</em> of the UNIX world, this is understandable. Without cajoling, it really won&#8217;t detect libraries written in anything else, unless they have C bindings, and that includes C++. What then do you do if you have a C++ library that you wish to detect using autoconf? If you&#8217;re interested in the answer to this question, you probably already know what the problem is, and here I will give two workable solutions.</p>
<p><span id="more-302"></span></p>
<p>When you want to detect a C library with autoconf, it&#8217;s easy enough. You invoke the <code>AC_CHECK_LIB</code> macro. According to <a href="http://www.gnu.org/software/hello/manual/autoconf/Libraries.html">it&#8217;s documentation</a>, it works like this:</p>
<blockquote><p>Macro: AC_CHECK_LIB (<em>library</em>,<em> function</em>, [<em>action-if-found</em>], [<em>action-if-not-found</em>], [<em>other-libraries</em>])</p>
<p>    Test whether the library <em>library</em> is available by trying to link a test program that calls function <em>function</em> with the library. function should be a function provided by the library. Use the base name of the library; e.g., to check for -lmp, use `mp&#8217; as the <em>library</em> argument. </p></blockquote>
<p>The problem should jump out at you from this. <code>AC_CHECK_LIB</code> is going to create a test program. That test program will be in C, the function you provide as the <em>function</em> argument had better be callable from C. Obviously, if your C++ library is a library consisting solely of classes, there&#8217;s nothing in there at this callable from C, since C doesn&#8217;t understand C++ classes. Worse yet, even if there are functions in your library that are not members of classes, these functions probably are <em>still</em> not callable from C. </p>
<p>The reason that even &#8220;free&#8221; (i.e. non class member) functions in C++ are not callable from C programs is because of <a href="http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B">name mangling</a>. Since C++ was originally built to compile down to C (and later to compile on its own but still link as C), the developers of the C++ language had to devise a way to cram information about function-related C++ features that C did not support into function identifiers that C allowed. For example, C++ had to have some way to differentiate <code>ClassA::foo()</code> from <code>ClassB::foo()</code> when passing off object code to the linker, given that a C-oriented linker would have no concept of classes. Even outside of class members, C++ added support for function overloading, which was similarly alien to C linkers. </p>
<p>Early C++ compilers worked around these limitations of their linkers by encoding this information into a &#8220;mangled&#8221; function name. To swipe a good example from the Wikipedia article I linked above:</p>
<blockquote><p>Consider the following two definitions of f() in a C++ program:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> &nbsp;f <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="nu0">1</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> &nbsp;f <span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span> &nbsp;<span class="br0">&#123;</span> <span class="kw1">return</span> <span class="nu0">0</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">void</span> g <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw4">int</span> i = f<span class="br0">&#40;</span><span class="br0">&#41;</span>, j = f<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>These are distinct functions, with no relation to each other apart from the name. If they were natively translated into C with no changes, the result would be an error — C does not permit two functions with the same name. The compiler therefore will encode the type information in the symbol name, the result being something resembling:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> &nbsp;__f_v <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="nu0">1</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> &nbsp;__f_i <span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span> &nbsp;<span class="br0">&#123;</span> <span class="kw1">return</span> <span class="nu0">0</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">void</span> __g_v <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw4">int</span> i = __f_v<span class="br0">&#40;</span><span class="br0">&#41;</span>, j = __f_i<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Notice that g() is mangled even though there is no conflict; name mangling applies to all symbols.</p></blockquote>
<p>These days, C++ is its own language, but this mechanic remains as a vestige of its early days. Worse, C++ compilers are notoriously inconsistent about <em>how</em> names are mangled. There is no standardized conversion between a native C++ function identifier and its mangled equivalent. </p>
<p>So getting back to autotools, this leaves us with two problems when trying to detect a C++ library using <code>AC_CHECK_LIB</code>:</p>
<ol>
<li>C++ functions, even free functions, cannot be called from C using their C++ identifiers since their names (in the library binary) will be mangled from what they were in the C++ source code.</li>
<li>Even if you knew what the mangled identifier produced by your compiler was, the mangling schema is far from standardized, so checking for the mangled identifier is extremely non-portable, defeating the whole point of using autotools.</li>
</ol>
<p>How then do we get around this? Here are two solutions:</p>
<h3>Solution One: Find (or create) a function with C calling conventions in the library</h3>
<p>This is by far the easiest method if you are also the developer of the library. It is possible (and easy) to create a free function in a C++ program that is not mangled. You do it like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">extern</span> <span class="st0">&quot;C&quot;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw4">void</span> libfoo_is_present<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This declares a function called <code>libfoo_is_present</code> that has C-style linkage. That is, the name will not be mangled whatsoever by your C++ compiler. The tradeoff is, of course, that anything within an <code>extern "C"</code> block cannot use any C++ features such as classes or overloading. That&#8217;s fine though &#8211; all we want for this purpose is a single function with a relatively unique name that we can search for in this library. Note that since autotools will try to link with the library using this function, you are going to want to give the function an implementation as well, which can be empty.</p>
<p>If you&#8217;re not in control of the library sources, you might still be in luck. Some libraries have both C and C++ bindings. The C bindings are going to have C linkage, even if the library was built with a C++ compiler. Choose some function from the C bindings to search for with the <code>AC_CHECK_LIB</code> macro. Even if a library does not have explicit C bindings, you might still be able to find a function with C linkage hidden in the library. For this, you can use the <code>nm</code> tool (if you have a static library), or the <code>readelf</code> tool if you have a dynamic library. It should be fairly simple to tell C symbols from mangled C++ symbols. For example, using <code>readelf</code> on a C library produces</p>
<blockquote><p><code>tyler@kusari /lib $ readelf --dynamic --symbols libncurses.so.5 | grep FUNC | tail<br />
   548: 000000000001e1d0    42 FUNC    GLOBAL DEFAULT   11 slk_touch<br />
   549: 0000000000015ce0    15 FUNC    GLOBAL DEFAULT   11 erase<br />
   550: 0000000000027ae0   120 FUNC    GLOBAL DEFAULT   11 _nc_free_termtype<br />
   551: 000000000002c6f0   108 FUNC    GLOBAL DEFAULT   11 _nc_outch<br />
   552: 000000000002ac50  1095 FUNC    GLOBAL DEFAULT   11 _nc_tparm_analyze<br />
   553: 0000000000028940   149 FUNC    GLOBAL DEFAULT   11 _nc_keypad<br />
   554: 0000000000014d70    15 FUNC    GLOBAL DEFAULT   11 refresh<br />
   556: 0000000000014cf0    10 FUNC    GLOBAL DEFAULT   11 touchline<br />
   558: 0000000000024c40     2 FUNC    GLOBAL DEFAULT   11 _nc_freeall<br />
   559: 0000000000012d50    73 FUNC    GLOBAL DEFAULT   11 beep</code></p></blockquote>
<p>While using it on a C++ library produces</p>
<blockquote><p><code>tyler@kusari /lib $ readelf --dynamic --symbols libboost_regex-gcc43-mt.so | grep FUNC | tail<br />
  1403: 00000000000a1f70  1498 FUNC    WEAK   DEFAULT   11 _ZN5boost9re_detail18basi<br />
  1404: 0000000000090cd0    62 FUNC    WEAK   DEFAULT   11 _ZN5boost9re_detail12perl<br />
  1405: 0000000000091f50   166 FUNC    WEAK   DEFAULT   11 _ZN5boost9re_detail19basi<br />
  1407: 0000000000072fe0    15 FUNC    WEAK   DEFAULT   11 _ZN5boost6detail17sp_coun<br />
  1408: 000000000008cbb0   381 FUNC    WEAK   DEFAULT   11 _ZN5boost9re_detail19basi<br />
  1409: 0000000000042320    75 FUNC    WEAK   DEFAULT   11 _ZN5boost9re_detail12perl<br />
  1411: 0000000000043410    15 FUNC    WEAK   DEFAULT   11 _ZN5boost6detail17sp_coun<br />
  1412: 0000000000083d80   239 FUNC    WEAK   DEFAULT   11 _ZN5boost9re_detail12perl<br />
  1414: 0000000000042680    19 FUNC    WEAK   DEFAULT   11 _ZN5boost9re_detail12perl<br />
  1415: 00000000000980a0   775 FUNC    WEAK   DEFAULT   11 _ZN5boost9re_detail12perl</code></p></blockquote>
<p>The &#8220;_ZN5&#8243; prefix being a token that designates g++ name mangling.</p>
<p>Not all C++ libraries will have C symbols, but if you find one, just use <code>AC_CHECK_LIB</code> as usual. As a real-world example, the <a href="http://code.google.com/p/googletest/">Google Unit Testing Framework</a> is a C++ library, but the <code>gtest_main</code> library that comes with it provides a <code>main()</code> function which always has C linkage. Therefore you can check for googletest like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">AC_CHECK_LIB<span class="br0">&#40;</span><span class="br0">&#91;</span>gtest_main<span class="br0">&#93;</span>, <span class="br0">&#91;</span>main<span class="br0">&#93;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#91;</span><span class="re2">HAVE_GTEST=</span><span class="nu0">1</span><span class="br0">&#93;</span> <span class="br0">&#91;</span><span class="re2">TEST_LIBS=</span><span class="st0">&quot;$TEST_LIBS -lgtest_main&quot;</span><span class="br0">&#93;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; AC_MSG_WARN<span class="br0">&#40;</span><span class="br0">&#91;</span>libgtest is not installed.<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> </div>
</li>
</ol>
</div>
<p>This checks for googletest by way of checking for libgtest_main by trying to link against its <code>main</code> function. If the library is found, it sets <code>HAVE_GTEST</code> and adds libgtest to the <code>TEST_LIBS</code> variable, and if not it emits a warning.</p>
<h3>Solution Two: Roll your own test</h3>
<p>If you are out of luck and the library source is not in your control and it has no functions with C linkage, you are not out of luck. Autoconf is quite extensible. Recall that the main issue causing this whole mess is that autoconf&#8217;s test programs are C programs. Well, that&#8217;s the default, but it doesn&#8217;t have to be that way. You have to do it by hand, but it is possible to make autoconf use a C++ test program by way of the <code>AC_LANG_PROGRAM</code> macro.</p>
<p>The semantics of this macro are a bit too lengthy for me to quote here, so just go read <a href="http://www.gnu.org/software/hello/manual/autoconf/Generating-Sources.html">the documentation</a>. In summary, this macro takes a bit of code, and wraps it in the usual boilerplate common to C and C++. That is, the code snippet you provide will be treated as the body of a <code>main</code> function. This provides a lot of flexibility. You&#8217;re no longer limited to trying to call a function. If your library has no free or static functions, that may not be possible. Instead, you might test the library by trying to instantiate one of its objects.</p>
<p>To use a similar real-world example to the previous solution, consider the <a href="http://code.google.com/p/googlemock/">Google C++ Mocking Framework</a>, which contains no symbols with C linkage. Here&#8217;s how we would construct a test for gmock, starting with the call to <code>AC_LANG_PROGRAM</code>:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">AC_LANG_PROGRAM<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="re3">#include &lt;gmock/gmock.h&gt;<span class="br0">&#93;</span>, <span class="br0">&#91;</span>testing::Cardinality dummy<span class="br0">&#93;</span><span class="br0">&#41;</span></span></div>
</li>
</ol>
</div>
<p>Simple enough. The <em>prologue</em> argument (the first argument) gives the include directive necessary for using the gmock library. The <em>body</em> argument gives a simple instantiation of a dummy object from the library. I chose the <code>Cardinality</code> class largely because it was default-constructable and therefore simple to use.</p>
<p>To make <code>AC_LANG_PROGRAM</code> act like <code>AC_CHECK_LIB</code>, you need to wrap it in <code>AC_LINK_IF_ELSE</code> like so:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">AC_LINK_IFELSE<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#91;</span>AC_LANG_PROGRAM<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="re3">#include &lt;gmock/gmock.h&gt;<span class="br0">&#93;</span>, </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#91;</span>testing::Cardinality dummy<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#93;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#91;</span><span class="re2">TEST_LIBS=</span><span class="st0">&quot;$TEST_LIBS -lgmock&quot;</span><span class="br0">&#93;</span> <span class="br0">&#91;</span><span class="re2">HAVE_GMOCK=</span><span class="nu0">1</span><span class="br0">&#93;</span>, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#91;</span>AC_MSG_WARN<span class="br0">&#40;</span><span class="br0">&#91;</span>libgmock is not installed.<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>The <code>AC_IF_ELSE</code> macro simply gives us the ability to do something if the test passed, or if it failed, much like the final two arguments to <code>AC_CHECK_LIB</code>.</p>
<p>Finally, there are two details left out. First, the above <code>AC_LINK_IF_ELSE</code> macro doesn&#8217;t know to provide <code>-lgmock</code> to the linker. In order to do that, we have to set <code>-lgmock</code> in <code>LDFLAGS</code>, and remember to restore the original <code>LDFLAGS</code> afterwords.</p>
<p>Second, we need to tell autoconf to compile this test program with a C++ compiler, or else it&#8217;s going to fail for the same reason that using <code>AC_CHECK_LIB</code> would have. This is accomplished using the <code>AC_LANG</code> macro, <a href="http://www.gnu.org/software/hello/manual/autoconf/Language-Choice.html">documented here</a>, along with documentation about how to use multiple languages in test programs in the same configure script. </p>
<p>In total, the whole test for <code>libgmock</code> looks like this: </p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">AC_LANG<span class="br0">&#40;</span>C++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re2">SAVED_LDFLAGS=</span><span class="re1">$LDFLAGS</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re2">LDFLAGS=</span><span class="st0">&quot;$LDFLAGS -lgmock&quot;</span></div>
</li>
<li class="li1">
<div class="de1">AC_LINK_IFELSE<span class="br0">&#40;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#91;</span>AC_LANG_PROGRAM<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="re3">#include &lt;gmock/gmock.h&gt;<span class="br0">&#93;</span>, </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#91;</span>testing::Cardinality dummy<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#93;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#91;</span><span class="re2">TEST_LIBS=</span><span class="st0">&quot;$TEST_LIBS -lgmock&quot;</span><span class="br0">&#93;</span> <span class="br0">&#91;</span><span class="re2">HAVE_GMOCK=</span><span class="nu0">1</span><span class="br0">&#93;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#91;</span>AC_MSG_WARN<span class="br0">&#40;</span><span class="br0">&#91;</span>libgmock is not installed.<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re2">LDFLAGS=</span><span class="re1">$SAVED_LDFLAGS</span></div>
</li>
</ol>
</div>
<p>Tada!</p>
<p>This should, however, leave you with the same question I had when I went through the process of figuring this out. Namely: <em>why in the hell does <code>AC_CHECK_LIB</code> not respect <code>AC_LANG</code> when creating its test programs?</em>. If you look at the config.log that the configure script generates, it <em>always</em> uses C programs and the C compiler to run <code>AC_CHECK_LIB</code> test, even when the language has been set to C++. If this were not so, things would be <em>much</em> simpler, except in the case where a C++ library had absolutely no free or static functions to test for.</p>
<p>If anyone knows why autoconf behaves like that, please enlighten me (or better yet, explain how to make it behave otherwise!).</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/AHgAK1X59iI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/07/detecting-c-libraries-with-autotools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/07/detecting-c-libraries-with-autotools/</feedburner:origLink></item>
		<item>
		<title>Monty Hall is Falling Down</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/TbiEELv7_CI/</link>
		<comments>http://nerdland.net/2009/06/monty-hall-is-falling-down/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 22:16:23 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Probability]]></category>
		<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=294</guid>
		<description><![CDATA[Today&#8217;s entry on Jeff Atwood&#8217;s weblog, &#8220;Coding Horror&#8221; talks about the time-honored probability puzzler known as the Monty Hall problem. If you are unfamiliar with the problem, it deals with devising the optimal strategy for playing a game that was common on the game show &#8220;Let&#8217;s Make a Deal&#8220;, starring Monty Hall, and has a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codinghorror.com/blog/archives/001278.html">Today&#8217;s entry on Jeff Atwood&#8217;s weblog, &#8220;Coding Horror&#8221;</a> talks about the time-honored probability puzzler known as <a href="http://en.wikipedia.org/wiki/Monty_Hall_problem">the Monty Hall problem</a>. If you are unfamiliar with the problem, it deals with devising the optimal strategy for playing a game that was common on the game show &#8220;<a href="http://en.wikipedia.org/wiki/Let%27s_Make_a_Deal">Let&#8217;s Make a Deal</a>&#8220;, starring <a href="http://en.wikipedia.org/wiki/Monty_Hall">Monty Hall</a>, and has a solution that is commonly perceived as unintuitive.</p>
<blockquote><p>
Suppose you&#8217;re on a game show, and you&#8217;re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what&#8217;s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, &#8220;Do you want to pick door No. 2?&#8221; Is it to your advantage to switch your choice? (<a href="http://en.wikipedia.org/wiki/Monty_Hall_problem#refWhitaker1990">Whitaker 1990</a>)
</p></blockquote>
<p>This, much like <a href="http://www.airplaneonatreadmill.com/">airplane on a treadmill</a>, and <a href="http://math.fau.edu/Richman/HTML/999.htm">0.999&#8230; == 1</a> are common topics of long, drawn-out arguments on the Internet. But what I want to talk about is not the problem itself (which has been done to death), or Jeff&#8217;s post. Rather, I want to discuss a variant problem humorously called the &#8220;Monty Fall Problem&#8221; proposed by Professor Jeffrey S. Rosenthal of the University of Toronto, which is included in <a href="http://www.probability.ca/jeff/writing/montyfall.pdf">an article titled &#8220;Monty Hall, Monty Fall, Monty Crawl&#8221;</a>, which was linked from the Coding Horror article.</p>
<p><span id="more-294"></span></p>
<p>The Monty Hall problem in its most common form has never really seemed all that unintuitive to me. Admittedly, yes, when I first heard the problem my immediate reaction was to claim that the probability of winning was 50-50, but the logic behind why that is not correct was clear when it was explained. </p>
<p>When I explain the solution to this problem to others, I use a variant of what Rosenthal refers to as &#8220;The Shaky Solution&#8221;. He gives it that derogatory name because the logic employed in that explanation is not readily generalizable to variants of the Monty Hall problem. Indeed, the main subject of his article is to present a method of reasoning that does generalize to variants. But the explanation I use (which is perfectly valid for the vanilla problem) is as follows:</p>
<blockquote><p>
In the Monty Hall problem, the option to switch is equivalent to the option of trading the contents of your original door for the contents of <em>both</em> other doors. From this it is obvious that picking two doors (switching) improves your chances of winning over picking only one.
</p></blockquote>
<p>Put another way, the choice to switch asks you to wager on whether you were right or wrong, and when you made your original selection, you had a 1/3 chance of being right.</p>
<p>So that is straightforward enough. I read Rosenthal&#8217;s article, and most of the other examples seemed similarly straightforward when they were explained. But the solution given to the &#8220;Monty Fall&#8221; problem made me pause and briefly doubt that the argument he was using was necessarily correct. The Monty Fall problem is stated as:</p>
<blockquote><p>
In this variant, once you have selected one of the three doors, the host slips on a banana peel and accidentally pushes open another door, which just happens not to contain the car. Now what are the probabilities that you will win the car if you stick with your original selection, versus if you switch to the remaining door?
</p></blockquote>
<p>And his solution, which claims that there is an equal probability of winning whether you switch or not, is given as:</p>
<blockquote><p>
In the Monty Fall problem, suppose you select Door #1, and the host then falls against Door #3. The probabilities that Door #3 happens not to contain a car, if the car is behind Door #1, #2, and #3, are respectively 1, 1, and 0. Hence, the probabilities that the car is actually behind each of these three doors are respectively 1/2, 1/2, and 0. So, your probability of winning is the same whether you stick or switch.
</p></blockquote>
<p>The question that came to mind is this: The Monty Fall problem seems to give no information beyond that which is available in the traditional Monty Hall problem. In particular, both problems seem to say only that a door other than the selected door was opened, and a goat was revealed. If the game is the same and the information is the same, then it would follow that the probabilities involved cannot possibly differ.</p>
<p>The key is, of course, that the information is <em>not</em> the same, and I will now go through the reasoning I used to convince myself that Rosenthal&#8217;s solution to the Monty Fall problem is indeed correct.</p>
<p>First, the problem statement as presented by Rosenthal elides what is actually a key piece of information. The problem says that we, the player, are operating under the assumption that Monty fell, and that the door he opened as a result of his spill was not the door that we selected, and also did not contain a car. But wait &#8211; Monty&#8217;s fall itself is implicitly described as a probabilistic event! Why are we allowed to ignore the other possibilities? </p>
<p>Let&#8217;s work this through, using Rosenthal&#8217;s own method of applying the Proportionality Principle. Rosenthal&#8217;s generalizable method for the Monty Hall problem is, in essence: <strong>For each door, calculate the probability that the described situation occurred given that the car is behind the door in question.</strong> Then, if you normalize the probabilities, the probability of winning by <em>not</em> switching is the probability that the described situation occurred given that the car was behind the door you originally selected.</p>
<p>But here, there are actually four possible &#8220;situations&#8221;. There are two binary choices associated with Monty&#8217;s fall. Either the door he opens is the player-selected door, or it is not. And either the door he opens contains the car, or it does not. In lieu of any other stipulations, I&#8217;ll make the reasonable assumption that the door that Monty falls into is chosen uniformly at random from all three available doors.</p>
<p>Then, the probability that he opens the player selected door is 1/3 (since the player selects only of three doors), and the probability that he opens the car door is also 1/3 (since the car is placed behind only one door). From this, we can draw up a table of the probability of being in any given situation after the fall:</p>
<table>
<tr>
<th>&nbsp;</th>
<th>Reveals Car</th>
<th>Reveals Goat</th>
</tr>
<tr>
<th>Opens Selected</th>
<td>1/9</td>
<td>2/9</td>
</tr>
<tr>
<th>Opens Other</th>
<td>2/9</td>
<td>4/9</td>
</tr>
</table>
<p>The values given are simply the products of the probabilities of the two events in the row and column headers. Rosenthal&#8217;s explanation is limited to the case of Opens Other / Reveals Goat, which I will abbreviate as OO/RG.</p>
<p>Now, to use Rosenthal&#8217;s method, we must determine, for each situation, the door-by-door probability that each situation occurs, given that the car is behind the door. Without loss of generality, let&#8217;s denote the door we the player choose as Door #1. If Monty falls into a door other than yours, we call that Door #3. </p>
<p>We can compute this in two steps. First, we list the situation/door combinations, and mark a 1 when the combination is <em>possible</em> and a zero when it is not.</p>
<table>
<tr>
<th>&nbsp;</th>
<th>Door 1</th>
<th>Door 2</th>
<th>Door 3</th>
</tr>
<tr>
<th>OS/RC</th>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>OS/RG</th>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<th>OO/RC</th>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<th>OO/RG</th>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
</table>
<p>This gives us a description of all possible car-placement / situation pairs. For example, the intersection of the Opens Selected / Reveals Goat situation and the Door 1 car placement has a zero because it is impossible that the selected door (Door #1 by assumption) has the car and is also opened to reveal a goat.</p>
<p>Now, if we multiply the contents of each row by the probability that the situation occurs, we get this:</p>
<table>
<tr>
<th>&nbsp;</th>
<th>Door 1</th>
<th>Door 2</th>
<th>Door 3</th>
</tr>
<tr>
<th>OS/RC</th>
<td>1/9</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>OS/RG</th>
<td>0</td>
<td>2/9</td>
<td>2/9</td>
</tr>
<tr>
<th>OO/RC</th>
<td>0</td>
<td>0</td>
<td>2/9</td>
</tr>
<tr>
<th>OO/RG</th>
<td>4/9</td>
<td>4/9</td>
<td>0</td>
</tr>
</table>
<p>And if we normalize these probabilities (so that they add to one) we get</p>
<table>
<tr>
<th>&nbsp;</th>
<th>Door 1</th>
<th>Door 2</th>
<th>Door 3</th>
</tr>
<tr>
<th>OS/RC</th>
<td>1/15</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>OS/RG</th>
<td>0</td>
<td>2/15</td>
<td>2/15</td>
</tr>
<tr>
<th>OO/RC</th>
<td>0</td>
<td>0</td>
<td>2/15</td>
</tr>
<tr>
<th>OO/RG</th>
<td>4/15</td>
<td>4/15</td>
<td>0</td>
</tr>
</table>
<p>What this table describes is the complete, original state of the Monty Fall problem <em>before</em> Monty falls. For example, there is a 2/15 chance that the car has been placed behind Door #2 and also that Monty will fall into Door #1 (the player-selected door) to reveal a goat.</p>
<p>The important point is that this table is based on <strong>zero information</strong> beyond what is known in the vanilla Monty Hall problem. It is just as if we are speculating on what might happen if Monty were to fall. Therefore, we should expect that the probabilities implied by this table should be identical to the probabilities of the orignal problem. In particular, this table should tell us that if we must commit to a strategy before the game begins, the strategy of &#8220;pick a door and stick with it&#8221; works 1/3 of the time, and &#8220;pick a door and switch&#8221; works 2/3 of the time.</p>
<p>Does it? Add up the columns to find out the probabilities of the car being behind each door.</p>
<table>
<tr>
<th>&nbsp;</th>
<th>Door 1</th>
<th>Door 2</th>
<th>Door 3</th>
</tr>
<tr>
<th>Total</th>
<td>5/15</td>
<td>6/15</td>
<td>4/15</td>
</tr>
</table>
<p>Now, recall that we &#8220;Door #1&#8243; is just a label for &#8220;the door the player selected&#8221;. According to my version of the so-called &#8220;Shaky Solution&#8221; logic, the choice of whether to switch or not amounts to the choice between door 1 or both of doors 2 and 3. If we add the probabilities (and reduce the fractions), we get:</p>
<table>
<tr>
<th>&nbsp;</th>
<th>Door 1</th>
<th>Doors 2 &#038; 3</th>
</tr>
<tr>
<th>Total</th>
<td>1/3</td>
<td>2/3</td>
</tr>
</table>
<p>Which is exactly what one would expect. We have added no information to the problem, and so we have come up with precisely the same solution, even with all that mumbo-jumbo about what might happen if Monty were to fall.</p>
<p>But the key realization is this: While the knowledge that Monty <em>will</em> fall gives you no additional information, the knowledge of exactly <em>how</em> he fell (once he falls), does indeed give you new information. In particular, <strong>knowledge of how exactly Monty fell eliminates 3 of 4 situations from the speculative probability table above</strong>.</p>
<p>From a numerical standpoint, when Monty actually does fall into Door #3 and reveals a goat, the new information that you have been given is that all car-placement / situation pairs other than Opened Other / Revealed Goat are impossible, and should have their probabilities in the full table updated to zero. When you recompute from that point, you end up with the 1/2 probability of winning by staying that Rosenthal gives.</p>
<p>From a logical standpoint, when Monty actually does fall into Door #3 and reveals a goat, the player can infer that certain placements of the car were more likely to cause the random event of Monty&#8217;s tumble to reveal exactly what it did. That constitutes new information through indirect observation. A non-rigorous analogy would be that hearing your doorbell ring constitutes new information about whether or not someone is standing outside your door. It is <em>possible</em> that it rang of its own volition, or that a child many yards away just struck your ringer with a baseball, but it is <em>more likely</em> that someone is standing there ringing it.</p>
<p>In summary, the difference between the vanilla Monty Hall problem and the Monty Fall variant is that in both problems it is known <em>a priori</em> that Monty will attempt to open a door other than your selection which contains a goat. But in the Monty Fall problem, the extra information is that Monty failed to do this in a very specific way, and that <strong>his exact method of failure is something that is strongly influenced by the position of the car that you are trying to find.</strong></p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/TbiEELv7_CI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/06/monty-hall-is-falling-down/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/06/monty-hall-is-falling-down/</feedburner:origLink></item>
		<item>
		<title>Mechanics of a Greedy Wheel Generator</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/EwYYF0DAvtQ/</link>
		<comments>http://nerdland.net/2009/06/mechanics-of-a-greedy-wheel-generator/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 02:36:59 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Article Announcements]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[Software Design]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=289</guid>
		<description><![CDATA[Today brings us, at long last, the latest installment in my article series on exploration of the Lottery Problem. In the previous article, I discussed the design decisions that were made in the greedy lottery wheel generator in order to get around the computational problems inherent in even a simple greedy generator for realistically-sized lotteries. [...]]]></description>
			<content:encoded><![CDATA[<p>Today brings us, at long last, the latest installment in my <a href="http://nerdland.net/the-lottery-problem/">article series on exploration of the Lottery Problem</a>. </p>
<blockquote><p>
In the previous article, I discussed the design decisions that were made in the greedy lottery wheel generator in order to get around the computational problems inherent in even a simple greedy generator for realistically-sized lotteries. In this article, I will walk through the mechanics of generating all tickets and matches, selecting a ticket for the wheel, and updating ticket meta-data in order to allow the next selection.
</p></blockquote>
<p><a href="http://nerdland.net/the-lottery-problem/greedy-algorithm-implementation-walkthrough/">You can read the whole article here</a>. In essence, this is a walkthrough, using text, diagrams, and pseudocode, of how exactly I went about implementing a greedy algorithm based lottery wheel generator. This article addresses my solutions to problems of time complexity, much as the previous article largely addressed the solutions to problems of space complexity.</p>
<p>The actual generator was implemented in, of course, C++. However this article contains no actual code, only pseudocode, so as to put emphasis on the algorithms and not the details. Besides, full details of any program can only really be understood by reading code itself, not descriptions of it with snippets. I have still not yet made available the source code to this generator, although that will be forthcoming. Hopefully I will have it cleaned up enough to be suitable for public consumption before the next article in the series is posted.</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/EwYYF0DAvtQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/06/mechanics-of-a-greedy-wheel-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/06/mechanics-of-a-greedy-wheel-generator/</feedburner:origLink></item>
		<item>
		<title>No Factories Necessary</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/NKJRHMy0aNw/</link>
		<comments>http://nerdland.net/2009/06/no-factories-necessary/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 12:03:57 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Templates]]></category>
		<category><![CDATA[Type Systems]]></category>
		<category><![CDATA[Update]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=271</guid>
		<description><![CDATA[I thought of a quick addendum to Monday&#8217;s article on covariant, templatized copy constructors. The end of that article said [T]here is a large caveat that you may have noticed. Due to the the inversion of the inheritance hierarchy around the cloning mixins, we have completely lost the ability to construct derived objects using anything [...]]]></description>
			<content:encoded><![CDATA[<p>I thought of a quick addendum to Monday&#8217;s article on <a href="http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/">covariant, templatized copy constructors</a>. The end of that article said</p>
<blockquote><p>
[T]here is a large caveat that you may have noticed. Due to the the inversion of the inheritance hierarchy around the cloning mixins, we have completely lost the ability to construct derived objects using anything but the default constructor. If you need to construct a <code>Derived</code> (which is really a <code>Cloneable&lt;Derived_&gt;</code>) and pass an argument to the <code>Derived_</code> constructor, you cannot.
</p></blockquote>
<p>And then I go on to suggest using a factory pattern that is aware of the quirky type hierarchy described in that post. But in fact there is a relatively elegant way around this problem without using factories.</p>
<p><span id="more-271"></span></p>
<p>If you look back at the definition of the <code>Cloneable&lt;T&gt;</code> class from Monday&#8217;s post, you will notice that its sole constructor is a &#8220;copy&#8221; constructor that is not really a copy constructor at all but an implicit conversion constructor from the wrapped type <code>T</code>. This was intentional, and intended for convenience (although I also left out a true copy constructor, which was an error). </p>
<p>Since we can implicitly convert <code>T</code> to <code>Cloneable&lt;T&gt;</code> it should be trivial enough to construct a <code>T</code> any way we like and have it magically turn into a <code>Cloneable&lt;T&gt;</code>. The tricky part is that the pattern requires that the typedef for <code>Cloneable&lt;T&gt;</code> be the meaningful name, and true name of the type <code>T</code> be somewhat obfuscated. So it becomes inelegant to refer to <code>T</code> directly.</p>
<p>But all is not lost. We can extend <code>Cloneable&lt;T&gt;</code> and <code>AbstractCloneable&lt;T&gt;</code> like this (and note that I have added the missing copy constructors).</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">template</span> &lt;typename T&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> AbstractCloneable : <span class="kw2">public</span> T</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span>:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw4">typedef</span> <span class="kw2">typename</span> T construct;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; AbstractCloneable<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; AbstractCloneable<span class="br0">&#40;</span><span class="kw4">const</span> AbstractCloneable&lt;T&gt;&amp; copy<span class="br0">&#41;</span> : T<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; AbstractCloneable<span class="br0">&#40;</span><span class="kw4">const</span> T&amp; copy<span class="br0">&#41;</span> : T<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw2">virtual</span> ~AbstractCloneable<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> AbstractCloneable&lt;T&gt;* clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">template</span> &lt;typename T&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Cloneable : <span class="kw2">virtual</span> <span class="kw2">public</span> AbstractCloneable&lt;T&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Cloneable<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; Cloneable<span class="br0">&#40;</span><span class="kw4">const</span> Cloneable&lt;T&gt;&amp; copy<span class="br0">&#41;</span> : AbstractCloneable&lt;T&gt;<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Cloneable<span class="br0">&#40;</span><span class="kw4">const</span> T&amp; copy<span class="br0">&#41;</span> : AbstractCloneable&lt;T&gt;<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> ~Cloneable<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> Cloneable&lt;T&gt;* clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="kw3">new</span> Cloneable&lt;T&gt;<span class="br0">&#40;</span>static_cast&lt;const T&amp;&gt;<span class="br0">&#40;</span>*<span class="kw3">this</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>The key is on line 5. Now, if we have a class with a non-default constructor like so</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Derived_ : <span class="kw2">public</span> Base</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Derived_<span class="br0">&#40;</span><span class="kw4">int</span> arg<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; Derived_<span class="br0">&#40;</span><span class="kw4">const</span> Derived_&amp; copy<span class="br0">&#41;</span> : Base<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">typedef</span> Cloneable&lt;Derived_&gt; Derived;</div>
</li>
</ol>
</div>
<p>We don&#8217;t need a factory to make a <code>Derived</code>. We can just do this</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Dervied d = Derived::<span class="me2">construct</span><span class="br0">&#40;</span><span class="nu0">4</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>It&#8217;s not exactly the usual constructor syntax, but you are indeed accessing the constructor directly and not through a factory method.</p>
<p>So it turns out that the idea from Monday&#8217;s post is a lot more generally applicable than I originally thought. I think I&#8217;ll start using it myself.</p>
<p><b>Edit:</b> This post originally ended here, but as commenter <a href="#comment-100">Matthias points out below</a>, this is not quite complete. In particular, calling <code>Derived::construct</code> doesn&#8217;t work because it constructs a raw <code>Derived_</code> object outside of its <code>Cloneable</code> wrapper. <code>Derived_</code> inherits from <code>AbstractCloneable&lt;Base_&gt;</code>. As the name implies, <code>AbstractCloneable&lt;Base_&gt;</code> is abstract, and outside of its wrapper, the <code>Derived_</code> object does not provide an implementation for the abstract <code>clone</code> method in <code>AbstractCloneable&lt;Base_&gt;</code>.</p>
<p>The solution is adding one more small shim, a wrapper class to be used when deriving from <code>AbstractCloneable</code> classes:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">template&lt;typename T&gt; </div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> AbstractCloneableParent : <span class="kw2">public</span> T</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">protected</span>:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; AbstractCloneableParent<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw2">typename</span> T::<span class="me2">construct</span>&amp; copy<span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; : T<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">private</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> AbstractCloneable&lt;typename T::<span class="me2">construct</span>&gt;* clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw3">new</span> Cloneable&lt;typename T::<span class="me2">construct</span>&gt;<span class="br0">&#40;</span>*<span class="kw3">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Derived_ : <span class="kw2">public</span> AbstractCloneableParent&lt;Base&gt;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Derived_<span class="br0">&#40;</span><span class="kw4">int</span> arg<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Derived_<span class="br0">&#40;</span><span class="kw4">const</span> Derived_&amp; copy<span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; : AbstractCloneableParent&lt;Base&gt;<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>(If you find the use of <code>T::construct</code> above to be confusing, you could always add a second typedef in <code>AbstractCloneable</code> that calls the base class something that seems more intuitive in this kind of circumstance.)</p>
<p>This provides an implementation of <code>clone()</code> in <code>Derived_</code> for the sole purpose of appeasing the compiler in order to allow the creation of temporary <code>Derived_</code> objects. Keep in mind that the <code>Derived_</code> class is internal, and is not intended to be used by consumers of this class hierarchy, so the only time that a raw <code>Derived_</code> object should exist is temporarily when invoking <code>Derived::construct</code> to create a <code>Derived</code> object.</p>
<p>Note that the implementation of <code>clone</code> in <code>AbstractCloneableParent</code> is private. That is because in all cases invoking this implementation is an error. It is a function called &#8220;clone&#8221; which does not clone at all, and in fact slices the object. Therefore we want to be sure that if someone ever does create a <code>Derived_</code> object that at least they will get an error if they try to call <code>clone()</code> on it, rather than having it behave in an unintuitive manner. Granted, one could still access that <code>clone()</code> implementation through a <code>Base*</code> that points to a raw <code>Derived_</code> object, but in the end there&#8217;s only so much you can do to protect people from themselves.</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/NKJRHMy0aNw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/06/no-factories-necessary/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/06/no-factories-necessary/</feedburner:origLink></item>
		<item>
		<title>I’ll Tell You When You’re Older</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/DoaNYoKUuiM/</link>
		<comments>http://nerdland.net/2009/06/ill-tell-you-when-youre-older/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 18:58:06 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Language Comparison]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=251</guid>
		<description><![CDATA[I&#8217;d like to address something that I&#8217;ll call the &#8220;I&#8217;ll tell you when you&#8217;re older&#8221; phenomenon in computer science education. This mostly has to do with programming, but there are also situations in theory education where this happens as well. Specifically here I&#8217;m going to discuss how it can affect the choice of a teaching [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to address something that I&#8217;ll call the &#8220;I&#8217;ll tell you when you&#8217;re older&#8221; phenomenon in computer science education. This mostly has to do with programming, but there are also situations in theory education where this happens as well. Specifically here I&#8217;m going to discuss how it can affect the choice of a teaching language.</p>
<p>As is often the case with something so complex and with such a storied history, programming languages have <a href="http://c-faq.com/aryptr/joke.html">quirks</a>, <a href="http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.14">gotchas</a>, and <a href="http://en.wikipedia.org/wiki/Template_metaprogramming">many layers of operation</a>. Therefore, it turns out to be extremely tempting when introducing an aspiring programmer to his or her first computer language to try as much as possible to avoid confusing the student by eliding material that isn&#8217;t relevant to the lesson at hand. Essentially, this means deflecting tangential questions, or anticipated questions, with the educational equivalent of the commonly experienced childhood situation where your parent promises to explain to you what some mature or risque term means &#8220;when you&#8217;re older.&#8221; In this sense, the instructor is requiring the student to wait until a later point in the course before explaining the meaning of a particular concept or construct.</p>
<p>In the abstract, this seems to be an advisable thing to do, but (though I certainly haven&#8217;t done formal study to back up this assertion) I suspect that it encourages the dreaded practice known as <a href="http://en.wikipedia.org/wiki/Cargo_cult_programming">cargo-cult programming</a>. Cargo-cult programming is the software engineering manifestation of an <a href="http://en.wikipedia.org/wiki/Appeal_to_authority">appeal to authority</a>. It entails the use of patterns or code fragments when one does not really understand what they do or why they are being used, simply because one has been instructed to use them, or has seen an instructor silently do so.</p>
<p>It is my belief that a programmer, be he a student or a professional, should never, ever write a line of code without understanding what it does or why they wrote it. This maxim is fundamentally incompatible with the &#8220;I&#8217;ll tell you when you&#8217;re older&#8221; educational tactic because that tactic necessarily entails that the student use something without having understood it. So this tactic should be avoided whenever possible, and it should absolutely be avoided in a student&#8217;s very first lesson.</p>
<p><span id="more-251"></span></p>
<p>Now, I&#8217;m not trying to say that simplification is wrong. For example, when introducing STL containers to a student of C++, it would be sufficient to explain that <code>vector</code> is a container whose elements must all be of one type, and that the notation <code>vector&lt;string&gt;</code> means that this particular vector will contain <code>string</code> objects. This gets across the idea of exactly what the angle-bracket notation means in the context of containers, and a student can extrapolate from that with success. One need not launch into a detailed explanation of the C++ templating system in order to provide an adequate and non-evasive explanation.</p>
<p>The key is not so much to be exhaustive but to give the student a sense of control. When I was ten years old, I experienced an epiphany regarding computers. A friend of mine came to my house, sat at my computer, and showed me how to rearrange groups in Windows 3.1&#8242;s program manager, and how to change settings in the control panel. Before that point, I felt like I was operating in an environment that I could not control &#8211; that the rules of the system were hidden from me, and that as a consequence I had to interact with the operating system in a very specific, scripted, inflexible way. By showing me how things worked, and how they were configured, and demonstrating that the operating system was truly under <em>my</em> control. </p>
<p>From then on, I had a much easier time learning how more complicated interactions with the computer worked. The explanation gave me a foundation of knowledge to build on, rather than just a script, and the feeling of control and understanding gave me confidence to experiment. So it is also with programming. A novice programmer must be comfortable in his or her language environment in order to be an effective learner.</p>
<p>As I mentioned above, a particularly crucial point where a student should be told everything up front, is a first introduction to a programming language &#8211; in particular, a first introduction to a <em>first</em> programming language.</p>
<p>Here, there is a distinct difference between which language is used to teach. Consider the &#8220;boilerplate&#8221; code that a language requires &mdash; the minimum framing code that must go before, or around, any executable code that the instructor wishes to demonstrate in order to make a complete, valid program.</p>
<p>In some languages, this is quite minimal and easy to explain. For example, in Python (or analogously in Perl, Ruby, Bash, or almost any other interpreted language), all you need is something like</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">#!/usr/bin/python</span></div>
</li>
</ol>
</div>
<p>Or in fact nothing at all if you use the interpreter interactively, or provide the source file as an argument to the interpreter. When the shebang line is needed, the explanation can be terse: &#8220;This line tells the computer which programming language interpreter should be used to run this program.&#8221; Though you may still need to explain the term <em>interpreter</em> if you have not yet already.</p>
<p>In C or in C++, you end up with something a bit more involved, like:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The return statement is not strictly necessary but in my opinion things are more confusing without it. Here, to give the student an understanding of the boilerplate code, you must explain the concept of a function. With a simple function like this, such a thing can be done with an analogy to mathematical functions with which the student is likely familiar. The only sticking points would be giving a rationale for why a function may take no arguments (which does not happen in mathematics), and perhaps explaining the concept of side-effects lest the student take the analogy with mathematical functions too literally. This level of explanation can easily be done within the prologue to a first lesson.</p>
<p>But then you get to something like Java (or similarly, C#), wherein the minimal boilerplate code is the following:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> ApplicationName </div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span> <span class="kw2">static</span> <span class="kw4">void</span> main<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a><span class="br0">&#91;</span><span class="br0">&#93;</span> args<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>Here, the requirements for fully understanding the boilerplate code balloon substantially. To fully understand this code, in addition to everything that needs to be explain in the C/C++ example, one must understand what access modifiers are, what a string is, what an array is, where the <code>args</code> parameter will come from, what a class and an object are, what a method is, and how a static method differs from a non-static method. This goes far beyond what one could, or would want to, explain as introductory material, and here the temptation to say, &#8220;Just write this around your code. Don&#8217;t worry about what it does, <strong>I&#8217;ll tell you when you&#8217;re older</strong>,&#8221; becomes very great indeed.</p>
<p>For this reason I think that Java and C# (and other purely object-oriented languages) suffer a severe handicap as first teaching languages. In order to write code in an object-oriented language and understand what it is that you are writing, one must <em>first</em> understand the concept of objects, which is a very difficult thing to explain without first teaching other programming concepts. This handicap applies not only to the boilerplate code, but extends into any introductory instruction, as so many language features revolve around objects. One must continue to put off the explanation of what objects are and why they are employed while still instructing the student to use and create them frequently.</p>
<p>These languages are nevertheless popular as starter languages because they feature automatic memory management and enforce object-oriented design. While both of these are indeed important, in my opinion they do not warrant the sacrifice of having to teach beginners that cargo-cult programming is sometimes acceptable. Besides, one can get quite far even in C++ without needing to touch manual memory management (which any serious programmer must eventually learn anyway), and object-oriented design can be taught independently of language once a sufficient familiarity with programming in general has been established.</p>
<p>In my opinion, an ideal teaching language is a <a href="http://en.wikipedia.org/wiki/Multi-paradigm_programming_language">multi-paradigm language</a> so that instruction can begin with <a href="http://en.wikipedia.org/wiki/Imperative_programming">imperative programming</a>, which is the simplest to explain in full, move on to <a href="http://en.wikipedia.org/wiki/Procedural_programming">procedural programming</a>, and then finally to <a href="http://en.wikipedia.org/wiki/Object-oriented_programming">object-oriented programming</a> and beyond, all without changing syntax. This allows knowledge to build, and encourages the student to comprehend what it is that he or she is writing, knowing that it will be built on later, rather than (as I discussed earlier) feeling like the program is not fully within his or her control.</p>
<p>Of course, there is a language that has the benefits of being multi-paradigm and memory-managed, and also has a very minimal boilerplate: Python, which I mentioned earlier. I&#8217;m curious as to why it is not more often used in formal education as an introductory language, given its excellence at being both straightforward to explain to a beginner and powerful when used by a professional. Perhaps it is a systematic bias against interpreted languages? Or maybe the faculty of Computer Science departments have all experienced one too many irritating Makefile typos and retain a vehement hatred for significant whitespace.</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/DoaNYoKUuiM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/06/ill-tell-you-when-youre-older/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/06/ill-tell-you-when-youre-older/</feedburner:origLink></item>
		<item>
		<title>Covariant, Templatized Virtual Copy Constructors</title>
		<link>http://feedproxy.google.com/~r/nerdland/~3/1F_xXvfLNFU/</link>
		<comments>http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 05:39:07 +0000</pubDate>
		<dc:creator>Tyler McHenry</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Templates]]></category>
		<category><![CDATA[Type Systems]]></category>

		<guid isPermaLink="false">http://nerdland.net/?p=212</guid>
		<description><![CDATA[One problem that programmers new to C++ often run into when they begin to write non-trivial programs is that of slicing. In object-oriented programming, a subclass often holds more information than its superclass. Thus, if we assign an instance of the subclass to a variable of type superclass, there is not enough space to store [...]]]></description>
			<content:encoded><![CDATA[<p>One problem that programmers new to C++ often run into when they begin to write non-trivial programs is that of <a href="http://en.wikipedia.org/wiki/Object_slicing">slicing</a>.</p>
<blockquote><p>
In object-oriented programming, a subclass often holds more information than its superclass. Thus, if we assign an instance of the subclass to a variable of type superclass, there is not enough space to store the extra information, and it is <strong>sliced</strong> off.
</p></blockquote>
<p>The simple way to avoid slicing is to avoid making copies. Simply pass polymorphic objects around by reference (or by pointer) rather than by value, and one never has to worry about slicing. But sometimes the need to copy is unavoidable. In particular, when the need arises to make a copy of a derived object and all you have is a base pointer, things get hairy. How do you know which derived class the pointer&#8217;s target really belongs to, in order to make a proper, deep, non-sliced copy?</p>
<p>Fortunately, this problem has a well-known solution pattern called the <a href="http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8">virtual copy constructor idiom</a>. One implements a virtual <code>clone()</code> method that makes a proper deep, non-sliced copy. There&#8217;s nothing <em>wrong</em> with this idiom, but it does entail the inelegance of having to repeat near-identical code for the <code>clone()</code> method in each derived class.</p>
<p>C++ programmers encountering this pattern for the first time often get the bright idea that this code can be centralized through the use of templates. Doing this while maintaining all the advantages of a non-templatized virtual copy constructor turns out to be much trickier than it first appears. This post will explain the problem, and how to do something that I have not yet found described on the Internet: implement the virtual copy constructor idiom using templates <strong>without sacrificing covariance</strong>.</p>
<p><span id="more-212"></span></p>
<p>First, let&#8217;s take a look at a simple, straightforward use of the virtual copy constructor idiom</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Base </div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Base<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; Base<span class="br0">&#40;</span><span class="kw4">const</span> Base&amp; copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> Base* clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Derived : <span class="kw2">public</span> Base </div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Derived<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Derived<span class="br0">&#40;</span><span class="kw4">const</span> Derived&amp; copy<span class="br0">&#41;</span> : Base<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> Derived* clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="kw3">new</span> Derived<span class="br0">&#40;</span>*<span class="kw3">this</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>(Virtual destructors, while <a href="http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7">extremely necessary in real code like this</a>, have been omitted for space). This allows a proper deep copy to be made in this situation:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">void</span> foo<span class="br0">&#40;</span><span class="kw4">const</span> Base* b<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; Base* b_copy = b-&gt;clone<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// etc</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Here, <code>b_copy</code> will point to an object of whatever <code>Base</code>-derived class <code>b</code> originally pointed to, and no information will be sliced.</p>
<p>Note something that might seem peculiar about the signature of the <code>clone()</code> method. In <code>Base</code> it is declared to return a <code>Base*</code>, but in <code>Derived</code> it is declared to return a <code>Derived*</code>. This is made possible by a feature of C++ called <a href="http://msdn.microsoft.com/en-us/magazine/cc301742.aspx">covariant return types</a>. Without these, the following would not be possible, even though it is obviously desirable.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Derived d;</div>
</li>
<li class="li1">
<div class="de1">Derived* d2 = d.<span class="me1">clone</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>So now to the question of how we implement <code>clone()</code> in a generic way that does not require its implementation to be repeated in each derived class. This can be done using a templatized <code>Cloneable</code> <a href="http://en.wikipedia.org/wiki/Mixin">mix-in class</a>.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">template</span> &lt;typename B, <span class="kw2">typename</span> T&gt; <span class="kw2">class</span> Cloneable</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> B* clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="kw3">new</span> T<span class="br0">&#40;</span>static_cast&lt;const T&amp;&gt;<span class="br0">&#40;</span>*<span class="kw3">this</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>Now, all <code>Derived</code> has to do is inherit from <code>Cloneable&lt;Base, Derived&gt;</code> in addition to <code>Base</code> directly, and the <code>clone()</code> method is automatically added</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Base </div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Base<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; Base<span class="br0">&#40;</span><span class="kw4">const</span> Base&amp; copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> Base* clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Derived : <span class="kw2">public</span> Base, <span class="kw2">public</span> Cloneable&lt;Base, Derived&gt; </div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Derived<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Derived<span class="br0">&#40;</span><span class="kw4">const</span> Derived&amp; copy<span class="br0">&#41;</span> : Base<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>You can go further by defining a purely abstract <code>ICloneable</code> interface and having <code>Base</code> and <code>Cloneable</code> both inherit virtually from it, but we&#8217;ll save on complexity and avoid that here.</p>
<p>The practice of a derived class inheriting from a templatized class where the derived class itself is one of the template arguments is known as the <a href="http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern">curiously-recurring template pattern</a>. This works great, except for one thing &#8211; we&#8217;ve lost covariancy. </p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Derived d;</div>
</li>
<li class="li1">
<div class="de1">Derived* d2 = d.<span class="me1">clone</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// Doesn&#8217;t work anymore</span></div>
</li>
<li class="li1">
<div class="de1">Derived* d2 = static_cast&lt;Derived*&gt;<span class="br0">&#40;</span>d.<span class="me1">clone</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="co1">// Works, but ugly!</span></div>
</li>
</ol>
</div>
<p>If you don&#8217;t really care about this, then the above implementation of the virtual constructor idiom is by far the cleanest solution to the slicing problem. But a lack of covariancy in the <code>clone()</code> method pretty annoying not to have in a lot of cases, so you would be right to want it back. </p>
<p>As you might guess, the obvious solution of defining the <code>Cloneable</code> mix-in as</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">template</span> &lt;typename T&gt; <span class="kw2">class</span> Cloneable</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">public</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> T* clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="kw3">new</span> T<span class="br0">&#40;</span>static_cast&lt;const T&amp;&gt;<span class="br0">&#40;</span>*<span class="kw3">this</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>doesn&#8217;t work, otherwise we would have done that in the first place. The error you will get if you try this is that the compiler will complain that it is illegal override <code>clone()</code> returning <code>Base*</code> with <code>clone()</code> returning <code>Derived*</code>. That seems odd, since we did that successfully in the very first non-templatized example. </p>
<p>The reason why this doesn&#8217;t work <a href="http://www.cpptalk.net/query-on-covariant-return-types-vt12758.html">has to do with the &#8220;timing&#8221; involved in how the curiously-recurring template pattern works</a>. In short, when we declare <code>Derived</code> to derive from <code>Cloneable&lt;Derived&gt;</code>, the compiler doesn&#8217;t necessarily know that <code>Derived</code> also derives from <code>Base</code> and so can&#8217;t be sure that covariancy between <code>Base*</code> and <code>Derived*</code> is legal. </p>
<p>In order to get around this &#8211; and this is, from what I can tell, the novel part of this post &#8211; we must instead add the <code>clone()</code> method <strong>after</strong> we have already defined a complete <code>Derived</code> class that is fully recognizable as a subclass of <code>Base</code>. We do this by inverting the mixin pattern. Instead of having a <code>Derived</code> inherit from a <code>Cloneable</code>, we instead have a <code>Cloneable</code> class inherit from a <code>Derived</code>. Or, to be more generic, we define a templatized <code>Cloneable</code> class that can inherit from any copy-constructable class.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">template</span> &lt;typename T&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> AbstractCloneable : <span class="kw2">public</span> T</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span>:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; AbstractCloneable<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; AbstractCloneable<span class="br0">&#40;</span><span class="kw4">const</span> T&amp; copy<span class="br0">&#41;</span> : T<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> ~AbstractCloneable<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> AbstractCloneable&lt;T&gt;* clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> = <span class="nu0">0</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">template</span> &lt;typename T&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Cloneable : <span class="kw2">virtual</span> <span class="kw2">public</span> AbstractCloneable&lt;T&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">public</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Cloneable<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Cloneable<span class="br0">&#40;</span><span class="kw4">const</span> T&amp; copy<span class="br0">&#41;</span> : AbstractCloneable&lt;T&gt;<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">virtual</span> ~Cloneable<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw2">virtual</span> Cloneable&lt;T&gt;* clone<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span> <span class="kw1">return</span> <span class="kw3">new</span> Cloneable&lt;T&gt;<span class="br0">&#40;</span>static_cast&lt;const T&amp;&gt;<span class="br0">&#40;</span>*<span class="kw3">this</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>The <code>AbstractCloneable</code> class exists so that we can also use this pattern on abstract base classes that cannot be directly instantiated, as the <code>clone()</code> method&#8217;s implementation requires.</p>
<p>Now, instead of having <code>Derived</code> inherit from <code>Base</code> and from <code>Cloneable&lt;Derived&gt;</code> we invert the pattern and define an intermediate <code>Derived_</code> class that inherits from <code>Base</code>, and then use a <code>typedef</code> to make <code>Cloneable&lt;Derived_&gt;</code> masquerade as <code>Derived</code>. We also do this analogously with <code>Base</code> and <code>AbstractCloneable</code>.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Base_</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">protected</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Base_<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; Base_<span class="br0">&#40;</span><span class="kw4">const</span> Base_&amp; copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">typedef</span> AbstractCloneable&lt;Base_&gt; Base;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">class</span> Derived_ : <span class="kw2">public</span> Base</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">protected</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Derived_<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Derived_<span class="br0">&#40;</span><span class="kw4">const</span> Derived_&amp; copy<span class="br0">&#41;</span> : Base<span class="br0">&#40;</span>copy<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">typedef</span> Cloneable&lt;Derived_&gt; Derived;</div>
</li>
</ol>
</div>
<p>If you don&#8217;t like the appended-underscore convention, you can use whatever convention of your own you like, or even a separate namespace.</p>
<p>Covariancy is accepted by the compiler in this case because the initially-declared return type of <code>clone()</code> is <code>AbstractCloneable&lt;Base_&gt;</code>, and the return types of all overrides are types that are fully and unambiguously defined to be subclasses of <code>AbstractCloneable&lt;Base_&gt;</code> before being used as template arguments.</p>
<p>This implementation conquers the problem we set out to solve: we now have a way to add virtual copy constructors to a class hierarchy while avoiding re-implementing the <code>clone()</code> method for each derived class, and without sacrificing covariancy.  All you have to do is add a typedef after each derived class. This is quite convenient, assuming you don&#8217;t mind too much the slight obfuscation of class names involved in using this method. With the above pattern, all of the following work</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Derived d;</div>
</li>
<li class="li1">
<div class="de1">Derived* d2 = d.<span class="me1">clone</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">Base* b_d = &amp;d;</div>
</li>
<li class="li1">
<div class="de1">Base* b_d2 = b_d-&gt;clone<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">Base* b_d3 = d.<span class="me1">clone</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p><strong>However, there is a large caveat</strong> that you may have noticed. Due to the the inversion of the inheritance hierarchy around the cloning mixins, we have completely lost the ability to construct derived objects using anything but the default constructor. If you need to construct a <code>Derived</code> (which is really a <code>Cloneable&lt;Derived_&gt;</code>) and pass an argument to the <code>Derived_</code> constructor, you cannot. Therefore, this pattern is <strong>completely inappropriate</strong> unless you are using a <a href="http://en.wikipedia.org/wiki/Factory_method_pattern">factory pattern</a> to construct your derived objects, and the factory is aware of the quirky type hierarchy. Fortunately, using a factory pattern is quite common in cases where one would run into this problem, so this restriction does not entirely defeat the point of this exercise. <em>[<strong>Ed:</strong> you may want to read <a href="http://nerdland.net/2009/06/no-factories-necessary/">the addendum to this article</a> that I posted, which further addresses this issue.]</em></p>
<p>Whether this new pattern is useful or not is debatable. It adds a fair bit of complexity and obfuscation in order to eliminate a relatively small amount of code duplication. It may be more useful if the code necessary to clone an object were for some reason more complex than simply calling a copy constructor, but I cannot immediately think of a situation in which this might be the case. Nevertheless, the above demonstrates that covariant, templatized virtual copy constructors are at least <em>possible</em> in C++ under a not-unreasonably-restrictive set of circumstances.</p>
<img src="http://feeds.feedburner.com/~r/nerdland/~4/1F_xXvfLNFU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://nerdland.net/2009/06/covariant-templatized-virtual-copy-constructors/</feedburner:origLink></item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 27/45 queries in 0.141 seconds using disk

Served from: nerdland.net @ 2010-09-08 16:29:00 -->
