Everything2
Near Matches
Ignore Exact
Full Text
Everything2

quine

created by ChrisHall

(thing) by ChrisHall (?) (print)   ?   (I like it!) Sat Nov 13 1999 at 8:55:59

A program that generates its own source code as its output. The mechanism is the same as used in the English sentence:

"preceded by itself in quotes." preceded by itself in quotes.

Named after Willard van Orman Quine, by Douglas Hofstadter.


(thing) by Magenta (7.3 y) (print)   ?   (I like it!) 1 C! Sat Nov 13 1999 at 8:55:59

"'Is a quine' is a quine" is a quine, albeit untruthful. "'Is not a quine' is not a quine" is a quine, and is also true.

"Is a statement without a subject" is a statement without a subject.

"Would be a silly name to give a child" would be a silly name to give a child.

And furthermore.


(thing) by rescdsk (1.6 y) (print)   ?   (I like it!) Mon May 15 2000 at 23:04:41

"gives resc food for thought" gives resc food for thought

To manufacture a computer quine, a program which prints its own source code, you might do this:

  1. Write a function or subroutine or whatever which, given input, prints it twice: once in the syntax of a data structure which contains the input, and once as just the input.
  2. Feed the function to itself.

(thing) by gkAndy (6.5 mon) (print)   ?   (I like it!) Sat Aug 19 2000 at 19:00:14

Doric word meaning "girl" or "woman". See also lass.

See the Everything Guide to Doric Words and Phrases.

(thing) by spiregrain (29.1 min) (print)   ?   (I like it!) 1 C! Thu Sep 07 2000 at 12:06:21

If you take quines away from computing for a second they become even more interesting. Good ol'Dougie Hofs gives a few examples in GEB, including:

"is made up of six words" is made up of six words.

Which is much more profound than it seems at first sight, and is a useful first step towards looking at self referential stuff like "this statement cannot be proven". It shows what the "this" is really doing, ie its inserting the reference to the rest of the sentence.


(idea) by Zirtix (3.1 y) (print)   ?   (I like it!) 1 C! Sat Apr 28 2001 at 21:03:00

quine, v. To deny resolutely the existence or importance of something significant. - The Philosophical Lexicon (8th edition, 1987). Yes, 'quine' is a verb as well! This eccentric and satirical usage comes from W. V. O. Quine's tendency to deny the existence of things in this way (for example, it has been claimed of his philosophy that it denies the existence of beliefs).

The classic use of this term may be in Daniel Dennett's 'Quining Qualia' (in Consciousness in Contemporary Science, eds. Marcel and Biasch, OUP); a paper where Dennett argues that subjective experience is not 'special' in the ways that other thinkers have presumed (i.e., the ways in which qualia are special).


(thing) by FordPrefect (4.4 y) (print)   ?   (I like it!) 2 C!s Tue Jul 03 2001 at 6:44:20

The word "quine" is now used to refer to two separate concepts - a phrase that quotes itself, and a phrase that reproduces himself. I believe Douglas Hofstadter distinguished these as "self-ref" and "self-rep".

Most self-reps are self-refs. Most self-refs are not self-reps. Additionally, in computer languages, the word "quine" is almost exclusively used to refer to self-reps, even the ones that work by some mechanism other than quining (quoting themselves).

Most of the English sentences above are self-refs, and the computer examples are self-reps. '"preceded by itself in quotes." preceded by itself in quotes.' is both an English quine and a self-rep, because it actually does thoroughly describe itself.

For the sake of comparing what is and is not a self-rep:

  • The Befunge code (this language was selected because it has really short quines) <@,g09,k9"
    This program describes (in fact, it outputs) a certain line of text, which happens to be <@,g09,k9" - which is the same as the code. This is a self-rep. Because it works by making two passes over itself, one of which is in 'quotes', it could be considered a self-ref as well.
  • "preceded by itself in quotes." preceded by itself in quotes.
    This phrase describes a certain phrase, which happens to be "preceded by itself in quotes." preceded by itself in quotes. - which is the same as the phrase. This is a self-ref and a self-rep.
  • 'is not a quine' is not a quine
    This sentence describes a feature of the phrase 'is not a quine'. A sentence is not the same as a feature of part of that sentence. Sure, you could create a sentence which describes that feature. Then, by definition, you get the original sentence or something very similar to it back. "My name is Bob" describes a feature of my name, which is that it is Bob, but in doing so it does not describe the sentence "My name is Bob". So neither does this, in any way, describe the sentence "'is not a quine' is not a quine". So this sentence is a quine, but only by Hofstadter's definition, not by the same definition used for computer programs.
    In fact, I think it would be rather difficult for a complete sentence to be a self-rep. Sentences describe concepts, but sentences are objects. On the other hand, noun phrases describe objects, and noun phrases are objects - noun phrases have a shot at being a self-rep. This is why command-line computer programs can be quines, as well - a command-line computer program's source code describes its output, which is made of lines of text, and a command-line computer program's source code itself is made of lines of text.

I've revised this node after discovering that 'quine' was originally used to mean self-ref and not self-rep. After changing some words, the points before still stand, but the closing statement that I rather enjoyed writing no longer holds:

In summary: "'is not a quine' is not a quine" is not a quine. And neither is the previous sentence.


(idea) by jliszka (2.2 y) (print)   ?   (I like it!) Thu Jul 12 2001 at 0:43:30

This is not exactly a quine, but is a neat quine-related curiosity:
int n=103;char*f="int n=%d;char*%c=%c%s%c;
main(){printf(%c,205-n,n,34,%c,34,n,n,10);}%c";
main(){printf(f,205-n,n,34,f,34,n,n,10);}
This code (newlines removed) prints out an other C program, which in turn prints out the original code (I could've equally supplied this code's output).

This is not a bi-quine nor a multi-quine, I'm not sure what to call it.


(idea) by Jargon (1.8 y) (print)   ?   (I like it!) Thu Jul 19 2001 at 14:33:09

quick-and-dirty = Q = quote chapter and verse

quine /kwi:n/ n.

[from the name of the logician Willard van Orman Quine, via Douglas Hofstadter] A program that generates a copy of its own source text as its complete output. Devising the shortest possible quine in some given programming language is a common hackish amusement. (We ignore some variants of BASIC in which a program consisting of a single empty string literal reproduces itself trivially.) Here is one classic quine:

((lambda (x)
  (list x (list (quote quote) x)))
 (quote
    (lambda (x)
      (list x (list (quote quote) x)))))

This one works in LISP or Scheme. It's relatively easy to write quines in other languages such as Postscript which readily handle programs as data; much harder (and thus more challenging!) in languages like C which do not. Here is a classic C quine for ASCII machines:

char*f="char*f=%c%s%c;main()
{printf(f,34,f,34,10);}%c";
main(){printf(f,34,f,34,10);}

For excruciatingly exact quinishness, remove the interior line breaks. Here is another elegant quine in ANSI C:

#define q(k)main(){return!puts(#k"\nq("#k")");}
q(#define q(k)main(){return!puts(#k"\nq("#k")");})

Some infamous Obfuscated C Contest entries have been quines that reproduced in exotic ways. There is an amusing Quine Home Page.

--The Jargon File version 4.3.1, ed. ESR, autonoded by rescdsk.


(thing) by malloc (2 d) (print)   ?   (I like it!) 1 C! Mon Jan 28 2002 at 18:30:21

Examples of quines in Perl 5

This first example, while a valid quine, is considered "cheating" by those in the know , since it opens itself and simply scans its own source code:

#! /usr/bin/perl -w

# This is not really a quine:

open (Q, $0);
while (<Q>) {
    print;
}

This next example is considered a quine amongst the Perl community, but it still seems like cheating to me, using a syntactic trick of Perl to do it's job. It takes advantage of the fact that Perl will scan the __DATA__ structure before it is even declared:

#! /usr/bin/perl

undef $/;
$_ = <DATA>;
print;
print;

__DATA__
#! /usr/bin/perl

undef $/;
$_ = <DATA>;
print;
print;
__DATA__

Here is another example that is a little more fun, courtesy of Blake Mills (Blakem on perlmonks):

#!/usr/bin/perl

printme(q{
sub printme {
  print "#!/usr/bin/perl\n\n";
  print "printme(q{$_[0]});\n";
  print "$_[0]";
}
});

sub printme {
  print "#!/usr/bin/perl\n\n";
  print "printme(q{$_[0]});\n";
  print "$_[0]";
}

Now is where it starts getting really interesting. Try and GOLF your own in any language!


(thing) by Shen (1.2 wk) (print)   ?   (I like it!) 1 C! Wed Sep 21 2005 at 21:24:28

A quine, as every good programmer should know, is a program (or sentence) that produces itself when run, without using its own source. They are named after Willard Van Orman Quine, a philosopher who studied self-reference. From HQ9+'s

Q
command, to the everlasting
print "print "print "print "print...
there are a lot of them, and sadly, most of them are in the form of meaningless blocks of code, because writing clever bits of code makes you look cool.

However, you should know:

So let us make a quine, to see what makes it tick.
Note that we are going for understanding, rather than conciseness, so if you want large globs of unintelligible code to run there are plenty out there (see the link at the bottom).


The easiest type of quine to do is one that simply loads its own source, and prints that:

int main() {
	FILE *f = fopen("/path", "r");
	char c;
	while (c = (getc(f))) putchar(c);
	fclose(f);
}

But this is considered cheating by many as it goes against the rule of not using its source. Besides, this does not work on compiled programs.


All proper quines must contain the same code twice: one to execute, and another in a string to print out. This program can print out part of it:

int main() {
	int a = 0;
	printf("int a = 0;\n");
}

This looks simple enough, so far. The most difficult task, though, is printing the command that prints - as you can imagine, you would have to print the command that prints the command that prints the command, and so on ad infinitum.

int main() {
	char *s = "int main(){ char *s = \"int main(){ char *s = \\\"int main(){ char *s.......";
	printf(s);
}

The string s is going to be pretty big, if we keep on going like that way. And even if you could have it infinitely long, it would take ages to download from the Internet.
The thing to do here is put the string in itself. By using %s in a string, with the string as its argument, we can encode the string inside itself, giving us a string containing the entire program's code, which we can then merrily printf out.

int main() {
	char *s = "int main(){char*s=\"%s\"; printf(\"s,s\");}";
	printf(s, s); 
}

If you cannot tell what is going on here, think of the printf statement as:

printf("int main(){ char*s=\"%s\"; printf(\"s, s\"); }", s);
It prints the program, including the command that sets s to %s, which in this case is itself.

So are we done? Not quite. If you try running the code, it prints the string s fine. However, if you try running the code that that produces, it doesn't quite compile. Oh dear.
What's wrong? The generated program, although it looks fine, does not escape the quotes in the string - it uses " instead of \", and the C compiler chokes on it. We cannot escape them, as then the original program would not be correct.
Instead, we must print the character (ASCII code 34) with a printf %c, instead of actually quoting it in the string, as well as adding it to the printf statement. This way, the quotes are carried through into the source that the program generates.

int main() {
	char *s = "int main() { char *s = %c%s%c; printf(s, 34, s, 34); }";
	printf(s, 34, s, 34);
}

Echo it, compile it, and run it. Then run that, and, if you want, run what that produces as well. Run it as many times as you like, you'll get the same thing. Which means we finally have our quine, yay!

Variants on this method include printing a series of character codes instead of a string, or running exec() on a string to print it out. Some programs manage to produce a quine slightly different from the original. For examples, as well as other eschewed code, see the Obfuscated C Contest.

Now that they look a lot less like blocks of meaningless code now, why don't you try a few others? http://www.nyx.net/~gthompso/quine.htm contains the list.


printable version
chaos

Willard Van Orman Quine Obfuscated C Contest golf What question is its own answer?
multi-quine Everything Guide to Doric Words and Phrases quote chapter and verse Douglas Hofstadter
:wq! This statement cannot be proven doric Gödel, Escher, Bach: An Eternal Golden Braid
Befunge fuku Reverse Quine Perl grep hack
Quine in ANSI C++ BOFH Perl Cute as hell
Geb cat hack quick-and-dirty qualia
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help


cooled by Halspal

Cool Staff Picks
Nodes to live by:
September 11, 2001 - III
Rules of program optimization
Irish Wake
Louisiana Purchase
Critias
CCBBA Karate Curriculum
sorghum
Thomas Jefferson's First Inaugural Address
How to meet the most girls
hypoglycemia
Gloria Anzaldúa Memorial Quest
Making an F-16 from a cereal box, some Scotch tape, and a penny
Cy Twombly
New Writeups
XWiz
Trism(review)
artman2003
Briefcase Full of Souls - Part I(fiction)
Dreamvirus
Alan Ladd(person)
waverider37
Harold Holt(person)
The Debutante
Until death do us part(fiction)
Ysardo
a brother to a sister(personal)
antigravpussy
your warm whispers(personal)
Clarke
Multiculturalism(idea)
aneurin
Earl of Landaff(person)
Heitah
Pseudocide(idea)
XWiz
Google Knol(lede)
Mythi
July 24, 2008(personal)
locke baron
The fall of Earth(fiction)
BookReader
Fear the Cold(dream)
Pavlovna
Kathleen MacInnes(person)
This affordable entertainment brought to you by The Everything Development Company