Quine
Thus in this program, one string functions in two ways: first as program, and second as data. This is the secret of self-reproducing programs… ~ D. R. Hofstadter[1]
A quine is a program that produces a copy of itself by printing a string twice in two differently-quoted contexts. Quines are the earliest example of formal self-reproducing objects. Quines were named after the philosopher W. V. O. Quine by Douglas Hofstadter in 1979.[1] One of the typical challenges of any new esoteric programming language is to write a quine in it.
It is considered cheating for a program to access its own source code through language-specific libraries or reflection, like accessing its own memory or disk storage. Similarly, the null program is usually not considered a quine. Hofstadter gives the example of a photocopier; a piece of paper is not a quine merely because it is compatible with the photocopier's ability to make copies of it.
The language Muriel is based on the idea of quines.
How to write quines
Quines can be developed in any Turing-complete language which has the ability to freely create output in its own source alphabet (though the existence of quines does not imply Turing-completeness; also see User talk:Smjg for a previous discussion about "quineless" TC languages and what "freely create output" means). Despite all the mystery surrounding them, they are actually quite simple to develop. Here is an algorithm for doing so:
First we construct the function print which outputs its input. In most languages this is trivial. For example, in Javascript we have
console.log(input);
Now consider print(print), the program which passes the source code of print to print. For example
input = "console.log(input);"; console.log(input);
It is quite easy to construct print(print), so we'll go ahead and write a program that given the input of the source code of program t outputs a program that passes the source code to the function, which is like saying "say 'say'" or "shout 'shout'." We'll call that program quinify. A quick note: you'll sometimes run into issues with escape sequences when writing quines. This is not a theoretical problem, only a language specific one. In this case we're using String.fromCharCode(34) to represent double quotes to avoid the issue. Anyway, here's quinify in Javascript:
console.log('input = ' + String.fromCharCode(34) + input + String.fromCharCode(34) + ';' + String.fromCharCode(10) + input);
The final step is easy! Pass the source code of quinify to quinify. We have a quine.
input = "console.log('input = ' + String.fromCharCode(34) + input + String.fromCharCode(34) + ';' + String.fromCharCode(10) + input);";
console.log('input = ' + String.fromCharCode(34) + input + String.fromCharCode(34) + ';' + String.fromCharCode(10) + input);
Here it is minimized for those inclined (151 bytes)
i="console.log('i='+String.fromCharCode(34)+i+String.fromCharCode(34)+';'+i)";console.log('i='+String.fromCharCode(34)+i+String.fromCharCode(34)+';'+i)
Clearly the only challenge here is in writing quinify, which in many esolangs is non-trivial. Get to work!
Generation
Quines can be generated by relational logic over an interpreter. For example, a Scheme interpreter written in miniKanren can generate Scheme quines automatically.[2]
See also
External resources
- The Quine Page has a nice collection of quines in different languages.
- Quines (self-replicating programs) is excellent information about quines.
- A truly esoterical quine
- Quine on Wikipedia
- Quine on Rosetta Code
References
- ↑ 1.0 1.1 D. R. Hofstadter, 1979. Gödel, Escher, Bach: an eternal golden braid. p499-503.
- ↑ W. E. Byrd, E. Holk, D. P. Friedman. 2012. MiniKanren, live and untagged: quine generation via relational interpreters (programming pearl). In Proceedings of the 2012 Annual Workshop on Scheme and Functional Programming (Scheme '12). Association for Computing Machinery, New York, NY, USA, 8–29. https://doi.org/10.1145/2661103.2661105 http://webyrd.net/quines/quines.pdf