Rachit Talks

Monday 3 August 2020

Quine : The Self Printing Code

A few days back I came across the following painting through social media :

Figure 0
Here, we can see a painter painted himself painting himself who is painting himself back. Suddenly a thought striked in my mind, can we write a program which is capable of writing itself back? It sounds interesting right? 

Now, suppose I ask you to write such a program, how will you try to approach this problem? Before reading further, give it some thought and try to do it (may be write some pseudo code). 

I will be using python for this blog, so I recommend you to open up any python IDE on your system and let's get started. Okay, so one basic approach can be to write a program which accesses the file in which it is written and then displaying the whole content of that file as an output!
Let's see how we can do it in python (you can try in other languages as well).

Figure 1
Just write this one line in a file, save it as "test.py" & run it. You should see an output like this:
Figure 2
Here we get our desired output right!, but if I put a constraint that no input should be provided to the program by any means i.e. user or his/her system can't provide any kind of input to the program then this program not stands correct because here our program is reading a file (i.e. itself) so it is taking a file as an input.

Now the problem gets a bit tricky, we have to take no input, no file reading, nothing. How can we proceed now? Here comes the idea of "Quine".

Quine : A program that outputs its own source code without taking any inputs is said to belong to the family of programs called Quine.

Let's see a simple quine program in python:

Figure 3

If you run this program, you'll get the following output:

Figure 4
If you watch carefully, here we aren't taking any kind of input, we are not accessing any file still we are getting our desired result. This is the shortest python quine program. Now let's see how it works?

Firstly to create a quine program the basic approach can be, to create a variable which stores the source code itself. In Figure 3, we can see such a variable "a" which is storing a string. Actually the variable "a" is like the soul of this program or you can say DNA which has the potential to replicate the source code of our program. Now let's understand string stored by variable "a".

a = "a=%r;print(a%%a)" = "a=%r;" + "print(a%%a)" 

To understand the first part, you must know about %r in python, it's similar to 
%s with a minute difference. Actually %s uses the str function while %r uses repr function. So, let's see the difference between str and repr functions itself.

Figure 5

As you can see, repr() return printable representation of an object whereas str() return nicely printable representation of an object. In other words, repr() will return the complete representation of string what a programmer types i.e. with quotes, on the other hand str() won't return quotes. Let's see one more simple example,
Figure 6

In the figure 6, we can see that %s behaves like str() & %r behaves like repr(). So I hope, now the idea of %s & %r is clear. So, I hope you must be getting why we are using %r in Figure 3 and not %s, because we want the quotes as well while printing, right! But to make it crystal clear let me show you what happens if we use %s instead of %r, 

Figure 7

Let's see the output,

Figure 8

You can clearly see the difference between outputs in figure 4 and figure 8. In red marked portion of figure 8, we can see quotes are missing due to which the output is not exactly same as the source code, this happened because we used %s instead of %r. Hence program in figure 7 isn't a quine. 


I hope by now you must have got some idea about quine & how can we write a program to print its own source code without taking any input. There are many ways in which we can implement a quine program, let's see how can we implement it using eval(). eval() method parses the expression passed to it,  runs the python expression (code) passed to it & returns the result evaluated from the expression, so print(eval('1+1')) will print 2. Note that the argument passed to eval() is just a string in which we can write some code and it gets evaluated. Now a question for you, what will eval('print(2)') will do? It will also print 2 right ?, I hope you get it now!

 Now see the code below-

Figure 9

Let's see the output,
Figure 10

I have already talked about repr() and eval(), so try to understand the code, its quite easy, just two lines and again DNA lies in the first line itself ;). So, I hope I was able to incorporate basic idea of quine in your mind, how such code works & how can you create very simple ones. You can create such programs in various other languages but I demonstrated using python because it is very easy to grasp. There are many resources if you want to learn further about quine programs, you can just read more about them on internet if you wish. Before ending, let me show you a bit more complicated program, 

Figure 11
No need to write it, click here to download! It is a bit more complicated program to demonstrate the idea of quine, once you save this file as Program1.py, run it, the same source code would be generated in a file named Program2.py. Now, if you run Program2.py, same source code would be generated in Program3.py & so on. The only difference you might see, will be in line 2 and 3 because I have splitted that line of code to show you properly the entire code in that image as that string was quite long but it has no effect over the logic of the code. So, I hope you got the point that we can take the idea of quine programs further more. It depends all upon your level of curiosity but I believe that I was able to lay down some foundation in your mind regarding quine programs. I purposely didn't explain the last code because I want you to do some research, try a bit & grasp it by yourself. Let me know in comments if it's too hard, I will add a description about that code as well. I hope you liked this blog!

HAPPY CODING!

Suggested readings:

5 comments: