# (6 of 7) PolyQuine: The rosetta stone of computing

Welcome back to our [Quine](https://darshan.hashnode.dev/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**](https://darshan.hashnode.dev/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](https://en.wikipedia.org/wiki/Rosetta_Stone).

The stone has three distinctive sections. It was found that the top text is ancient Egyptian [hieroglyphic](https://en.wikipedia.org/wiki/Egyptian_hieroglyphs), the middle text to be Egyptian [Demotic](https://en.wikipedia.org/wiki/Demotic_(Egyptian)) script, while the bottom is in [Ancient Greek](https://en.wikipedia.org/wiki/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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723319981444/93627301-e6b1-4722-8336-76e0e45c1d2c.jpeg align="center")

The stone currently, is on display at [The British Museum](https://artsandculture.google.com/story/the-rosetta-stone-the-british-museum/OQVRt6m8dEG4rw?hl=en) after the British stole the stone. Also, check out the [3D Model of the Stone](https://sketchfab.com/3d-models/the-rosetta-stone-1e03509704a3490e99a173e53b93e282) 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.

```Python
print("Hello World!")
```

[SQL Injection](https://en.wikipedia.org/wiki/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!`

[Website source](https://www.nyx.net/~gthompso/poly/applin.txt)

Highlighted in C: [Try it online!](https://tio.run/##XY5NDoIwEIX3nOLJQn5SbNAFCyJrD@AFaluhsRRSQEyMXB2LsHIWk8w3770ZnpSczzNnPc7pCTSmKKiQT2oGrXEs9mkOyasG/kVq3RCMjdVi5zv6Ur0XezFwrVSH1jalZfWyf3QYjJAWnBPcs4yAGQF6U4Z21WHx0Bw1UyaM8Pbwq9GqXoae@yOZEtdXTGMShE6@TuTvh40upihHO/S8YjackmmNcOzjEjZVFETbKWmEi5znLw)

```C
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!](https://tio.run/##XY5BDoIwEEX3nOLLQoEUG3XBgsjaA3iB0lbbWAopICZGro5FWDmLSebN/3@mZK2aJs46nA8n0ISioEI@qe2NwbHYHnJIrmqEF2lMTTDUzohN6OlLd0ESJMBV6RaNq@@OVfP@0aK3QjpwTnDLMgJmBWipLW3VfvbQHBXTNorxDvCrwelORoH/Ix1T3xdME7KLvHyZyN8PK51NcY6m77hiLhrTcYnw7OMTVlW8i9dT0gofOU1f)

```bash
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!](https://tio.run/##bVFLTsMwFNz7FEPaRRIBRkis3M@WQ3STxi/YkrEj1w6RgLtwFi4WTD9KUnXleTPz5tnP@@qghgG8XKy2rWoZqFYO2SsZ4@7x4byRd5ngHM8bcEkdt9EYTPEOguHKINCvq0Qv@/WLuBZPHbpBni/7orjRHBRZBk8heounW/GNZlhIarQl0O/PdjOWe5Q8VdrWJkrC6hCkdo9q4nivtM0LaBtOsHNaFqPc@qQ05yMf@eAjYeJroq2DdummF3ROZvhkl5T5Jnc2@0/h5XwhX3jz1OKhO84ouZg9/nscWaePYscpiSv5MPwB)

```Bash
 /*#<?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!](https://tio.run/##bVFLTsMwFNz7FEPaRWIBRkis3M@WQ3RT4hdiydiWsUMk4C6chYsF04@SVF153sy8efazb/0wQPDFautbz0B161A8kzHuFh8uGHVTSCHwuIFQ1AmbjMEU7yAZLgwS/Xqf6WW/fpKX4rFDNyjLZV9VV5pjS5YhUEzB4uFafKMZFooabQn0@7PdjOULuMiVtrVJirB6j0q7@3bieNtrW1bQNh5h57SqRtmHrDSnoxz5GBJh4muSraN2@aZndEpm@GTnlPkmd7b4TxF8vpAvvAbyuOsOM7iQs8d/jyPr/FHsMCVzXAzDHw)

```PHP
 /*#<?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
 #*/
```

C: [Try it online!](https://tio.run/##bVFLbsMgFNxziqmThY2SUlXqyvlse4hsUniOkShYFFxLae/Ss/RiLs1HtqOsmDczbx485PIgZd9D8Nlq29QNA8naIXslY9wCn84b9ZCVQuB5A6GoFTYagzHeoWS4MZTo1vtEz7v1S3krnjt0hTyfd0VxpznUZBk8hegtnu7FV5phpqjSlkC/P9vNUL6Bi1RpK01UhNVHUNo91iPH@17bvIC24Qxbp1UxyI1PSnU58oEPPhJGvipaGbRLN72iSzLDkV1Tppvc2ew/RfDpQr5w8NRg2Z5mcFFOHv89jJTpo9hpSuK46Ps/)

```C
 /*#<?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](https://en.wikipedia.org/wiki/Brainfuck), [Whitespace](https://en.wikipedia.org/wiki/Whitespace_(programming_language)) and [Befunge](https://en.wikipedia.org/wiki/Befunge), where they all print `hitforum`.

[Website source](https://www.retas.de/thomas/computer/programs/useless/misc/polyglot/c.html)

* Perl5: [Try it online!](https://tio.run/##pY/JDoIwFEX3/YqbQuKA1pUbaRoWuvUH0DSoJTSRIQiExPDtiMWhiUtPu7j3TX0tVHld972Di4p1ptCixmoOQwPiYPOLGHFdN6JpXeZxpRPqhGLjLGRwBFFNdMVEnZMcNNFVnJd1Sn3V6mri3@oTTOaOotRZBRrIQ0a78UUQKXf7rZTC@2DJ0NJ2fOjzOF8exdLAPPZKMm7sGyZGBxvyLGKcM8xXIGmks@nsvV2M6ecHw5YzH923z1x70L@BlyPD6fsH)
    
* C: [Try it online!](https://tio.run/##pY/JDoIwFEX3/YqbQiKDlL00jQvd@gNqGsQiTQQMgjExfjticWji0tMu7n1TX7PokGV972Cvcl0pXNEhDmC4gDiY/SJGXNdNadk1dd7qgjprMXOmcr4FUZf0iInKihq00G1eN11JE3XV7SQ5dzuYzA2nRlct6FxuKnofXwSRcrlaSCnCD5ZcW9qOD30h59FWRAYWsleScWPfMDE62JBnEeOcIYhBylRXnv/eLof3@cGwpZ/g/u0z1x70b@DlyHD6/gE)
    
* Bash: [Try it online!](https://tio.run/##pY9LDoIwFEXnXcVNIUFA61yahoFO3YCSBrWEJgKGjzExrh2xKDZx6GkH975fXw9pk/e9g5PKdKlwQ4dlAMMVxMHqFzHium5Ki66uslbn1NmJlTOXcQKirukZnjrmFWiu26yqu4JG6qZbL2q6A0zmjkutyxY0lvuSPsYXQaTcbNdSinDCkjtL2/GhL@R8kYiFgYXsnWTc2A9MjA425FXEOGcIliBFqsuZ/9kuw2z6wbClH@Hx7TPXHvRv4O3IcPr@CQ)
    
* Brainfuck: [Try it online!](https://tio.run/##pZBLCsIwFEXnWcUlLfipxrkNoQOdugGVUDWhQdtKbUQQ115rqjXg0JMM3n3/ZFelptB2f2yaAAelTaFwg8VsDMcVJMD8F9ERhmFKc1uVujYZDdZiHkxksgVR1/SEgdpnJWhmal1WNqexupl6EF/sDi5yx7kyRQ2ayE1BH91EECmXq4WUIurxzLVn@/62LuJ8uhVTB4vYO8i4kx@Y6BR8yCuJcc4wnoHk7ZcMR5/tNIb9C9otRzEe3zp3/Ub/Ot6KtKdpng)
    
* Whitespace: (Not working for now)
    
* Befunge: [Try it online!](https://tio.run/##pY/PjsIgEMbvPMUX2sQ/3eLVtYR40KsvoIZUhS3JlppuMW6Mz167VF0Sj/6Gw/fNMMywU9rZL5V@TtPjr5dtG@GgtLEKZzhMxvCcQCLMXhE9cRzntHR1pRtT0GgtZtGHnG9B1Cn/xkDtiwq0MI2ualfSTJ1NM8h@3A6@csGxNrYBncuNpdd@IoiUy9VCSpE8CeQ60GG@60s4T7ci9bCE3YuMe/uAid4hhPxdYpwzjCcgZW7scPTYTmP4/EG35SjD9b/Pn/ChdxN3R7po2xs)
    

```c
# 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](http://esolangs.org/wiki/Malbolge), [Brainfuck](http://esolangs.org/wiki/Brainfuck), [Whitespace](http://esolangs.org/wiki/Whitespace), [Spoon](http://esolangs.org/wiki/Spoon), [asdf](http://esolangs.org/wiki/Asdf), [123](http://esolangs.org/wiki/123) and [EXCON](http://esolangs.org/wiki/EXCON).

[GitHub](https://github.com/kenrube/Esopoly?tab=readme-ov-file)

The code has an ASCII art that spells out *X-P ESOTERIC POLYGLOT*.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723320220823/37c8d9c9-5183-4d81-8463-15103274bf8d.png align="center")

## 450th-order polyglot

There is an [ongoing thread](https://codegolf.stackexchange.com/questions/102370/add-a-language-to-a-polyglot/266144?answertab=modifieddesc#tab-top) 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+

[450+ Polyglot](https://codegolf.stackexchange.com/questions/102370/add-a-language-to-a-polyglot/266144?answertab=modifieddesc#tab-top)

## 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](https://www.lab6.com/1)

---

# 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](https://darshan.hashnode.dev/multiquine), but all MultiQuines are not [PolyQuines](https://darshan.hashnode.dev/polyquine).

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!

[GitHub](https://github.com/lamarqua/polyquine/blob/master/test.c)

## Another 2nd-order PolyQuine

This is a Polyglot in Racket and Perl, which is also a valid Quine, in both those languages!

[GitHub](https://github.com/lamarqua/polyquine/blob/master/quine2.pl)

## 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.

[Codegolf post](https://codegolf.stackexchange.com/a/156562)

* Perl: [Try it online!](https://tio.run/##TY/BisIwFEX38xXy5tUmUNK6mE1D6MKdCH6AaKo1aiU0tSkipfTXO2kjzJC3yLnvXnJTq0b/jCNKEWInkgxwCaxS7zZdqNdJE6ibsmqvojCVNVoxbW781JpzvlkXOVDu1wQCK0Vgp8Pn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6uawRohS0SPoZD4NU2/)
    
* JavaScript: (Try in console)
    

```Perl
$_='$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:

```Python
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:

```Python
#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!](https://tio.run/##K6gsycjPM/7/XzklNS0zL1WhoCgzr0SjQlMBSCnkJmbmaWhWg8XSgILWtVwQeaXy0qSkxBwQkVKaBERKmv//AwA)

C: [Try it online!](https://tio.run/##S9ZNT07@/185JTUtMy9VoaAoM69Eo0JTAUgp5CZm5mloVoPF0oCC1rVcEHml8tKkpMQcEJFSmgRESpr//wMA)

```Python
#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](https://en.wikipedia.org/wiki/Polyglot_(computing)), wiki page.
    
* [Polyglot](https://esolangs.org/wiki/Polyglot), esolang wiki page.
    
* [How The Rosetta Stone Unlocked Hieroglyphics](https://www.youtube.com/watch?v=yeQ-6eyMQ_o), youtube video by Tom Scott.
    
* [3D Model of the Rosetta stone](https://sketchfab.com/3d-models/the-rosetta-stone-1e03509704a3490e99a173e53b93e282) webpage by The British Museum.
    
* [Code Golf Polyglot](https://codegolf.stackexchange.com/questions/102370/add-a-language-to-a-polyglot?answertab=modifieddesc#tab-top), Code Golf post to write the highest nth-order Polyglot.
    
* [Write a Palindrome-Polyglot-Quine](https://codegolf.stackexchange.com/questions/93324/write-a-palindrome-polyglot-quine) Code Golf post to write the highest nth-order PaliPolyQuine.
    
* [The Polyglot List](https://www.nyx.net/~gthompso/poly/polyglot.htm), blog by Gary P. Thompson II.
    
* [PolyQuine](https://darshan.hashnode.dev/polyquine), self-referencing blog that has a polyglot-ish paragraph in English, Spanish, and Hindi.
