Write a Metaquine

19

A metaquine is a program which is not a quine, but whose output, when run as a program in the same language, is a quine.

The goal of this challenge is to write a metaquine. This is , so shortest code wins, with earliest answer used as a tiebreaker. Note that only full programs are acceptable, due to the definition of a quine.

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.)
  • the source code consisting purely of literals (whether they be string literals, numeric literals, etc.) and/or NOPs.

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.

Example

Ignoring the rule which forbids literal-only programs and built-in quining, this would be a metaquine in Seriously:

"Q"

The program consists of the single string literal "Q", which is implicitly printed at output. When the output (Q) is run, it is a quine (Q is the built-in quine function).

Mego

Posted 2016-02-12T18:15:35.927

Reputation: 32 998

2Does the literals/comments/NOPs rule apply to the output (i.e. the real quine) of the metaquote as well? Otherwise ex. T is an easy 1-byte Pyth answer. – Doorknob – 2016-02-12T18:23:45.207

@Doorknob Yes, I'll clarify. – Mego – 2016-02-12T18:25:42.507

8I don't really see where's the difficult point of this challenge. Isn't the strategy "print the string which contains the shortest known quine" pretty much guaranteed to win for each language? – Fatalize – 2016-02-12T18:30:25.773

1@Fatalize Well I guess the interesting question is, can that be done in the same amount or fewer bytes than the quine itself. – Martin Ender – 2016-02-12T18:53:51.160

3@Fatalize Isn't it also possible that you could write a short metaquine that prints a long but valid quine? – Rhyzomatic – 2016-02-12T19:25:18.163

You should add a rule to prevent the empty quine. E.g. any C program which produces no output technically could be a meta-quine, because a blank source code will compile (and print nothing, which happens to be its source code). – Darrel Hoffman – 2016-02-12T22:58:23.637

@DarrelHoffman Empty programs are already a standard loophole – Mego – 2016-02-12T23:35:43.533

What kind of a quine gives shutdown messages? o_O – Conor O'Brien – 2016-02-13T00:59:40.967

@CᴏɴᴏʀO'Bʀɪᴇɴ I'd imagine some interpreter out there always prints "Goodbye!" or something similarly ridiculous on exit. – Mego – 2016-02-13T01:03:37.213

Oh, program shutdown messages. I thought you meant computer shutdown messages ^^" – Conor O'Brien – 2016-02-13T01:04:13.330

@CᴏɴᴏʀO'Bʀɪᴇɴ Well it really depends on your definition of interpreter. It could be a literal OS shutdown message with C/assembly, where the "interpreter" is the processor. – Mego – 2016-02-13T01:05:11.093

So... is SMBF allowed, since the source code and execution happen on the same tape? Example quine (not metaquine): <[<]>[.>], which does use its own code that was on the tape (and can be read/modified) during execution. I guess I'll post an answer, and people can downvote if they don't like it.

– mbomb007 – 2016-02-13T03:19:12.287

Answers

15

CJam, 6 bytes

"_p"_p

Prints

"_p"
_p

which is the shortest proper quine in CJam.

Test it here.

Explanation

Both programs work exactly the same, since the linefeed inside the proper quine is a no-op and only included because it's more expensive to remove it from the output. How the programs work:

"_p"  e# Push this string.
_     e# Duplicate it.
p     e# Print a string representation of the top of the stack (i.e. the string with
      e# quotes) followed by a linefeed.
      e# The other copy is then printed automatically at the end of the program, without
      e# stringifying it.

Side Note

The same works in GolfScript as

".p".p

which prints

".p"
.p

with a trailing linefeed, which in turn is one of the shortest known quines.

Martin Ender

Posted 2016-02-12T18:15:35.927

Reputation: 184 808

10

Pyth, 12 11 10 9 bytes

Knocked off one more byte thanks to @Pietu1998.

jN B".[9N

This prints

.[9N".[9N

which is a quine in Pyth. You can try it out here.

Rhyzomatic

Posted 2016-02-12T18:15:35.927

Reputation: 620

Is the output a new record for the shortest Pyth quine? I don't believe I've ever seen it before. – ETHproductions – 2016-02-12T21:08:29.940

I couldn't find it anywhere else. I came up with it trying to get a metaquine. – Rhyzomatic – 2016-02-12T21:11:11.853

1

Perhaps you should post it as an answer to the original quine challenge :)

– ETHproductions – 2016-02-13T01:08:54.097

2You can get this down to 9 by using jN B".[9N or .[9N"jN B. (jN B"jN B is another true quine at 9: bifurcate identity function and join by ".) – PurkkaKoodari – 2016-02-13T01:36:45.970

@Pietu1998 Thanks! I knew there must be a way to get it to 9. Are there any quines in Pyth that are shorter than 9 bytes? – Rhyzomatic – 2016-02-13T05:50:04.760

@Rhyzomatic None that I know of, mainly due to the fact that Pyth has no short way of eval'ing Pyth code. – PurkkaKoodari – 2016-02-13T14:24:55.707

8

Fission, 6 bytes

!+OR"'

Prints

'!+OR"

Which is the shortest Fission quine. This works because cyclic shifts of the program leave its output completely unaffected.

Try it online!

Martin Ender

Posted 2016-02-12T18:15:35.927

Reputation: 184 808

6

Javascript ES6, 21 bytes

$=_=>`$=${$};$()`
$()

Trivial.

Mama Fun Roll

Posted 2016-02-12T18:15:35.927

Reputation: 7 234

3Happy 3k!!!!!!! – Conor O'Brien – 2016-02-13T01:00:36.660

2@ConorO'Brien Wow, much rep. 3k factorial is already more than Jon Skeet... – NoOneIsHere – 2016-12-10T07:06:51.817

4

Python 2, 29 bytes

_="_=%r;print _%%_";print _%_

Turns out the well-known short python quine is easily turned into a metaquine :)

And, since we don't have to worry about matching the trailing newline, the metaquine is actually shorter!

quintopia

Posted 2016-02-12T18:15:35.927

Reputation: 3 899

beat me to it, again :( I thought of a wrapper, but yours is better – Erik the Outgolfer – 2016-09-14T12:52:09.663

3

Japt, 15 13 bytes

Q+"+Q ³s7J" ²

Test it online!

This program outputs

"+Q ³s7J"+Q ³s7J

which is the shortest known quine in Japt.

How it works

Q+"..." // Take a quotation mark plus this string.  "+Q ³s7J
²       // Repeat it twice.                         "+Q ³s7J"+Q ³s7J
        // Implicit output

Non-competing version, 11 bytes

Q+"+Q ²é" ²

I've just added é, a "rotate" command. So

"+Q ²é"+Q ²é

is now a valid quine.

ETHproductions

Posted 2016-02-12T18:15:35.927

Reputation: 47 880

1

7, 2 bytes, language postdates challenge

The program is two bytes long, and can be interpreted in several ways; as a hex dump:

00000000: 4ff4                                     O.

as codepage 437:

O⌠

or, most readably, in octal (which 7 natively uses):

237723

Try it online!

The workings of this are very simple: it's the standard 7 quine with a no-op stack element inserted between the two halves, to make it different (in this context, 7 separates stack elements). (I could also have inserted it at the start, 723723. I couldn't have interpreted it at the end because trailing 7s are like trailing whitespace, and ignored in the packed encoding, thus it wouldn't be any different from the output program.)

Incidentally, this program is a in hexadecimal, but that's mostly just coincidence.

user62131

Posted 2016-02-12T18:15:35.927

Reputation:

1

Underload, 11 bytes

(:aSS)::aSS

Pretty simple. It prints out (:aSS):aSS, which is the standard Underload quine.

Esolanging Fruit

Posted 2016-02-12T18:15:35.927

Reputation: 13 542

1

Brachylog, 12 bytes

"~k;?w₁"gjw₃

Try it online!

One of 132 same-length metaquine variations on the quine I translated from Fatalize's Brachylog v1 (non-cheating) quine. I actually wrote a (non-golfed, and overall silly) program to print all of them out:

{[[";?","gj","jḍ"],"₁₃₅₇"]j∋ᵐ{ḍ≠&};"\"~~k~ww~w\"~ww~w~n"}ᶠ{lw" metaquines generated:
"ẉ&}w₁ᵐ

Try it online!

There are two parts of the original quine which could be replaced inconsequentially: ;? can be changed to gj or jḍ, and can be changed to , , or . If we keep or change them outside the string literal in one way and inside the string literal in a different way, then the output won't match the source, because whichever variants were present within the string literal will be printed both inside and outside it, resulting in a quine which was not the initial program run.

;?, gj, and jḍ are interchangeable because all three pair the string literal with itself: due to the layout of the program, the input variable ? is unified with the string, so ;? pairs the string with itself; regardless of the layout, gj wraps the string in a list which is then concatenated to itself; likewise jḍ concatenates the string to itself then splits it in half.

The odd-numbered w subscripts are interchangeable because, although originally w could only take 0 or 1, with 0 printing the input to w and 1 printing the first element formatted with the second, the subscript inventory for w has grown into something that externally functions as a bitfield: the low bit of the subscript encodes whether it's direct or formatted, the middle bit encodes whether the output variable from w is unconstrained or set to the input, and the high bit encodes whether the printing is done immediately or if it's delayed until the end of the program so it can be subject to backtracking. (Those subscripts were my first and so far largest contribution to Brachylog.) Since the quine and metaquine don't use the output variable and don't backtrack, all four odd subscripts are equivalent.

Unrelated String

Posted 2016-02-12T18:15:35.927

Reputation: 5 300

1

Ruby, 25 23

puts"puts <<2*2,2
"*2,2

Generates the classic Ruby HEREdoc quine

puts <<2*2,2
puts <<2*2,2
2

Old solution

_='_=%p;$><<_%%_';$><<_%_

Generates itself except with the single quotes replaced with double quotes.

histocrat

Posted 2016-02-12T18:15:35.927

Reputation: 20 600

1

JavaScript, 50 bytes

a="a=%s;console.log(a,uneval(a))";eval(a.slice(5))

Based on the shortest non-source-reading JS quine known to man, but a good 8 bytes shorter. Prints

a="a=%s;console.log(a,uneval(a))";console.log(a,uneval(a))

which is a quine.

ETHproductions

Posted 2016-02-12T18:15:35.927

Reputation: 47 880

1

Fish (><>), 17 bytes

This works using the classic fish quine "r00g!;oooooooo| but adds a { which shifts the entire stack to the left so that the original program isn't a quine, but it's output, when run, is.

"{r00g!;oooooooo|

Outputs:

"r00g!;oooooooo|

which is a fish quine!

gowrath

Posted 2016-02-12T18:15:35.927

Reputation: 885

1

Jelly, 3 bytes (non-competing)

Unfortunately, the required atoms are roughly 2 weeks younger than the challenge.

Metaquine

”Ṙv

Try it online!

How it works

”Ṙv  Main link. No arguments.

”Ṙ   Set the left argument and the return value to the character 'Ṙ'.
  v  Dyadic eval; evaluate the return value with the left argument as argument.
     Executing the atom Ṙ on the argument 'Ṙ' prints a string representation of
     the character, i.e., the string "”Ṙ".
     Finally, v returns its unaltered argument, which is printed implicitly.

Quine

”ṘṘ

Try it online!

How it works

”ṘṘ  Main link. No arguments.

”Ṙ   Set the left argument and the return value to the character 'Ṙ'.
  Ṙ  Print a string representation of the character, i.e., the string "”Ṙ".
     (implicit) Print the unaltered return value.

Since the second prints the first two characters (”Ṙ), this is a proper quine by our definition.

Dennis

Posted 2016-02-12T18:15:35.927

Reputation: 196 637

1

Octopen-Baru, 22 bytes

** Non competing answer

愛[35211].pack歩U')

Ruby output

puts [35211].pack(' U')

Which outputs . Which is a quine in Octopen-Baru!

XiKuuKy

Posted 2016-02-12T18:15:35.927

Reputation: 369

0

Lua, 45 bytes

Quine code not mine.

s="s=%qprint(s:format(s))"print(s:format(s));

will result in

s="s=%qprint(s:format(s))"print(s:format(s))

which is a quine. Glad the ; is optional in Lua.

devRicher

Posted 2016-02-12T18:15:35.927

Reputation: 1 609

0

Pushy, 7 bytes

Non-competing as the language postdates the challenge.

This is the standard quine, but with newlines removed. Newlines mean nothing in Pushy, but are the print operator's standard separator, and therefore needed for a true quine.

95 34_"

Try it online! This outputs:

95 34
_"

Which is the shortest Pushy quine - an explanation on how it works can be found here.

Alternatively, H5-34_" works for the same byte count.

FlipTack

Posted 2016-02-12T18:15:35.927

Reputation: 13 242

0

Foam, 14 bytes

[. .'|: ~|]: ~

This prints the second Foam quine that can be found here. This is actually shorter because . will always put a space between each element, but the bar in the third token allows me to omit the space before it.

Esolanging Fruit

Posted 2016-02-12T18:15:35.927

Reputation: 13 542

I've removed the non-competing bit because newer languages are not automatically non-competing as per site policy. – Mego – 2017-07-09T00:21:58.477

0

Husk, 6 bytes

D"S+s¨

Try it online!

Prints the standard Husk quine S+s"S+s".

 "S+s¨    The string S+s"
D         concatenated with itself.

The quine explained:

   "S+s"    The string S+s
 +          concatenated with
S           itself passed through
  s         the function which returns its string representation.

Unrelated String

Posted 2016-02-12T18:15:35.927

Reputation: 5 300

0

Befunge-93, 22 bytes

"+1_@#`+39:,g0:">:#,_@

I just took the standard quine, put it in quotes, reversed it (so it is loaded on the stack correctly), and then added a stack-print at the end.

It's 8 characters added to the normal quine, so it's very possible there's a better solution.

Kevin W.

Posted 2016-02-12T18:15:35.927

Reputation: 763

Couldn't you just take the standard quine and add a newline? The newline will disappear at the parsing stage, leaving you with just the standard quine (which will print itself). – None – 2016-12-31T22:35:59.223

0

Turtlèd, 52 bytes (noncompeting)

1 less than original quine

@##'@r,r,r-{ +.r_}r{ +.r_}"#'@r,r,r-{ +.r_}r{ +.r_}"

Works in the same way, except that it does not have a character at the end, when run, it will have a character at the end, that does not effect output, because it writes # on to a #. Note the output is slightly different to the quine I have made for the quine challenge, but works the same: " writes #, which is its own last character, like the quine I made. Check out the explanation there.

Destructible Lemon

Posted 2016-02-12T18:15:35.927

Reputation: 5 908