# Pseudo-quine polyglot language-detection counters

Quines are fun. Polyglots are fun, too. Polyglot Quines exist, but we can raise the bar even higher.

Write a file that contains a valid program for languages α, β, and γ. When the file is executed (possibly after compiling it) as a language α or β program, the program's output shall be of the same form as a valid submission to this contest. If your file is executed as a language γ program, it shall output a number. The value of this number is the chain of previous executions of the program interpreted as a binary number.

This explanation might be a bit hard to understan, so here is an example. Let Α, Β, and Γ be functions that execute their input as a language α, β, or γ resp. program and return the output of these programs. Let x be a valid submission to this contest. Then the following expression, where we process x through language β, α, β, α, α, β, and γ in this order, shall yield 41, since 4110 = 1010012.

Γ(Β(Α(Α(Β(Α(Β(x)))))))

You may not assume that penultimate execution in the chain is an execution in language β. For the case where your original submission is directly executed as a language γ program, it shall print 0.

Your program shall behave correctly for up to sixteen compilations in the chain; that is, the highest number your program might print out at the end is 215 - 1. Of course, your program is allowed to support longer compilation chains.

This is a popularity contest to encourage creative solutions. The submission with the highest vote tally wins.

• Can the three languages be equal (even though it would drastically reduce upvotes)? Feb 12, 2015 at 13:07
• @Zgarb I'd love to see a solution where some (or all) of the languages are equal. Let's see how you manage to distinguish equal languages. Feb 12, 2015 at 13:09
• ...I see. :D I read the challenge too hastily. Feb 12, 2015 at 13:11
• @Zgarb how about different versions of the same language? magic += Number(System.env.lang_version[-1]) Feb 12, 2015 at 16:11
• +1 for using greek letters instead of the boring a,b,c or 1,2,3 =) Feb 13, 2015 at 10:33

# Python 2, Python 3, ><> (Fish)

#;n0
import sys
x='\\\'\nn#;n0import sysx=v=int(1/2*2)sys.stdout.write(x[4:7]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[7:8]),sys.stdout.write(x[2:3]),sys.stdout.write(x[8:18]),sys.stdout.write(x[2:3]),sys.stdout.write(x[18:20]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[3:4]),sys.stdout.write(x[4:7]),sys.stdout.write(x[7:8]),sys.stdout.write(x[8:18]),sys.stdout.write(x[18:20]),sys.stdout.write(x[20:32]),sys.stdout.write(x[32:851]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[1:2]),sys.stdout.write(x[2:3]),sys.stdout.write(x[20:32]),sys.stdout.write(x[2:3]),sys.stdout.write(x[32:851])n'
v=int(1/2*2)
sys.stdout.write(x[4:7]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[7:8]),sys.stdout.write(x[2:3]),sys.stdout.write(x[8:18]),sys.stdout.write(x[2:3]),sys.stdout.write(x[18:20]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[3:4]),sys.stdout.write(x[4:7]),sys.stdout.write(x[7:8]),sys.stdout.write(x[8:18]),sys.stdout.write(x[18:20]),sys.stdout.write(x[20:32]),sys.stdout.write(x[32:851]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[1:2]),sys.stdout.write(x[2:3]),sys.stdout.write(x[20:32]),sys.stdout.write(x[2:3]),sys.stdout.write(x[32:851])


## Python explanation

The Python 2 and Python 3 interpreters work similarly except the v=int(1/2*2) variable gets different values (0 and 1) as Python 2 uses float division and Python 3 uses integer division.

In every run they add the expression +0*2 or +1*2 to the first line (after #;n) and to the x string (after the last write command). The ><> interpreter uses the first addition and the Pythons use the second one to create correct quines.

Code after B(A(B(B(x)))):

#;n+1*2+0*2+1*2+1*20
import sys
x='\\\'\nn#;n0import sysx=v=int(1/2*2)sys.stdout.write(x[4:7]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[7:8]),sys.stdout.write(x[2:3]),sys.stdout.write(x[8:18]),sys.stdout.write(x[2:3]),sys.stdout.write(x[18:20]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[3:4]),sys.stdout.write(x[4:7]),sys.stdout.write(x[7:8]),sys.stdout.write(x[8:18]),sys.stdout.write(x[18:20]),sys.stdout.write(x[20:32]),sys.stdout.write(x[32:851]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[1:2]),sys.stdout.write(x[2:3]),sys.stdout.write(x[20:32]),sys.stdout.write(x[2:3]),sys.stdout.write(x[32:851])+1*2+0*2+1*2+1*2n'
v=int(1/2*2)
sys.stdout.write(x[4:7]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[7:8]),sys.stdout.write(x[2:3]),sys.stdout.write(x[8:18]),sys.stdout.write(x[2:3]),sys.stdout.write(x[18:20]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[3:4]),sys.stdout.write(x[4:7]),sys.stdout.write(x[7:8]),sys.stdout.write(x[8:18]),sys.stdout.write(x[18:20]),sys.stdout.write(x[20:32]),sys.stdout.write(x[32:851]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[1:2]),sys.stdout.write(x[2:3]),sys.stdout.write(x[20:32]),sys.stdout.write(x[2:3]),sys.stdout.write(x[32:851])


## ><> (Fish) explanation

When you run the ><> interpreter the code pointer bounces back from the # wraps around the first line and starting from the end of the first line and heading West starts pushing numbers onto the stack. If an operator comes (+ or *) it pops the top two elements from the stack and pushes back the result. With this method we end up with the base2 representation of the previous runs (13 in the former example). This is the desired number so we output it with n and terminate with ;.

• This is pretty nice. Feb 13, 2015 at 13:08