← Back to Paradoxes

Quines

Programs that print themselves — the ultimate self-reference

The Paradox

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!

JavaScript Quine JavaScript

Source Code
(function q(){console.log('('+q+')()');})();
Output
Click "Run" to see output
The function q prints itself by combining the string '('+q+')' (which JavaScript converts to the function's source) with '()' to create the complete self-invoking call.

Python Quine Python

Source Code
s='s=%r;print(s%%s)';print(s%s)
Output
Click "Run" to see output
The string s contains the template with %r (repr). When s%s runs, it substitutes s into itself, quotes and all!

The Shortest Quine Empty

Source Code
(empty file)
Output
(empty output)
Technically, an empty program that produces empty output is a quine! But this "trivial quine" is usually disallowed — the challenge is to write non-empty quines.

The Quine Structure Visualized

Every quine has two parts: DATA (the recipe) and CODE (the chef). The code uses the data to reconstruct both itself AND the data!

Interactive Quine Laboratory

Write your own quine or experiment with templates. Step through execution to see how self-reference works!

Click "Run" to execute your code...
Enter code and click Run to check if it's a quine

The Bootstrap Problem

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"

yields falsehood when preceded by its quotation.

— Quine's Paradox, the linguistic inspiration for computational quines

The Kleene Magic Trick

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:

QUINE = CODE(DATA)
where DATA = description of CODE

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).

Why This Matters

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

Variations and Extensions

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.

The Philosophical Depth

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.

Sources & Further Reading