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.
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.1 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.2
In general, the method used to create a quine in any programming language is to have, within the program, two pieces: (a) code used to do the actual printing and (b) 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 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.
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.3
Thanks to new text blocks feature in Java 15 (or newer), a more readable and simpler version is possible:4
The same idea is used in the following SQL quine:
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:
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.
In some languages, particularly scripting languages but also C, an empty source file is a fixed point of the language, being a valid program that produces no output.5 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.6 The program was not actually compiled, but used cp to copy the file into another file, which could be executed to print nothing.7
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.
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.
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:
David Madore, creator of Unlambda, describes multiquines as follows:17
"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:
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#18 and there is also a 25-language multiquine.19
Similar to, but unlike a multiquine, a polyglot program is a computer program or script written in a valid form of multiple programming languages or file formats by combining their syntax. A polyglot program is not required to have a self-reproducing quality, although a polyglot program can also be a quine in one or more of its possible ways to execute.
Unlike quines and multiquines, polyglot programs are not guaranteed to exist between arbitrary sets of languages as a result of Kleene's recursion theorem, because they rely on the interplay between the syntaxes, and not a provable property that one can always be embedded within another.
A radiation-hardened quine is a quine that can have any single character removed and still produces 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:20
Using relational programming techniques, it is possible to generate quines automatically by transforming the interpreter (or equivalently, the compiler and runtime) of a language into a relational program, and then solving for a fixed point.21
Bratley, Paul; Millo, Jean (1972). "Computer Recreations: Self-Reproducing Automata". Software: Practice and Experience. 2 (4): 397–400. doi:10.1002/spe.4380020411. S2CID 222194376. /w/index.php?title=Paul_Bratley&action=edit&redlink=1 ↩
Kuhn, Bradley M. (November 21, 2007). "stet and AGPLv3". Software Freedom Law Center. Archived from the original on March 15, 2008. Retrieved June 14, 2008. /wiki/Bradley_M._Kuhn ↩
"Quine Program". wiki.c2.com. http://wiki.c2.com/?QuineProgram ↩
"Simple Java quine, self replicating (Self copying) Java code, with text blocks. This code can be run with Java 15+ or Java 13+ with special flags. License is public domain, no rights reserved". https://gist.github.com/destan/c0db5a237e9875a56141403aaa6cb9c7 ↩
Examples include Bash, Perl, and Python /wiki/Bash_(Unix_shell) ↩
"IOCCC 1994 Worst Abuse of the Rules". Archived from the original on 12 November 2020. https://web.archive.org/web/20201112015540/http://www0.us.ioccc.org/1994/smr.hint ↩
"Makefile". IOCCC.org. Archived from the original on 23 April 2019. Retrieved 4 April 2019. https://web.archive.org/web/20190423002150/http://www0.us.ioccc.org/1994/Makefile ↩
Dan Piponi (5 February 2008). "A Third Order Quine in Three Languages". http://blog.sigfpe.com/2008/02/third-order-quine-in-three-languages.html ↩
Bruce Ediger. "Ask and ye shall receive: Self-replicating program that goes through three generations, Python, Bash, Perl". Archived from the original on 2011-02-23. Retrieved 2011-03-17. https://web.archive.org/web/20110223164033/http://www.stratigery.com/source.html#Ouroboros ↩
b.m. (1 February 2011). "multiquine". Archived from the original on 2013-04-15. https://archive.today/20130415050710/http://hpaste.org/43501/multiquine ↩
Dan Piponi (30 January 2011). "Quine Central". http://blog.sigfpe.com/2011/01/quine-central.html ↩
Ruslan Ibragimov (20 April 2013). "Quine Ruby -> Java -> C# -> Python" (in Russian). Archived from the original on 4 March 2016. Retrieved 20 April 2013. https://web.archive.org/web/20160304040341/http://ruslan.ibragimov.by/20-04-2013.quine-ruby-java-c-python ↩
Shinichiro Hamaji (10 November 2007). "Quine by shinh (C C++ Ruby Python PHP Perl)". (this one is also a polyglot) http://golf.shinh.org/reveal.rb?Quine/shinh+%28C+C%2B%2B+Ruby+Python+PHP+Perl%29_1194650418&rb ↩
Ku-ma-me (22 September 2009). "Uroboros Programming With 11 Programming Languages". Archived from the original on 29 August 2011. Retrieved 17 March 2011. https://web.archive.org/web/20110829204605/http://asiajin.com/blog/2009/09/22/uroboros-programming-with-11-programming-languages/ ↩
Yusuke Endoh (2 November 2021). "Quine Relay - An uroboros program with 100+ programming languages". GitHub. https://github.com/mame/quine-relay ↩
Michael Wehar (10 November 2019). "C Prints JavaScript". http://michaelwehar.com/quines/c_prints_javascript.html ↩
David Madore. "Quines (self-replicating programs)". http://www.madore.org/~david/computers/quine.html ↩
Rijnard van Tonder (14 January 2020). "Pentaquine - 5 part multiquine". GitHub. https://github.com/rvantonder/pentaquine ↩
Lu Wang (21 May 2021). "Quine Chameleon#Variants". GitHub. https://github.com/coolwanglu/quine-chameleon#variants ↩
Yusuke Endoh. "Radiation-hardened Quine". GitHub. Retrieved 2014-02-24. https://github.com/mame/radiation-hardened-quine ↩
Byrd, William E.; Holk, Eric; Friedman, Daniel P. (2012-09-09). "MiniKanren, live and untagged: Quine generation via relational interpreters (Programming pearl)" (PDF). Proceedings of the 2012 Annual Workshop on Scheme and Functional Programming. Scheme '12. New York, NY, USA: Association for Computing Machinery. pp. 8–29. doi:10.1145/2661103.2661105. ISBN 978-1-4503-1895-2. 978-1-4503-1895-2 ↩