PolyQuine: The rosetta stone of computing
Welcome back to our Quine saga! We've delved into self-replication, hidden messages, and even self-healing code. Now, prepare to be dazzled by another mind-bending concept: PolyglotQuine, or PolyQuine for short.
Before PolyglotQuine, let's understand what a Polyglot itself is. In the human world, a polyglot is someone who speaks, reads, and/or writes in multiple languages. Multi-lingual in other words.
The Rosetta stone
Speaking of multi-lingual, on 15 July 1799, French soldiers under Napoleon, were strengthening the defenses of Fort Julien, a couple of miles north-east of the Egyptian port city of Rosetta (modern-day Rashid). One of the soldiers spotted a slab with inscriptions on one side. He saw at once that it might be important and informed his superiors. A month later, Napoleon himself inspected what had already begun to be called la Pierre de Rosette, the Rosetta Stone.
The stone has three distinctive sections. It was found that the top text is ancient Egyptian hieroglyphic, the middle text to be Egyptian Demotic script, while the bottom is in Ancient Greek all saying the same thing! The decree has only minor differences across the three versions, making the Rosetta Stone key to deciphering the long-lost Egyptian scripts.
The stone currently, is on display at The British Museum after the British stole the stone. Also, check out the 3D Model of the Stone online.
In the coding world, a polyglot is a digital equivalent of the Rosetta Stone, a program that speaks not one, but multiple programming languages fluently.
Essentially, a Polyglot of order n is a program such that:
The same source code is a valid syntax in multiple languages.
Executing the same script in any of those languages yields exactly the same outputs.
Polyglots are a hack whereas Quines, and everything up until now in this blog series, were a general phenomenon of the fixed-point theorem.
Los políglotas son un truco, mientras que los Quines, y todo lo discutido hasta ahora en esta serie de blogs, son un fenómeno general del teorema del punto fijo.
पॉलीग्लॉट्स एक तरकीब हैं, जबकि क्वाइन और इस ब्लॉग श्रृंखला में अब तक की गई सभी बातें, स्थिर-बिंदु प्रमेय की एक सामान्य घटना हैं।
Trivial polyglot
The below syntax for printing is so common that this is a valid code in Python, Ruby, and Perl! The same file can be fed to any of the three compilers/interpreters, and each would execute as a valid program.
print("Hello World!")
SQL Injection is also a trivial exploit of the polyglot phenomenon, where a server naively expects user-controlled input to conform to a certain constraint, but the user supplies syntax which is interpreted as SQL code.
2nd-order polyglot
This is a 2nd order polyglot in C and Bash, that prints Hello, world!
Highlighted in C: Try it online!
cat =13 /*/ >/dev/null 2>&1; echo "Hello, world!"; exit
*
* This program works under cc, f77, and /bin/sh.
*
*/; main() {
write(
cat-~-cat
/*,'(
*/
,"Hello, world!"
,
cat); putchar(~-~-~-cat); } /*
,)')
end
*/
Highlighted in Bash: Try it online!
cat =13 /*/ >/dev/null 2>&1; echo "Hello, world!"; exit
*
* This program works under cc, f77, and /bin/sh.
*
*/; main() {
write(
cat-~-cat
/*,'(
*/
,"Hello, world!"
,
cat); putchar(~-~-~-cat); } /*
,)')
end
*/
3rd-order polyglot
Two commonly used techniques for constructing a polyglot program are to make use of languages that use different characters for comments and to redefine various tokens as others in different languages. These are demonstrated in this public domain polyglot written in ANSI C, PHP, and bash, where all three of them print Hello, world!
Bash: Try it online!
/*#<?php
echo "Hello, world!";// 2> /dev/null > /dev/null \ ;
// 2> /dev/null; x=a;
$x=5; // 2> /dev/null \ ;
if (($x))
// 2> /dev/null; then
return 0;
// 2> /dev/null; fi
#define e ?>
#define b */
#include <stdio.h>
#define main() int main(void)
#define printf printf(
#define true )
#define function
function main()
{
printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/;
return 0;
}
#define c /*
main
#*/
PHP: Try it online!
/*#<?php
echo "Hello, world!";// 2> /dev/null > /dev/null \ ;
// 2> /dev/null; x=a;
$x=5; // 2> /dev/null \ ;
if (($x))
// 2> /dev/null; then
return 0;
// 2> /dev/null; fi
#define e ?>
#define b */
#include <stdio.h>
#define main() int main(void)
#define printf printf(
#define true )
#define function
function main()
{
printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/;
return 0;
}
#define c /*
main
#*/
/*#<?php
echo "Hello, world!";// 2> /dev/null > /dev/null \ ;
// 2> /dev/null; x=a;
$x=5; // 2> /dev/null \ ;
if (($x))
// 2> /dev/null; then
return 0;
// 2> /dev/null; fi
#define e ?>
#define b */
#include <stdio.h>
#define main() int main(void)
#define printf printf(
#define true )
#define function
function main()
{
printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/;
return 0;
}
#define c /*
main
#*/
6th-order polyglot
Polyglot in 6 languages: written in Perl, C, Unix shell, Brainfuck, Whitespace and Befunge, where they all print hitforum
.
Perl5: Try it online!
Bash: Try it online!
Brainfuck: Try it online!
Whitespace: (Not working for now)
Befunge: Try it online!
# define x u /* v
# :::::::::::::::::::>>>>>>>$$$a"muroftih"#[>:#,_@]
eval 'echo "hitforum";exit';sub echo { print "@_\n"}
__END__>++++++++++>++++++++++[>+++++++++++>++++++++++
+<<-]>------.+.>++++++.<---.+++++++++.>--.+++
.<--.<<. */
main() { printf ("hitforum\n"); }
7th-order polyglot
A polyglot written in 7 esoteric programming languages: Malbolge, Brainfuck, Whitespace, Spoon, asdf, 123 and EXCON.
The code has an ASCII art that spells out X-P ESOTERIC POLYGLOT.
450th-order polyglot
There is an ongoing thread in CodeGolf to chain as many languages as possible while still keeping the entire program a Polyglot. Mind-blowingly enough, the count has now reached to 450+
PDF + MP3 polyglot
Here's a file that is a Polyglot of two completely different file types. Saving the file as file.pdf opens as a valid PDF document, and saving the same file as file.mp3 plays a valid audio. Download and try renaming the file yourself!
Download the PDF + MP3 Polyglot
PolyQuine
A Polyglot program is not required to have a self-reproducing quality. But if a polyglot program also becomes a valid Quine in one or more of its possible ways to execute. Then it would be termed as a PolyglotQuine or PolyQuine for short.
However, let's clear up a common misconception:
All PolyQuines are MultiQuines, but all MultiQuines are not PolyQuines.
While both involve multiple languages, MultiQuines can transform into different languages based on command-line arguments, whereas PolyglotQuines remain static but are valid in multiple languages simultaneously. But, as stated earlier, PolyglotQuines don't exist for every combination of programming languages (although it is true that some people have been incredibly smart at constructing them).
2nd-order PolyQuine
This is a Polyglot in C and Haskell, which is also a valid Quine, in both those languages!
Another 2nd-order PolyQuine
This is a Polyglot in Racket and Perl, which is also a valid Quine, in both those languages!
4th-order Palindromic PolyQuine
Here's a bind blowing 4th order PaliPolyQuine in Perl, Ruby, PHP, and JavaScript. PaliPolyQuine is a program that is a Quine, a Polyglot, and a Palindrome.
Perl: Try it online!
Ruby: Try it online!
PHP: Try it online!
JavaScript: (Try in console)
$_='$z=0?"$&".next: eval("printf=console.log;atob`JCc`");printf("%s_=%s%s%s;eval(%s_);//#//;)_%s(lave;%s%s%s=_%s",$d=$z[0]||h^L,$q=$z[1]||h^O,$_,$q,$d,$d,$q,"0"?$_.split("").reverse().join(""):~~reverse,$q,$d)';eval($_);//#//;)_$(lave;')d$,q$,esrever~~:)""(nioj.)(esrever.)""(tilps._$?"0",q$,d$,d$,q$,_$,O^h||]1[z$=q$,L^h||]0[z$=d$,"s%_=s%s%s%;eval(s%_);//#//;)_s%(lave;s%s%s%=_s%"(ftnirp;)"`cCJ`bota;gol.elosnoc=ftnirp"(lave :txen."&$"?0=z$'=_$
Writing your own Polyglot
Let's print the same thing in both C and Python in the same script.
Python / C:
int main(){printf("wubbalubbadubdub");}
print("wubbalubbadubdub")
Now, the problem is, Python throws a syntax error on line 1, and C on line 2.
Let's try to ignore line 1 in Python, by commenting it out.
Python / C:
#int main(){printf("wubbalubbadubdub");}
print("wubbalubbadubdub")
This works perfectly in Python, but the C compiler still doesn't know what print("wubbalubbadubdub")
means.
Luckily, C lets you define MACROS! Let's define a MACRO called print
such that it takes a string x
and replaces it with the current source code of C
Python: Try it online!
#define print(x) int main(){printf(x);}
print("wubbalubbadubdub")
There we go. It's a valid 2nd-order Polyglot!
At its core, an nth-order Polyglot is a game of n clever ways to hide irrelevant code in comments across n programming languages.
Sources and references:
Polyglot, wiki page.
Polyglot, esolang wiki page.
How The Rosetta Stone Unlocked Hieroglyphics, youtube video by Tom Scott.
3D Model of the Rosetta stone webpage by The British Museum.
Code Golf Polyglot, Code Golf post to write the highest nth-order Polyglot.
Write a Palindrome-Polyglot-Quine Code Golf post to write the highest nth-order PaliPolyQuine.
The Polyglot List, blog by Gary P. Thompson II.
PolyQuine, self-referencing blog that has a polyglot-ish paragraph in English, Spanish, and Hindi.
Subscribe to my newsletter
Read articles from Darshan A S directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Darshan A S
Darshan A S
Tricking rocks into thinking, at Google.