quine - a program that computes & prints its own source code
DESCRIPTION
A simple way to write a program that *computes* and outputs its own source code.The actually program itself is not posted but the entire implementation idea is explained - hopefully this encourages people to write the program on their own.TRANSCRIPT
Write a program that prints itselfBy Kiran Bhogadi
http://en.wikipedia.org/wiki/Drawing_Hands
One simple rule
• Don’ts :
The program can not read its own source code file and output it.
• Do’s :
The program is set up to compute its own source code and output it.
A preliminary approach
Divide program into 2 parts :
A and B
A BProgram =
A preliminary approach
• In your program :
– A and B could be two different functions/methods
– Or A and B could each be just a line of code
Assume that execution starts in A and ends with BSTART END
A B
A First Implementation Idea
• A and B are two methods
• The method A() has a copy of method B()’s code (as a literal string). A() prints this string and then calls B()
“<B>”
A
A First Implementation Idea (contd.)
• Tempting to design method B() to contain the code of method A as a literal string too!
• But that won’t work!
• Circular Dependency
A better approach
• Method A has a string literal copy of Method B code and it simply prints it.
• Method B will compute the
string the represents Method A’s code
“<B>”
A
Method A’s code – contains Method B as a string literal
• Let’s say the method A is written like this :
Method A () {
Print(“<Code_for_Method_B>”)
}
Can we compute the code for Method A …
… given the code in the “box” i.e., “<Code_for_Method_B> ?
• YES!!
Computing Method A’s code given <Code_for_Method_B> as input
• The code (method) that computes Method A’s code :
– Input_String S For e.g. “<Code_for_Method_B>”
– Returns :
“Method A () {” + NEWLINE+ “Print(\“” + Input_String S + “\”)” + NEWLINE+ “}”
Note the returned string represents Method A’s code!
Method A () {Print(“<Code_for_Method_B>”)
}
So how does the code for Method B look like?
See the previous slide!
Putting it All together
Method A () {Print(<Code_for_Method_B>);Call B(…);
}
<Code_for_Method_B> is a string literal representing Method B’s code :
Method B(Input String S) {String returnValue =
“Method A () {” + NEWLINE+ “Print(” + Input_String S + “);” + NEWLINE
+ “Call B(…);” + NEWLINE+ “}”;
Print returnValue;}
What happens when the program runs ?
• Execution starts with Method A()
• A() outputs the code (“literal”) for Method B
• A() calls B()
• B() computes the code for Method A and outputs it
Method A() Method B()
B
Step 3 : outputs computed value
Step 2: calls
A
Step 1 : outputs literal
Some Technicalities / Questions• If the original program file contained the
methods A and B in this order:
then the output from the program contained the methods in reverse order
But this can be fixed easily!
A B
B A
Further Reading
• Michael Sipser - Introduction to the Theory of Computation (http://www.amazon.com/Introduction-Theory-Computation-Michael-Sipser/dp/053494728X)