Talk:Quine

Revision as of 03:53, 1 September 2011 by rosettacode>Ledrug (→‎C quine: new section)

I did not read up as fully as I should have on this task. The program is to output its own source without accessing files. This makes the examples here incorrect. See the wiki for more clarification. --Mwn3d 20:32, 17 November 2007 (MST)

I don't think this task does illustrate the comparative features of different languages very well. As the Wikipedia article explains, one way to solve this task that will work in every language is put the program code into a string, together with some mechanism to replace it at a special location with a copy of itself. For languages which keep a more-or-less one-to-one representation of the code around at runtime (Lisp, Smalltalk, Forth, etc.), it just boils down to accessing this representation. Smaller solutions are in danger of becoming an exercise in obfuscation, or at least become unreadable very quickly. And the examples seen now which mostly access files are obviously missing the point.

So I'd propose to delete this task, and replace it with a task that shows how to access (and maybe manipulate) code at runtime for languages that support it. The general solution can be subsumed by task(s) that show how string manipulation works. Dirk Thierbach 18 November 2007

The name of the task is Quine. A Quine is not allowed to open its source file and to copy it to standard output. All implementations which do this should be removed. 194.48.84.1 05:56, 20 November 2007 (MST)

The task states (perhaps not clearly enough) that the program itself should do the printing, not any toplevel read-eval-print loop, or equivalent. Otherwise, all constants in languages that have such a toplevel would be quines. But that is completely missing the point of the original problem, which (as explained for example in the book Gödel, Escher, Bach) is about self-application, and quoting. -- Dirk Thierbach 21 November 2007

I clarified the task. Different scenarios have different requirements for output. The Lisp example satisfied the spirit of the task (produce a usable reproduction of one's own code), so the task itself needed to be adjusted. I won't take that approach in all cases; It's a matter of the spirit of the task. --Short Circuit 13:06, 21 November 2007 (MST)
I beg to differ - it's not the "scenarios" which have requirement for output, it's the spirit of the task itself which is not correctly presented if you allow constants (or empty programs) as solutions. I strongly recommend to read at least the Wikipedia article, or the book mentioned above (your library should have a copy). The philosopher Quine studied self-reference, as exemplified in the paradox "'Yields falsehood when preceded by its quotation' yields falsehood when preceded by its quotation."
And it's this idea that makes up a Quine - A piece of code, applied to itself, once quoted and once unquoted. (BTW, this then again is the same technique used to prove undecidability theorems.) And the extra requirement "should print it's output" is one way to enforce that the quoting has to be done by the program itself, not by some external read-eval-print loop.
The task is bad enough as it is (it doesn't really help in comparing programming languages), and it's not improved by allowing "fortunate" border cases which take away the main point. Ok, so what's the procedure to resolve differences in opinion? Dirkt 14:16, 21 November 2007 (MST)
To quote the Wikipedia page in question:
In computing, a quine is a program, a form of metaprogram, that produces its complete source code as its only output. For amusement, programmers sometimes attempt to develop the shortest possible quine in any given programming language.

Note that programs that take input are not considered quines. This would allow the source code to be fed to the program via keyboard input, opening the source file of the program, and similar mechanisms. Also, a quine that contains no code is ruled out as trivial; in many programming languages executing such a program will output the code (i.e. nothing). Such an empty program once won the "worst abuse of the rules" prize in the Obfuscated C contest.
While the question that Quine investigated implies the quote-interpreting solution, the definition of the problem (per Wikipedia, anyway), doesn't require it. Reading it over, I'll agree that the Lisp example doesn't demonstrate anything one wouldn't get from Empty Program, and would be better replaced with the Scheme/Common Lisp example from the Wikipedia page. I'd have no problem modifying the task description to discard empty programs as trivial, or even requiring the program to use a human-readable output method. Still, the task can still serve to compare languages. Some languages make accessing the source simple, like in the Forth example, or the JavaScript example on the Wikipedia page. Even the string-modifying solution allows for differences between languages; Different languages have different best solutions for replacing a substring.
It's not a 1:1 technique comparison, but neither is any task with both functional and procedural language examples. Would you be reasonably satisified if the task was changed to require human-readable output and exclude empty programs? --Short Circuit 21:35, 21 November 2007 (MST)
The problem with Wikipedia of course is that it is great to get a rough feeling for some topic, but it's neither precise nor authoritative. So it's dangerous to take some definition there literally and insist it's the "correct" one - using the context to determine the ideas is much more appropriate for Wikipedia.
It's true that this task exposes (a) access to source code and (b) ways to modify strings, but considering the amount of misunderstanding this task generates, I still think it is much better (and more informative) to handle those on pages of their own. Especially (a) could only benefit from greater detail. Languages like Forth, Smalltalk, Lisp and Tcl have interesting ways to access and modify code at runtime, but you need a code example to bring this out. A Quine isn't one.
I've expanded the task description with some background to better explain the "spirit", and required the "canonical" version as one of the code examples. I've also ruled out constant expressions, and replaced your Lisp version with the one from Wikipedia. If you've objections or improvements, feel free to modify it. Dirkt 03:43, 22 November 2007 (MST)

What is the license for the Forth example?

It says it was copied. I could not easily find a licence for the donor site. --Paddy3118 02:42, 30 March 2009 (UTC)

Wow... could there exist a "restrictive" license for such a code? It's like saying Hello World is copied, and asking about the license for it (if someone did it and laws agree, I hope at least everyone will laugh at him!). Everyone, taking a look at a forth manual, e.g. I use this, can produce exactly the same code, without copying it from nowhere. (Source pushes the address of the current input buffer and its length LEN on stack: ADDR LEN; type "prints" LEN bytes starting from ADDR). Rather straightforward. If the copied is a problem, to me it's enough to strip the link (which anyway shows other more complex quines), and ask to some forth expert to produce in the smallest time the most straightforward quine using the smallest possible number of forth primitives. I bet it would write down "source type". --ShinTakezou 10:34, 30 March 2009 (UTC)
Ick. Being on Rosetta Code, it's licensed GFDL whenever distributed by us. But being that it's copied from another site, there's a question of whether the other site's license is violated. As for what license that would be...Under US copyright law, anything without an explicit copyright label is assumed to be under an "all rights reserved" scenario. However, I think that with appropriate citation, it's acceptable. I'm not sure if what's there qualifies as appropriate citation, though; Someone who knows more about published papers would be a better judge of that. --Short Circuit 22:11, 30 March 2009 (UTC)
After looking at the edit history, I very strongly suspect that Shin created it on his own, and IanOsgood added a link to the list later. So I think we're completely kosher on this one, though the wording should probably be a bit more clear that the code wasn't copied. AFAIK, if Infinite Monkey Corp independently came up with Romeo and Juliet, they'd be every bit as entitled to its copyright as Shakespeare. (Assuming modern times...) --Short Circuit 22:18, 30 March 2009 (UTC)
No, I haven't written the code (as far as I remember I have not contributed to any forth code yet; surely not to quine anyway), or I would have said that here. I simply said that noone can claim a copyright on such a small piece of code, except maybe the creator of the forth language. The code simply says "get the buffer where the source text is, and print it"; provided that a language has a primitive to "print" and one to get such information, everyone able to read a manual can, without copying, produce this quine. If it is not stack based, one could say something like "printf("%s", get_source());" (interpreted C?)... Anyway, if it is an issue, one could try to contact Neal Bridges (named here), maybe he wrote it (or know who can have written it), altogether with more complex quine(s) (and these can't be copied without thinking about a lincense, since hardly one can reproduce them only reading the forth manual...); it appears here also (author unknown! and cheating suspect...).
Someone wrote it, maybe after forth manual reading, or maybe taking a look on the net... and IanOsgood added just the link to a list of forth quines, among these there's also "ours". I think there are not license violation (I imagine RC must care a lot about these details), but I am not a lawyer (rather in this case I would like to be a lawyer-eater) --ShinTakezou 00:12, 31 March 2009 (UTC)
Third thought, I think the guy suspecting it's almost cheating, after all, it's right; I've created the following C cheated-quine:

<lang c>#include "cheater.h" int main() {

 printf("%s", __source__);

}</lang>

Which works (with gcc statement expression extension) provided that cheater.h is

<lang c>#include <stdio.h>

  1. define __source__ ({ \
     char *s = "#include \"cheater.h\"\n\

int main()\n\ {\n\

 printf(\"%s\", __source__);\n\

}\n"; s;\

   })</lang>
From a "functional" point of view, this works the same way of forth (and maybe others), by accessing the text of the source stored in memory (not by loading it at runtime...); since C is compiled, the binary holds no the source (compiler at least once loaded the source in memory, but compiled binary can't see its past...), and I had to include it manually... (Hm, one could work harder on debug informations and ELF maybe, and write a more forth-like quine accessing at runtime the "segment" where the whole source code, stored by the compiler itself this time, is and print it...)... Is this a honest quine or really cheating? (Interesting question to me:D) --ShinTakezou 00:54, 31 March 2009 (UTC)

Second scheme example correct?

I have some doubt about the correctness of the second scheme example. It only works when entered into an interpreter. When compiled there will be no output. The reason for this is that the expression evaluates to a result which is identical to the original expression. However, I realise that this is not the same as simply entering a constant expression like 0, which will evaluate to itself. When the given expression is evaluated, the lambda function will be applied to the argument that follows, resulting in an expression that is identical to the original expression. The problem description states that the program must output its own source. The given example doesn't produce any output by itself (generated by display in scheme). How should we handle this? --Dsnouck 14:16, 26 May 2010 (UTC)

If you feel that this is a significant issue, I think you should document it. In the general case, the concept and implementation of "output" is situational and depends on the character of the host session, and Quines traditionally assume just one mode of output. (And this is more of an issue in some languages than others.) --Rdm 14:24, 26 May 2010 (UTC)

nostalgia comments

I added some nostalgia notes at the end of the Fortran example. I had assumed that this [QUINE} being such an old challenge, that it would be pertinent. Almost a half-century ago!! Gerard Schildberger

Haskell evaluation

If you're going to allow an expression that evaluates to itself, won't "blah" evaluate to "blah"? For that matter \x.x will evaluate to \x.x 71.176.132.192 20:35, 26 April 2011 (UTC)

C quine

I removed some comments about style about the C quine: it's a bit silly to be serious about good coding style for this subject. If you want properly indented, properly typed and properly header'ed C code, here you go:<lang c>#include <stdio.h> int main(void){ char*p="#include <stdio.h>%cint main(void){%c%cchar*p=%c%s%c;%c%cprintf(p,10,10,9,34,p,34,10,9,10,9,10,10);%c%creturn 0;%c}%c"; printf(p,10,10,9,34,p,34,10,9,10,9,10,10); return 0; }</lang> which does require a trailing newline, btw. Being "more correct", It definitely does not help one read or understand the code. --Ledrug 03:53, 1 September 2011 (UTC)

Return to "Quine" page.