'Quining' is the act of Quining
Code that references itself. A short quest into the huge abyss of quines.
Intro
Quines are a wonderful display of a basic paradox:
'Yields falsehood when preceded by its quotation' yields falsehood when preceded by its quotation.
Yes, that's an actual quote from the creator of quines, Willard Van Orman Quine. After reading this post, not only will you be able to flex on your friends that you know what a quine is, but also the name of this cursed philosopher. Apparently he was extremely hostile to Principia Mathematica, also known as 'the math book', claiming the logic section was not actual logic. But he also lost a huge argument with other logician, so you can draw your own conclusions. What a loser.. eats doritos.
The term, originally coined as quining, first appears in the excellent book Gödel, Escher, Bach, where it's used for very smart self referencing and recursion. This book also contains some computer science mentions, albeit in a cryptic fashion. Quite a good read, if you are completely wasted on a friday night.
You may have heard of the Liar paradox, so riddle me this, batman:
This sentence is a lie.
Another example of a piece of information existing solely for referencing itself. Attempting to solve the truthiness of that statement is paradoxical. Apparently, in fuzzy logic, the answer is 0.5
, looks like we have a quantum cosplayer.
So now you know, next time someone asks if you ate their lunch, your reply being a floating point number is completely valid.
As a quick summary, a quine is a self-referencing piece of information. That's it. It's just there, stuck on a loop of defining its own existence, menacingly. So, what can we do with it and why is it related to coding at all?
The Thing
A quine in coding, by definition, should not self-reference using external inspection. That means, it should not output its source code by reading itself as a file. It needs to actually compute it in a sort of quoted matter. Ironic, given that our lord and savior Willard originally meant for a quine to use quotation, of course he wouldn't be that smart to know that in software, that's exactly how you achieve it. Or would he? A mysterious man.
This could very well start with a simple example of a quine, but let's give it a little twist and cheat, just for the sake of understanding exactly what we are dealing with here. Also, we would be honoring the term introduced by Mr. Douglas Hofstadter of unasking the premise. We shouldn't read the file or we can't read the file?
As a quick note, yes I will use Go for all of the examples. No, I'm not "a Go one trick pony". Stop pointing at Javascript, quines are cursed enough, don't bring another horseman.
Running go run main.go
on that file will yield its own contents. I know, that's no fun! I used its context information (the source code file) to output itself. It did not compute anything on its own. Additionally, that is the exact same output as cat main.go
(sorry windows folks, we don't use that here).
Now that we know what isn't fun, let's get into the fancy tools.. let's combine unescaped strings and substitution.
I would show you the output, but you already know what it is. If you do not, refer to this great post about quines.
Now seriously, how? There is no function recursion here, no obvious self referencing code, and certainly nobody is cheating like before, what changed?
The trick remains the same in all languages. The string containing the whole source code should be re inserted into itself on runtime print. And we need to do it with the escaped characters, otherwise the output will be wrong. Pay attention to the %q
inside the quine
variable. You can try it yourself by replacing the fmt
specifier to an %s
, the output will look like this:
That change breaks it, because the output gets its line endings interpreted, along with the removal of the quotation marks.
And.. congratulations! Willard is pleased, he believes this Go quine is actual logic.
But wait, there's more
If your heretic language was not mentioned in this holy post, you can refer to the extensive Rosetta Code entry about quines. It has quines in basically all languages. Yes, even in Brainfuck. And don't get me started on the Prolog one. Hint: it's extra cursed.
Additionally, what other things can quines be used for? Can we build.. an ouroboros of language outputs?
Oh it's possible, in fact a madman did it, it's called quine-relay, it's a 128 languages (at the time of writing) cycle of madness, where one language creates the source code of the next one, until it circles back to the initial one, Ruby. Ah, Ruby developers never cease to amaze me, it's like they are too angry to die. It's okay, I respect their gems
for the console, like colorls
and istats
.
Logic, code and.. ships?
If you got this far then I will strike you down with a question that might be the existential dread you were looking for!
Quoting the famous question of the Ship of Theseus:
[...] whether an object that has had all of its original components replaced remains the same object
Is the output of a quine the same as the original quine? Food for thought.
But certainly, I am not a quine.
Thank you for reading.
If you liked this post, feel free to subscribe. Until next time!