Write a Semiquine

10

Suppose you have 2 languages, A and B. A string s is a semiquine in A and B if it satisfies the following conditions:

  1. s is a quine in language A.
  2. s is a polyglot in A and B.
  3. The output of running s as a B program is a different string s', which is a quine in B.

The goal of this challenge is to write a semiquine in two distinct languages. This is , so shortest code wins, with earliest answer used as a tiebreaker.

Rules for Quines

Only true quines are accepted. That is, you need to print the entire source code verbatim to STDOUT, without:

  • reading your source code, directly or indirectly.
  • relying on a REPL environment which just simply evaluates and prints every expression you feed it.
  • relying on language features which just print out the source in certain cases.
  • using error messages or STDERR to write all or part of the quine. (You may write things to STDERR or produce warnings/non-fatal errors as long as STDOUT is a valid quine and the error messages are not part of it.)

Furthermore, your source code must not consist purely of literals (whether they be string literals, numeric literals, etc.) and/or NOPs. For example, `12 is a polyglot in Jelly and Pyth, but in Jelly it is a NOP and a numeric literal, so it is not valid.

Any non-suppressible output (such as copyright notices, startup/shutdown messages, or a trailing line feed) may be ignored in the output for the sake of the validity of the quine.

Rules for Polyglots

The two languages used must be distinctly different. In particular:

  • They must not be different versions of the same language (e.g. Python 2 vs. Python 3).
  • They must not be different dialects of the same language (e.g. Pascal vs. Delphi).
  • One language may not be a subset of the other one (e.g. C vs. C++1).
  • One language may not be a trivial derivative of the other (e.g. Matlab vs. Octave2, brainfuck vs boolfuck vs TinyBF vs ShadyAsFuck vs all the other trivial brainfuck derivatives).

Miscellaneous Rules

  • You may not accept input from STDIN (or any other source). If your chosen language must take input, then either the input must be empty (empty string, piped from /dev/null, etc.), or the output of the program must not depend on the input.
  • For this challenge, you must write a complete program. Requiring additional code to run the solution is not allowed (such as assigning and calling a lambda function defined in the solution).

1: Yes I know that C is not really a subset of C++. It's close enough to be considered one for the purpose of polyglots, though, so I'm counting it for this challenge.

2: Like point 1 above, though Octave is technically not 100% compatible with Matlab, it was designed to be compatible, and is close enough that allowing a Matlab/Octave polyglot would trivialize the challenge.

Mego

Posted 2016-02-08T21:43:29.953

Reputation: 32 998

Are ESMin's copy blocks allowed? – lirtosiast – 2016-02-08T21:51:53.460

@ThomasKwa I'm not familiar with ESMin, so could you give me a quick rundown? – Mego – 2016-02-08T21:53:56.053

There's a command to start a copy block, which gets the source code of everything past it. I think other JS golfing languages might have one too. – lirtosiast – 2016-02-08T21:55:07.573

@ThomasKwa That would not be allowed under the first rule in the quine rules (no reading the source code, directly or indirectly). – Mego – 2016-02-08T21:56:10.837

Can we abuse interpreter quirks which aid in making a quine? – Downgoat – 2016-02-08T23:03:41.237

1@Downgoat So long as it doesn't break any other rules, absolutely. Abusing quirks is kind of the goal of this site :) – Mego – 2016-02-08T23:04:53.673

@ThomasKwa ESMin can't do this one anyway :P – Mama Fun Roll – 2016-02-09T04:16:04.533

Answers

6

Ruby + (JavaScript or Python 3), 76 bytes

This is another challenge that can be solved using my new favourite language, the almost-common subset of Ruby, JavaScript, and Python 3; and given that we don't have any answers in exoteric languages yet, and many people like to see non-golfing-language solutions, I thought I'd contribute one. Even better, the same answer solves the challenge in a variety of ways at the same time.

Here's s:

s="'";d='"';n='print("s="+d+s+d+";d="+s+d+s+";n="+s+n+s+";eval(n)")';eval(n)

This is a quine in Ruby. It isn't a quine in JavaScript or Python 3; if you run it in either of those languages, its output s' has a trailing newline, and thus is different:

s="'";d='"';n='print("s="+d+s+d+";d="+s+d+s+";n="+s+n+s+";eval(n)")';eval(n)

However, s' is a polyglot quine in JavaScript and Python 3! (As usual for me, I'm using the Rhino implementation of JavaScript, both for convenience (as it runs from the command line rather than needing a browser), and because it has a weird standard library in which print writes to standard output.) Both languages produce s' as a result of running either program.

As a bonus, this program also solves the problem in reverse. If you decide to run s' in Ruby, it'll print the original program s again. As such, s' is a valid (but slightly longer, at 77 bytes) answer to the question.

At this point, I'm almost starting to think "differences in whether or not the output ends with a newline being sufficient to count two programs as different" should be considered a standard loophole, given that this seems to be a second set of languages (beyond the well known GolfScript/CJam) in which it's possible to pull the trick off.

user62131

Posted 2016-02-08T21:43:29.953

Reputation:

6

GolfScript + Fission, 19 18 bytes

{".~
'!+OR"
2<}.~

The trailing linefeed is necessary.

This is a true quine in GolfScript. Try it online!

In Fission it prints

'!+OR"

which is a true quine in Fission. Try it online!

Explanation

In GolfScript, anything of the form

{...}.~

is a quine as long as ... leaves a string with ".~" on the stack. The . duplicates the block, such that there's one copy that gets printed at the end, and the ~ executes it, so we can use its contents to print the .~ itself. In this case, the block pushes the string

".~
'!+OR"

and then truncates it to the first two characters with 2<.

The Fission program really works exactly the same as the quine itself, since the first and third line are entirely ignored by the program.

Martin Ender

Posted 2016-02-08T21:43:29.953

Reputation: 184 808

2

Vitsy (safe mode) and Y, 9 bytes, noncompeting.

' Ugrd3*Z

In Vitsy, this is a quine. In Y, this prints Ugrd3*Z'; when run, this prints Ugrd3*Z', which is a quine in Y. Y postdates the question, however.

What Vitsy sees:

 ' Ugrd3*Z
 '         Heyyyy a string! Let's do this!
  ........ Capturing a string! Oh, not done? Hm, let's go back to the start.
 '         There we go.
           (space) ...
   U       (no op)
    g      use count, disabled in safe mode
     r     reverse stack
      d3*  push a '
         Z print it all!

What Y sees:

' Ugrd3*Z
'         Push a space.
  U       Wrap around and capture.
   g      Print that. (Nothing else is printed.)

Conor O'Brien

Posted 2016-02-08T21:43:29.953

Reputation: 36 228

1Note that this only works in Safe mode, or, rather, it has some... unintended side effects in non-safe mode. – Addison Crump – 2016-02-19T22:45:53.727

1

CJam + GolfScript, 9 bytes

"0$p"
0$p

This is a quine in CJam. In GolfScript, it outputs itself with a trailing newline, which is a quine in GolfScript.

I'm not sure whether CJam should be considered a trivial derivative of GolfScript. But I think they are quite different and at least not trivial.

jimmy23013

Posted 2016-02-08T21:43:29.953

Reputation: 34 042