# quine (computing) ![[javaimplementationofaquineprogram.png]] a quine's output is exactly the same as its source code a quine 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 ir 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 the name "quine" was coined by douglas hofstadter in ir 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 theorised 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 # # constructive quines 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 example a. chr(39) == "'" a = 'a = {}{}{}; print(a.format(chr(39) a chr(39)))'; print(a.format(chr(39) a chr(39))) example b. chr(39) == "'" b = 'b = %s%s%s; print(b %% (chr(39) b chr(39)))'; print(b % (chr(39) b chr(39))) example c. %r will quote automatically c = 'c = %r; print(c %% c)'; print(c % c) the following java code demonstrates the basic structure of a quine public class quine { public static void main(string args) { char q = 34; // quotation mark character string l = { // array of source code "public class quine"- "{"- " public static void main(string args)"- " {"- " char q = 34; // quotation mark character"- " string l = { // array of source code"- " "- " };"- " for (int i = 0; i < 6; i++) // print opening code"- " system.out.println(l);"- " for (int i = 0; i < l.length; i++) // print string array"- " system.out.println(l + q + l + q + '-');"- " for (int i = 7; i < l.length; i++) // print this code"- " system.out.println(l);"- " }"- "}"- }; for (int i = 0; i < 6; i++) // print opening code system.out.println(l); for (int i = 0; i < l.length; i++) // print string array system.out.println(l + q + l + q + '-'); for (int i = 7; i < l.length; i++) // print this code system.out.println(l); } } 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 thanks to new text blocks feature in java 15 (or newer) a more readable and simpler version is possible public class quine { public static void main(string args) { string textblockquotes = new string(new char{'"' '"' '"'}); char newline = 10; string source = """ public class quine { public static void main(string args) { string textblockquotes = new string(new char{'"' '"' '"'}); char newline = 10; string source = %s; system.out.print(source.formatted(textblockquotes + newline + source + textblockquotes)); } } """; system.out.print(source.formatted(textblockquotes + newline + source + textblockquotes)); } } the same idea is used in the following sql quine select replace(replace('select replace(replace("quot;-char(34)-char(39))-char(36)-"quot;) as quine'-char(34)-char(39))-char(36)-'select replace(replace("quot;-char(34)-char(39))-char(36)-"quot;) as quine') as 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 eval s="print 'eval s=';p s" lua can do s="print(string.format('s=%c%s%c; load(s)()'-34-s-34))"; load(s)() in python 3.8 exec(s:='print("exec(s:=%r)"%s)') 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. 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 actually compiled but used `cp` to copy the file into another file which could be executed 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 # !/bin/sh invalid quine reading the executed file from disk is cheating cat $0 a shorter variant exploiting the behavior 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 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 → brainflock → whitespace → unlambda **+** ruby → scala → scheme → scilab → shell (bash) → s-lang → smalltalk → squirrel3 → standard ml → ... → rexx (128 (and formerly 50) programming languages) **+** web application → c (web application source code consists of html javascript and css) 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 **+** 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 fand there is also a 25-language multiquine 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 ir 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 # automatic generation 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 **+** diagonal lemma **+** droste effect **+** fixed point combinator **+** self-modifying code **+** self-interpreter **+** self-replicating machine **+** self-replication **+** self-relocation **+** tiddlywiki **+** tupper's self-referential formula **+** programming languages **+** quine's paradox **+** polyglot (computing) 1. examples include bash perl and python **+** douglas hofstadter: gödel escher bach: an eternal golden braid **+** ken thompson: "reflections on trusting trust" (communications of the acm 27(8):761-3) **+** tiddlywiki a quine manifested as a wiki **+** the quine page (by gary p. thompson) **+** a brief guide to self-referential programs **+** quineprogram at the portland pattern repository wiki **+** david madore's discussion of quines **+** zip file quine **+** zip files all the way down **+** an introduction to quines - in particular quines using more than one language **+** quine web page: a standards-conforming html+javascript web page that shows its own source code **+** html quine: an html page that only uses html and css to show its own source code **+** quine challenge for tom's javascript machine with a series of interactive hints **+** a java quine built straight from kleene's fixed point theorem composition and s-n-m **+** a qr code quine // republic of bob