Quine is a self-replicating program that reconstructs its own source code during execution without any external input. It is a direct manifestation of "fixed points" in computability theory, demonstrating how code can reproduce itself like a living organism.
The notion of a quine (self-reproducing program) comes from computability theory and is named after the philosopher and logician Willard Van Orman Quine (1908–2000).
The term “quine” was popularized by Douglas Hofstadter in Gödel, Escher, Bach to describe programs that output their own source code.
This repository systematically explores different implementations and variants of Quines, from minimal Python examples to multilingual Ouroboros chains.
Quines are not ad‑hoc tricks; their existence follows directly from Kleene’s Recursion Theorem (a fixed‑point theorem) in computability theory.
Roughly speaking, it guarantees that in any Turing‑complete language there exists a program that can reproduce its own description.
A typical quine can be viewed as having two conceptual parts:
- Code part – the logic that prints.
- Data part – a textual representation of (part of) the program itself, usually stored as a string.
Execution pattern:
Use the data part to reconstruct and print the code part, while also printing the data itself.
Classic structural pattern:
Program = Data + code(Data → "Data + code(Data)")
_='_=%r;print(_%%_)';print(_%_)#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Python 3 Quine
"""
s='s=%r;print(s%%s)';print(s%s)#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
s="s=%r;print(s%%s)"
print(s%s)The most direct way to verify a quine is to redirect its output and compare it to its own source file.
# Standard Python 3 implementation
python3 classic/quine.py | diff - classic/quine.py
# Minimal version
python3 classic/quine_short.py | diff - classic/quine_short.py
# If diff produces no output, the quine is valid.We provide a demo script that runs and checks multiple quine variants:
python3 demo.pyThis project also experiments with an “engineering‑oriented” quine that introduces configuration and plugin mechanisms:
python3 enhanced_quine.pyThis repository is packed with content. We recommend the following path:
-
👀 Start Here (Level 0):
- Run
python3 demo.pyto see "programs printing themselves" in action. - Read the "Core Principles" section in this README.
- Run
-
👶 Beginner (Level 1):
- Read "Level 1" and "Level 2" in EXAMPLES.md to understand the basic Code + Data structure.
- Try running
classic/quine.pyand modifying it to see what happens.
-
🧑💻 Intermediate (Level 2):
- Read THEORY.md to learn about Kleene's Recursion Theorem.
- Complete the first 3 challenges in CHALLENGES.md.
- Experiment with
variants/iterative_quine.py(A→B→A cycle).
-
🚀 Advanced (Level 3):
- Explore
variants/ouroboros/and use the generator to create your own N-node self-referential chains. - Study
variants/multiquine.pyto understand polyglot techniques. - Use the validators and visualizers in
tools/for your research.
- Explore
This repository is intended as a quine lab, not just a code snippet collection:
- Multi‑language implementations: Classic quines in Python, C, Go, Java, Rust, JavaScript, etc.
- Variant exploration: Iterative quines (A→B→A), Ouroboros chains, multilingual forms, and more.
- Engineering experiments: Configuration management via
config.jsonand a simple plugin system underplugins/(seeenhanced_quine.py). - Quality assurance: Automated tests in
tests_new/and CI/CD configuration to keep self‑reproduction strict and reliable.
Quine/
├── README.md # Chinese main README
├── README_EN.md # This English README
├── demo.py # Integrated demonstration script
├── enhanced_quine.py # Enhanced quine with config/plugins
├── config.json # Configuration file
├── classic/ # Classic implementations (Python, C, etc.)
├── variants/ # Variants (iterative, Ouroboros, etc.)
├── generators/ # Quine generators
├── artistic/ # Artistic quines (ASCII art, QR code, etc.)
├── esoteric/ # Esoteric language quines (Brainfuck, ...)
├── plugins/ # Plugin system experiments
├── tests_new/ # Automated test suite
└── tools/ # Utilities (validator, size optimizer, visualizer)
- Locations:
classic/,generators/,tests/,tools/,variants/,artistic/ - Highlights: Modern syntax, type hints, f‑strings.
- Usage:
python3 <file>
- Location:
python2/ - Note: Python 2 reached end‑of‑life in 2020; kept here purely for historical/compatibility reasons.
- Usage:
python python2/<file>
Program A outputs program B, B outputs C, C outputs A, forming a cycle.
A single source file is interpreted correctly as a quine in multiple programming languages.
Program A outputs the source of B, B outputs C, …, and the last program outputs A, forming a closed “Ouroboros” loop.
The repository includes a small toolkit around Ouroboros chains, so you can move from the concept to concrete, verifiable experiments:
-
Generate Python chains of arbitrary length
Invariants/ouroboros/, use the generator to create an N‑node cycle:# Generate a 3‑node chain: py_chain_0.py, py_chain_1.py, py_chain_2.py python variants/ouroboros/make_py_chain.py 3 -
Automatically validate the closed loop
Use the validator to check that each node prints the exact source of the next node:python tools/ouroboros_validator.py
-
Visualize the chain structure
Get both a text view and a Graphviz DOT description:python tools/ouroboros_visualizer.py
Save the DOT output and render it with Graphviz, for example:
python tools/ouroboros_visualizer.py > chain.dot dot -Tpng chain.dot -o chain.png
For the theoretical background and detailed walkthroughs, see:
- Level 5 “Ouroboros chain” and the advanced example section in
EXAMPLES.md - Section 5.3 “Ouroboros chains and multi‑step fixed points” in
THEORY.md
The repository also includes a series of quine‑related programming challenges:
- Shortest quine – Minimize character count in a given language.
- Iterative quine – Build cycles of length ≥ 2.
- Multiquine – Make one file act as a quine in multiple languages.
- Radiation‑hardened quine – Error‑tolerant or noise‑resistant variants.
See CHALLENGES.md for the full list and details.
To support deeper understanding, we provide several documents:
| File | Description |
|---|---|
| THEORY.md | Theoretical foundations (fixed‑point theorems, Kleene’s recursion theorem, etc.) |
| EXAMPLES.md | Worked examples of quines in various languages |
| CHALLENGES.md | 25+ quine‑related coding challenges |
| FAQ.md | Frequently asked questions about definitions, tricks, and edge cases |
| PROJECT_SUMMARY.md | High‑level summary of the repository structure and content |
| FIXES_REPORT.md | Historical bug‑fix and change notes |
MIT License
"Yields falsehood when preceded by its quotation" yields falsehood when preceded by its quotation.
"‘在其引用之后产生谬误’在其引用之后产生谬误。"
-- Quine's Paradox (奎因悖论)