ArticleslgStudy

computer science

Quine (computing)

Quine (computing) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Quine (computing) rather than just read about it. In short: A quine ( KWYNE) is a computer program that takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying programs".

Quine (computing) — main illustration
Quine (computing) — illustration

Key takeaways

  • Quine (computing) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Quine (computing) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Quine (computing) from memory before moving on to harder problems.

Reference excerpt

A quine ( KWYNE) is a computer program that takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying programs". A quine is a fixed point of an execution environment, when that environment is viewed as a function transforming programs into their outputs. Quines are possible in any Turing-complete programming language, as a direct consequence of Kleene's recursion theorem. For amusement, programmers sometimes attempt to develop the shortest possible quine in any given programming language.

Name The name "quine" was coined by Douglas Hofstadter, in his popular 1979 science book Gödel, Escher, Bach, in honor of philosopher Willard Van Orman Quine (1908–2000), who made an extensive study of indirect self-reference, and in particular for the following paradox-producing expression, known as Quine's paradox:

"Yields falsehood when preceded by its quotation" yields falsehood when preceded by its quotation.

History John von Neumann theorized about self-reproducing automata in the 1940s. Later, Paul Bratley and Jean Millo's article "Computer Recreations: Self-Reproducing Automata" discussed them in 1972. Bratley first became interested in self-reproducing programs after seeing the first known such program written in Atlas Autocode at Edinburgh in the 1960s by the University of Edinburgh lecturer and researcher Hamish Dewar. The "download source" requirement of the GNU Affero General Public License is based on the idea of a quine.

Examples

Shortest proper quine The shortest non-trivial quine in a major programming language (a "proper" quine) known as of 2020 is the 21 character JavaScript ES6 program ($=_=>`($=${$})()`)().

Constructive quines In general, the method used to create a quine in any programming language is to have, within the program, two pieces: code used to do the actual printing and data that represents the textual form of the code. The code functions by using the data to print the code (which makes sense since the data represents the textual form of the code), but it also uses the data, processed in a simple way, to print the textual representation of the data itself. Here are three small examples in Python3:

The following classic Java code (Java 1.5) demonstrates the basic structure of a quine.

The source code contains a string array of itself, which is output twice, once inside quotation marks. This code was adapted from an original post from c2.com, where the author, Jason Wilson, posted it as a minimalistic version of a Quine, without Java comments. Following the introduction of the text blocks feature in Java 15, a more readable and simpler version is possible:

This is an example of a modern version of Java 25 code:

The code above can be executed by simply dropping its content in any file with the .java extension, such as "App.java", and then executed with "java App.java".

The same idea is used in the following SQL quine:

Eval quines Some programming languages have the ability to evaluate a string as a program. Quines can take advantage of this feature. For example, this Ruby quine:

Lua can do:

In Python 3.8:

"Cheating" quines

Self-evaluation In many functional languages, including Scheme and other Lisps, and interactive languages such as APL, numbers are self-evaluating. In TI-BASIC, if the last line of a program returns a value, the returned value is displayed on the screen. Therefore, in such languages a program consisting of only a single digit results in a 1-byte quine. Since such code does not construct itself, this is often considered cheating.

Empty quines In some languages, particularly scripting languages, an empty source file is a fixed point of the language, being a valid program that produces no output. Such an empty program, submitted as "the world's smallest self reproducing program", once won the "worst abuse of the rules" prize in the International Obfuscated C Code Contest. The program was not valid C (lacking a main() function) and was not actually compiled, but came with a Makefile which used cp to copy the empty file into another file, which would be executed as a shell script to print nothing.

Source code inspection Quines, per definition, cannot receive any form of input, including reading a file, which means a quine is considered to be "cheating" if it looks at its own source code. The following shell script is not a quine:

A shorter variant, exploiting the behaviour of shebang directives:

Other questionable techniques include making use of compiler messages; for example, in the GW-BASIC environment, entering "Syntax Error" will cause the interpreter to respond with "Syntax Error". Quine code can also be outputted visually, for example it's used to visualize the neutral zone in Yars' Revenge, along with syntactic saccharin, to obfuscate the source code.

Ouroboros programs The quine concept can be extended to multiple levels of recursion, giving rise to "ouroboros programs", or quine-relays. This should not be confused with multiquines.

Example This Java program outputs the source for a C++ program that outputs the original Java code.

Such programs have been produced with various cycle lengths:

Haskell → Python → Ruby Python → Bash → Perl C → Haskell → Python → Perl Haskell → Perl → Python → Ruby → C → Java Ruby → Java → C# → Python C → C++ → Ruby → Python → PHP → Perl Ruby → Python → Perl → Lua → OCaml → Haskell → C → Java → Brainfuck → Whitespace → Unlambda Ruby → Scala → Scheme → Scilab → Shell (bash) → S-Lang → Smalltalk → Squirrel3 → Standard ML → ... → Rexx (128 (and formerly 50) programming languages)

Multiquines David Madore, creator of Unlambda, describes multiquines as follows:

"A multiquine is a set of r different programs (in r different languages – without this condition we could take them all equal to a single quine), each of which is able to print any of the r programs (including itself) according to the command line argument it is passed. (Cheating is not allowed: the command line arguments must not be too long – passing the full text of a program is considered cheating)."

A multiquine consisting of 2 languages (or biquine) would be a program which:

… excerpt ends here. Continue reading the full article.

Illustrations

Quine (computing): A quine's output is exactly the same as its source code.
A quine's output is exactly the same as its source code.

Worked examples

Example 1 — a first encounter with Quine (computing)

Start with the simplest possible case. Write down what Quine (computing) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Quine (computing) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Quine (computing) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Quine (computing)

In research
Quine (computing) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Quine (computing) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Quine (computing) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer programming folklore, Self-replication, Source code, so understanding it makes those chapters shorter.
In everyday life
Look for Quine (computing) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Quine (computing) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Quine (computing) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Quine (computing) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Quine (computing) in simple terms?

A quine ( KWYNE) is a computer program that takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying…

Why does Quine (computing) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Quine (computing)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Quine (computing).

Tags

  • Computer programming folklore
  • Self-replication
  • Source code
  • Test items in computer languages
  • Willard Van Orman Quine

Keep exploring