Quines

Readings:

Read the Wikipedia article on quines:

http://en.wikipedia.org/wiki/Quine_%28computing%29

Read Ken Thompson’s Speech, Reflections on Trust:

http://cm.bell-labs.com/who/ken/trust.html

Look at this article on fixed points:

http://en.wikipedia.org/wiki/Fixed_point_%28mathematics%29

Skim this article:

http://c2.com/cgi/wiki?QuineProgram

Reading Questions:

1. What is a Quine?

2. How are quines and fixed points related?

3. What kinds of languages can Quines be written in?

4. Explain the structure of a quine

5. How would you add “baggage” to a quine

6. What is a multiquine?

7. Explain why your computer be at risk of being hacked by ken thompson

Homework Problems:

1. Write a Quine in Matlab

2. Write a Quine in Python

3. Look up how file writing is implemented in both of these languages

Matlab:

Fopen: http://www.mathworks.com/help/techdoc/ref/fopen.html

Fprintf: http://www.mathworks.com/help/techdoc/ref/fprintf.html

Fclose: http://www.mathworks.com/help/techdoc/ref/fclose.html

Python:

http://docs.python.org/tutorial/inputoutput.html

4. Challenge: see if you can write a multiquine!

Quiz Question: Edit your python quine such that instead of printing its output, it writes it's output to a new python file. Now change the filename of the quine to quine1.py . Now, change the quine such that if the quine's name is quinex.py where x is some positive integer, quinex will write itself to the file quine(x+1). Now change your quine such that the next quine would write itself to quine(x+2), and that one would write to quine(x+3) and so on.

Orion's Troll move: change your quine such that it will run the quine that it has written at the end of the program (I didn't actually do this since I didn't want to break my computer)

Quiz solution is file python_quine2.py

Lecture Notes:

A quine is a program that writes itself as output.

If we think of the interpreter as a mathematical function that takes source code as input and prints stuff as output, then quines would be a fixed point for the interpreter, since the input equals the output.

The question is, what makes quines so interesting?

Let’s imagine a hello_world program

So nice!

Well, what does it print?

That’s great.

So we have a hello world program. All it does is print the statement “hello world”

If we can just get the “print” part into the print statement, we should be good.

Ehhh, that was not really any better. Not only are we still one print statement off, we still need to put in some apostrophes. Let’s tackle the apostrophes first. There are two ways we could represent apostrophes. We could express them as:

But, this means that we now have to find a way to represent the backslash, which is gonna be a pain as well. A better way to do this is:

Which is the ASCII representation of apostrophe. This expression doesn’t have any delimiters inside of it, so we can later just reprint it as a string:

So let’s store the apostrophe character in some variable, I’m gonna call it “a”.

Quines print out their own source code, so let’s grab the low hanging fruit, and print out that first line:

We get:

Which means that we still have to print out the print statement, but we are getting closer.

Hmmm.

Well, clearly we are going to have to print out multiple lines of code regardless of how we do this. Perhaps if we store the stuff that we are going to print in some string array, we might have a better chance at writing this quine. Here it goes:

Clearly we are missing a few lines. Let’s print the low hanging fruit (i.e., lines without apostrophes):

The problem is, now we have 3 lines with apostrophes marks. Let’s try to knock them out all at once. It seems that all of them are structured the same. Each one is of the form s.append(‘ the element of s ‘). Let’s try to print them.

The problem is that now we have more quotes in line 7. Lets move those strings to elements in the array s:

Now all we have to do is add the final few lines of the code to the string array:

We get the output:

Congratulations! You have written your first quine. Easy isn’t it?

The structure is very similar to writing the Y combinator. I can’t quite explain to you why, but notice how we went through the process of starting far away from where we wanted to be, and changing our code bit by bit to get where we wanted to be.

Class activity: start working on the homework. Multiquines are really tough!