Quines, which are programs that generate their own code as part or all of their output are a neat idea for a programming puzzle. However, do they have any use beyond that?
4 Answers
The only time I ever heard of any practical use for quines is when Ken Thompson used it to hide a Trojan horse in the Unix login program.
The Trojan consisted of something like if (login == "Trojan") login();
(more probably written in proper C), but something like that would be too obvious in the code. So what he did was to embed it in a quine and hide it in the binaries of the C compiler.
The quine's self replicating properties were enough to ensure that even though there was no trace of this backdoor in any source code, it would effectively compile every time a C compiler would come across a Unix login() function.
I guess clearer explications could come from reading the paper. It's a good paper.
A quine was used to seed Tierra, a digital organism simulator, and life then evolved from it. The quine was used because it guaranteed that the first generation would produce viable offspring.
The evolution led to many interesting results, including parasitic and symbiotic life forms, and even meta-parasites. Might that be considered 'useful'?
After some Googling, the surprising answer is that there is at least some theoretical academic effort to use them to make code self repairing, apparently.
One of my search terms was "genetic programming", if anyone wants to look further - it's the only potentially useful related programming area I could think of.
EDIT - Just found the use of Quines in an English outline of a Mathematical proof (Ron Maimon's answer) of Godel's Theorem.
I've used Quine-like techniques in web pages in the past. Mind you, this was in 1998-99...
I had what we now call a "web app", a set of CGI-BIN programs, some of which performed some time-consuming interactions with mainframes. In order to save doing the time-consuming part twice, in certain cases I would send along JavaScript that could re-write the original page into a form to send back pre-computed information to "reserve a spot in the future". I can't recall what peculiarity of JavaScript interpretation at the time required it, but Quine-like encoding, and decoding on output, of the "reserve a spot in the future" HTML and associated JavaScript became necessary.