The Quine

A quine is a program that takes no input and prints out an exact copy of its own source code. The challenge is that the program may not cheat by reading its own source file from disk or otherwise inspecting itself from the outside. It must contain, somewhere in its text, a description of itself, and then use that description twice: once as the data to print and once as the code that does the printing. This double use of a single string is what the esoteric-programming community’s reference calls the heart of every self-reproducing program.

The trick that makes quines work is to write a piece of text that functions simultaneously as code and as data describing that code. The program holds a string, outputs the string in a literal quoted form, and then outputs the same string interpreted as instructions, so that the two halves reconstruct the whole. This is the same self-reference at the core of Kleene’s recursion theorem and the diagonal arguments Godel used, which guarantee that for any sufficiently powerful programming system a self-reproducing program must exist. Writing a quine is, in effect, a concrete and playful proof of that abstract result.

The name comes from Douglas Hofstadter, who in his 1979 book Godel, Escher, Bach honored the philosopher Willard Van Orman Quine and his study of self-reference and indirect self-reproduction. The word stuck, and constructing a quine became a rite of passage for new programming languages, since the ability to write one is a small but real test of a language’s expressiveness.

Quines are more than a parlor trick. Ken Thompson built one as the first stage of his 1984 Turing Award lecture, Reflections on Trusting Trust, where a self-reproducing program is the seed of a self-perpetuating compiler backdoor. There he writes that the construction of such a program is “an exercise” and walks through it precisely because the idea of code that reproduces itself is the foundation for code that can hide itself. The humble quine thus connects recreational programming, the deep theory of computation, and one of the most unsettling results in computer security.