Yusuke Endoh has created an astonishing 128 language Ouroborous quine. This Ruby program produces a Rust program, which produces a Scala program, which produces a Scheme program, and so on, progressing through another 124 languages to return to its original state. To understand what this is and how it came to be, we first need to consider the quine.
A quine is a program that prints its own source code to the screen. The term comes from philosopher Willard Van Orman Quine’s paradox “Yields falsehood when preceded by its quotation,” which fails to yield falsehood when preceded by its quotation, making it neither true nor false. The quine in code is named not in reference to the paradoxical quality of the statement, but to its embedding of the quote in itself, where it acts as both the subject and expression of the sentence. The quine program, likewise, flattens the reading of the program with the running of the program. If one were to print its source code to the screen or execute it, the result is the same.
Since this is a fancy way of saying “it prints its own source code,” one might expect such a program to be simple. However, even concise quines in fairly straightforward languages, like Python, are strange looking:
An Ouroborous quine, like Endoh’s Quine Relay cycles through many languages. We can think of an ordinary quine as an Ouroborous quine of only one language. Its primary feature is ending up back where it started, the snake eating its own tail. The multi-language Ouroborous is a cousin of the ployglot, the program which runs in more than one language, since each iteration of the Ouroborous quine holds information for the other steps, passively, until those steps are executed. At any one stage, changes to the program could affect any other step in the chain. Thus, adding a new language into the quine relay often involves touching nearly every line of the program. For an example of what that looks like, here’s a tiny excerpt from the diff for adding the Haxe language, between Haskell and Icon:
According to Endoh, the most challenging transitions were Befunge to BLC8 to brainfuck, as all three are esoteric. It runs through languages strictly in alphabetical order, so there was no opportunity to group easier-to-work-with languages together. BLC8 functions bit-wise, which meant finding a byte-aligned encoding way to work with it, to feed brainfuck. Other esolangs presented challenges as well; Piet, the language that uses images as source code (read the interview with Piet’s creator here) was a bit easier as it came after Perl 6, which bundles Zlib as standard library, making it straightforward to generate a PNG file. Had it followed, say, brainfuck, it would have been a much larger challenge.
Apart from his work in the esoteric space, Endoh is a programmer who helped develop Ruby (which explains why the Quine Relay begins with that language). He served as the release manager for Ruby 2.0 and developed a number of Ruby’s key features. His fascination with quines began with what he describes as “artistic obfuscation” and following the International Obfuscated C Code Contest (IOCCC):
I met Don Yang’s IOCCC 20000 winning entry. This is three-phase Quine. The shapes of each phase are three Japanese words, “aku”, “soku”, and “zan”. It was really interesting to me, so I studied it deeply, and I started writing similar programs in Ruby and C.
Endoh has gone on to win 14 IOCCC awards, two of them just last week in its latest iteration, making him the most-awarded programmer in the history of the contest. You can find more of his work on GitHub, including a radiation-hardened quine (where any single character in the program can be removed and it still works), an encoding of Ruby using only underscore and space, and Ruby encoded into DNA sequences.