LearningTechnology

The Curious Case of Quines 🤔

In 1994, Szymon Rusinkiewicz submitted his entry to the International Obfuscated C Code Contest (IOCCC). In the documentation for his code, he wrote –

The world's smallest self-replicating program. Guaranteed. Produces a listing of itself on stdout.

What made Szymon’s claim interesting?

The C file submitted by him was blank. Szymon’s hack was based on a quirk of a specific C compiler that compiled an empty file into a program that does nothing. So an empty C program when compiled and run gave an empty output, i.e. its own source code which is blank.

He was right in his claim. A self-replicating program, it was.

Technically, it should have been the winning entry but the jury wasn’t sure. However, they were sure that they needed to update the rules. From next year, the source code was required to have at least one character.

That brings me to a fascinating concept from computing called Quine – a computer program which takes no input and produces a copy of its source code as its only output. For example, consider the following PHP code –

The function readfile() reads the contents of the specified file and sends them directly to the output buffer. __FILE__ is a built-in PHP “magic” constant that contains the full path to the current source code file.

However, this php code doesn’t really qualify because the definition of Quine clearly states that the program should take no input. But our php piece is doing just that, i.e., taking a file path as input, albeit sheepishly.

For a true Quine, the source code has to be generated from within itself.

Interestingly, some people took the idea of quines and invented programming languages which are specifically designed to produce super short quines. For example, in an esoteric language called HQ9+, a Quine is just 1 byte.

Although, some programming languages are more suited than others to produce quines, writing a quine is not easy because it requires you to exploit the unknown loopholes and rare quirks of the language you’re trying to code with.

I am sure at this point you would be wondering, are there any real life scenarios where Quines are useful? I searched and found that there aren’t many.

So why am I wasting your time?

Writing Quines may seem like a pointless activity. But useless it is not.

The central question is — what’s the point of doing something (writing a quine) when there’s no practical use of it?

For example, why would anyone spend hours and days to find an answer to a silly (and hypothetical) question like this –

If every person on earth aimed a laser pointer at the moon at the same time, would it change the moon’s color?

Randall Monroe puzzled over this question. Monroe is an ex-NASA scientist and the creator of famous nerd comic strip XKCD. I stumbled upon a talk where he takes you through his process — step by step — how he tried to figure out the answer to the above question.

First of all, how do you even begin answering such an absurd query? If you assume that every human being on earth had access to a laser pointer, how would they manage to point it at moon considering that the earth is round and people are over 200 countries spread across several continents, and none of them face the moon at the same time?

What I learned from Monroe’s talk is that the path between a question and its answer can be more interesting than the two endpoints. Being curious allows you to grab every opportunity (even if it comes wrapped as an absurd question) for discovering something interesting.

For example, during his research to answer the laser pointer questions, Monroe found out an interesting fact — when the moon is over the Arabian sea, it’s visible to about 5/7th of the world population simultaneously.

The beauty of Monroe’s work is that he doesn’t stop marching on the trail left by his curiosity. If you could step into his mind, you will get a feeling that he’s not interested in the answer at all. The answers are all by-products of the process. And the process is — keep scratching the itch of a probing mind and never letting the fire of inquisitiveness to die down.

Coming back to Quines, some people wondered — is it possible to write a program which behaves like a Quine not just in one but multiple programming languages?

What does that mean?

For example, the following piece of code is valid Quine both in C# and Java.

Writing a polyquine is fiendishly hard because it demands a hacker’s eye.

What do hackers do? They force a system to behave in a way the creators of the system didn’t intend.

And that’s what happens when you decide to amuse yourself with a seemingly pointless (but hard) task. You may or may not arrive at the answer but you’d certainly unearth things that many others (even those who created the system you’re tinkering with) failed to notice.

While you’re still digesting polyquine, here’s something mind boggling — a Quine Relay. The mother of all Quines, if you will.

It’s a Ruby program that generates a Rust program that generates Scala program that generates …(through 128 languages in total)… REXX program that generates the original Ruby code again.

Source: github.com/mame/quine-relay

The author of this insane Quine is Yusuke Endoh. As Monroe says in his talk, “Every time I figured I had found the answer, I couldn’t resist, so I kept going.”

So Yusuke didn’t stop at Quine Relay. He kept going and created a rotating Quine – A Qlobe.

*  *  *

In 1998, when I was preparing for IIT-JEE entrance exam, there was this book on Physics problems by I.E. Irodov. An impossibly hard book. And, unlike other text books that I studied for JEE, there were no published solutions for Irodov’s problems. Sometimes, I’d wrestle with a problem for weeks only to give up in the end. But all that struggle was worth it.

In fact, I posit that the quickest and most robust (not the easiest though) way to learn deeply about a subject is to pick up an unresolved question from that field and then dive into it with a belief that you could crack it. There’s no downside to this method.

You’ve heard the old saying — Shoot for the stars…