quine - a program that computes & prints its own source code

15
Write a program that prints itself By Kiran Bhogadi http://en.wikipedia.org/wiki/Drawing_Hands

Upload: kiranbhogadi

Post on 02-Jul-2015

1.772 views

Category:

Technology


0 download

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

Page 1: Quine - A program that computes & prints its own source code

Write a program that prints itselfBy Kiran Bhogadi

http://en.wikipedia.org/wiki/Drawing_Hands

Page 2: Quine - A program that computes & prints its own source code

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.

Page 3: Quine - A program that computes & prints its own source code

A preliminary approach

Divide program into 2 parts :

A and B

A BProgram =

Page 4: Quine - A program that computes & prints its own source code

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

Page 5: Quine - A program that computes & prints its own source code

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

Page 6: Quine - A program that computes & prints its own source code

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

Page 7: Quine - A program that computes & prints its own source code

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

Page 8: Quine - A program that computes & prints its own source code

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>”)

}

Page 9: Quine - A program that computes & prints its own source code

Can we compute the code for Method A …

… given the code in the “box” i.e., “<Code_for_Method_B> ?

• YES!!

Page 10: Quine - A program that computes & prints its own source code

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>”)

}

Page 11: Quine - A program that computes & prints its own source code

So how does the code for Method B look like?

See the previous slide!

Page 12: Quine - A program that computes & prints its own source code

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;}

Page 13: Quine - A program that computes & prints its own source code

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

Page 14: Quine - A program that computes & prints its own source code

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

Page 15: Quine - A program that computes & prints its own source code

Further Reading

• Michael Sipser - Introduction to the Theory of Computation (http://www.amazon.com/Introduction-Theory-Computation-Michael-Sipser/dp/053494728X)