Harman Patil (Editor)

Quine (computing)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Quine (computing) httpsuploadwikimediaorgwikipediacommonsthu

A quine is a non-empty computer program which 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".

Contents

A quine is a fixed point of an execution environment, when the execution environment is viewed as a function. 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.

The name "quine" was coined by Douglas Hofstadter, in his popular science book Gödel, Escher, Bach: An Eternal Golden Braid, in the 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.

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.

History

The idea of self-reproducing automata came from the dawn of computing, if not before. John von Neumann himself theorized about them. 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. This program appears below:

%BEGIN !THIS IS A SELF-REPRODUCING PROGRAM %ROUTINESPEC R R PRINT SYMBOL(39) R PRINT SYMBOL(39) NEWLINE %CAPTION %END~ %CAPTION %ENDOFPROGRAM~ %ROUTINE R %PRINTTEXT ' %BEGIN !THIS IS A SELF-REPRODUCING PROGRAM %ROUTINESPEC R R PRINT SYMBOL(39) R PRINT SYMBOL(39) NEWLINE %CAPTION %END~ %CAPTION %ENDOFPROGRAM~ %ROUTINE R %PRINTTEXT ' %END %ENDOFPROGRAM

The "download source" requirement of the Affero General Public License is based on the idea of a quine.

Examples

The following Java code demonstrates the basic structure of a quine.

The source code contains a string array of itself, which is output twice, once inside quotation marks.

The following example is in Javascool.

The same idea is used in SQL quine:

A very concise quine with the same basic structure can be written in Lua:

And in Python:

A JavaScript example:

In R:

In Rust: https://play.rust-lang.org/?gist=538739d5b3008dd6edbdc8e2022abb47

In Go: http://play.golang.org/p/pVBds0oHrO

Quines can take advantage of eval. For example, this Ruby quine:

"Cheating" quines

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:

Nor this succinct use of the Shebang:

The above also applies to this JavaScript code:

In PHP:

A program in the joke language HQ9+ is a quine if and only if the source code consists only of zero or more '+' characters and a single 'Q' character (the 'Q' command prints a quine and '+' prints nothing):

++Q++++++

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". Ignoring the restriction that quines be non-empty, there are many examples of programming languages where an empty program is valid (such as C). Such programs generally do nothing, in effect, reproducing the program.

Ouroboros programs

The quine concept can be extended to multiple levels or recursion, originating what has been called "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 → … → Rexx (100 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. (Note that 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:

  • When run, is a quine in language X.
  • When supplied with a user-defined command line argument, would print a second program in language Y.
  • Given the second program in language Y, when run normally, would also be a quine in language Y.
  • Given the second program in language Y, and supplied with a user-defined command line argument, would produce the original program in language X.
  • A biquine could then be seen as a set of two programs, both of which are able to print either of the two, depending on the command line argument supplied.

    Theoretically, there is no limit on the number of languages in a multiquine, a 5-part multiquine (or pentaquine) has been produced with Python, Perl, C, NewLISP, and F# and there is also a 25-language multiquine.

    Radiation-hardened

    A radiation-hardened quine is a quine that can have any single character removed and still produce the original program with no missing character. Of necessity, such quines are much more convoluted than ordinary quines, as is seen by the following example in Ruby:

    References

    Quine (computing) Wikipedia