esolang
Learn · Ideas134 languages show it

Quine

A program whose only output is its own source code, written without peeking at the file.

●●○ moderate · where to see it

The idea

A quine prints itself. The name comes from Douglas Hofstadter's Gödel, Escher, Bach (1979), after the philosopher W. V. O. Quine, who studied sentences that talk about themselves.

Read the story

The rule that makes it a puzzle is no cheating. A quine may not open its own file or ask the interpreter for its text. It has to contain a description of itself and rebuild the whole from it.

That sounds impossible at first. If the program contains its own text, that text must contain the text again, and so on forever. The trick is to break the loop with two halves that describe each other.

How it works

Split the program into two parts.

Split the program into two parts.

  • Data. A string that holds the code part, as text.
  • Code. Instructions that print the data twice, once quoted (as the string literal) and once raw (as the code it describes).

The data never has to contain itself. It only contains the code, and the code knows how to print a quoted copy of the data. So each half supplies what the other is missing.

A two-line Python quine.

s = 's = %r\nprint(s %% s)'
print(s % s)

s is the data. %r inserts s in quoted form (with the quotes and the \n escape), and %% becomes a single %. So s % s is exactly line 1 followed by line 2. The output matches the file byte for byte.

python quine.py | diff --strip-trailing-cr quine.py -    # no output: identical

The same shape shows up in every language. Only the "print quoted" step changes.

LanguageHow the data gets quoted
Python%r / repr
Underloada wraps the top of the stack in parentheses
BrainfuckThe data is a run of cell values; the code prints it once as the + and > instructions that rebuild it, then decoded as the code's characters

Underload's classic quine, (:aSS):aSS, is the trick at its barest. Traced step by step.

(:aSS)   push the string :aSS          stack: [:aSS]
:        duplicate it                  stack: [:aSS, :aSS]
a        wrap the top in parentheses   stack: [:aSS, (:aSS)]
S        print the top                 output: (:aSS)
S        print the next one            output: (:aSS):aSS

Data, then the code that prints the data quoted and unquoted. Ten characters.

Step by step in Underload

Underload’s quine

Expected output

(:aSS):aSS

Output


  • (:aSS) pushes the text :aSS.
  • : copies it. a wraps the top copy into (:aSS).
  • S prints (:aSS), and the next S prints :aSS. Together they are the program.

Side by side

A kernel quine found by search

The same shape in kernel tokens. 91 32 CAT EMIT prints [ and a space; DUP 32 CAT EMIT prints the quoted text; 93 32 CAT EMIT prints ]; the last EMIT prints the text again.

Expected output

[ 91 32 CAT EMIT DUP 32 CAT EMIT 93 32 CAT EMIT EMIT ] 91 32 CAT EMIT DUP 32 CAT EMIT 93 32 CAT EMIT EMIT

Output


  • Found by a beam search over P in [ P ] P.

HQ9+ cheats

HQ9+ has a command Q that prints the program. It is a quine by decree, not by construction.

Expected output

Q

Output


What a quine search finds

A search over 88 languages found 425 quines in 85 of them, in the kernel, Underload and evolved dialects. Every quine it found has the shape [ P ] P. A free search over raw text found none.

  • brainfuck, none after 154,098 programs. A brainfuck quine exists but is long; the search tried every text up to 6 characters.
  • Deadfish, none after 97,655 programs. None can exist, because only o prints, and it prints digits and newlines, never the letter o.
  • An evolved dialect’s quine uses the dialect’s own words for printing. See dialects and shibboleths.

Try this

  1. Change the Python quine so it prints itself and a comment line at the end, and is still a quine. (The comment must be inside the data too.)
  2. Write an Underload program that prints itself twice. Is it a quine? Why not?
  3. Hard: a quine relay. Write a Python program that prints a JavaScript program that prints the original Python program.

Seen in

  • Underload. The ten-character quine above; a quine is almost natural here.
  • Befunge. The playfield wraps around, so a single " can sweep the whole line onto the stack as data. It can also read its own grid with g, which purists call cheating.
  • Brainfuck. Quines exist but are long, because the data must first be spelled out as cell values.
  • HQ9+. The Q command prints the program. A joke about how cheap quines are when the language hands over the source.

Further

Where to see it