Programs that print themselves — the ultimate self-reference
A quine is a program that outputs its own source code — without reading any files. How can code describe itself? It's like a sentence that tells you exactly what it says, word for word. This should be impossible, yet quines exist in every programming language!
(function q(){console.log('('+q+')()');})();
Click "Run" to see output
s='s=%r;print(s%%s)';print(s%s)
Click "Run" to see output
(empty file)
(empty output)
Write your own quine or experiment with templates. Step through execution to see how self-reference works!
Imagine trying to write a sentence that describes exactly what it says. Not just the meaning — the actual characters. Every quote mark, every space, every letter. If you try, you hit a wall: to describe quotes, you need quotes around the quote marks, and to describe those quotes, you need more quotes...
This is the quine paradox. It seems like infinite regression should make self-description impossible.
yields falsehood when preceded by its quotation.
— Quine's Paradox, the linguistic inspiration for computational quines
In 1938, logician Stephen Kleene proved something remarkable: in any sufficiently powerful computational system, self-replication is always possible. This is the Recursion Theorem, and it guarantees quines exist in every Turing-complete language.
The trick has two parts:
Think of it like DNA. The DATA is the genetic information — a complete blueprint. The CODE is the cellular machinery that reads the blueprint and builds a new cell (including copying the DNA itself).
Quines aren't just clever puzzles. They illuminate deep connections:
Gödel's Incompleteness Theorem: Gödel's proof works by constructing a mathematical statement that "talks about itself." The same diagonalization technique that makes quines possible also makes formal systems fundamentally incomplete.
Computer Viruses: Self-replicating code is the basis of all computer viruses. Understanding quines helps us understand how code can spread autonomously.
The Origin of Life: How did the first self-replicating molecule arise? The quine problem — bootstrap self-reference — is the same puzzle faced by primordial chemistry.
"The quine is a fixed point of the execution environment, when viewed as a function transforming programs into their outputs." — Mathematical definition
Intron Quines: Quines where you can insert arbitrary "junk" code that doesn't affect the output — like biological introns in DNA.
Polyglot Quines: Programs that are valid in multiple languages and produce their own source code in all of them!
Relay Quines: Program A outputs Program B, which outputs Program A. Self-reference across time.
Radiation-Hardened Quines: Quines that still work even if you delete any single character. Robustness through redundancy.
Douglas Hofstadter, in Gödel, Escher, Bach, argues that quines reveal something about consciousness itself. We are, in a sense, biological quines — systems that model themselves, creating the strange loop of self-awareness.
The fact that programs can describe themselves, that mathematics can reference itself, that minds can think about thinking — these are all instances of the same deep pattern. The quine is the simplest possible example of this self-referential magic.