7, 2 bytes
7 uses a 3-bit character set, but takes input packed into bytes (and according to meta, languages with sub-byte character sets are counted using bytes for the file on disk). Here's an xxd
dump of the program:
00000000: 4cf4 L.
When giving this file to the 7 interpreter, it will output the following program:
00000000: 4fa6 7f O..
which will then in turn output the original program again.
So what's happening here? There's no source reading involved (actually, I don't think it's possible to read the source in 7), although arguably the program is cheating in another way; let me know what you think. Here's how the program works. (Note that each 7 command has two variants, some of which don't have names and can't appear in the original program. There are twelve commands total, in six pairs. I'm using bold for active commands, nonbold for passive commands, and in cases where the active command has no name, I'm giving it the same name as the corresponding passive command and relying on the bold to distinguish. In the case where both are named, e.g. 7
which is the active variant of 1
, each command gets its own name and the bold is just syntax highlighting.)
231723 Original program, unpacked into octal
231 Push 237 onto the stack
23 Push 23 onto the stack
(implicit) append a copy of the top of stack to the program
2 Duplicate top of stack (currently 23)
3 Output top of stack, pop second stack element
At this point, the 7 interpreter sees that the top of stack contains commands (2
and 3
) that aren't representable, so it escapes the top of the stack, producing 723
(which is). The first command output selects the output format; in this case, it's format 7, "format the output the same way as the program". So the commands get output packed into bytes. Then the program continues:
23172323
(implicit) append a copy of the top of stack to the program
2 Duplicate top of stack (currently 237)
3 Output top of stack, pop second stack element
7 Push an empty element onto the stack
At this point, there's nothing but empty stack elements on the stack, so the program exits. We output 23
earlier. If we escape 237
(and we have to, because it contains unrepresentable commands), we get 7231
. That gets output directly, making the final output of the program 237231
(formatted the same way as the program, i.e. packed into bytes). That's 4fa67f
. (It can be noted that the 1
was entirely pointless in terms of affecting the output; the only reason it's there is to make the two programs different.)
Running 237231
proceeds almost exactly the same way; the difference is that the useless 1
runs just after the first print (and the empty element gets implicitly deleted the second time the current end of the program is reached). Again, the 231
ends up outputting itself, the 23
ends up outputting itself preceded by a 7
, and we get 231723
, the original program.
The observant might note that the two programs, despite being the same length in the language's "native" octal, are different lengths on disk. That's because a 7 program can be padded with an arbitrary number of 1 bits, and the packed format discards the trailing padding. Here's how the encoding happens:
2 3 1 7 2 3
010011001111010011(1...)
4 c f 4 padding
In other words, two bytes, 4C
F4
, are enough to represent the program, so that's all I used.
I'm still not entirely sure what constitutes a semi-quine. – Conor O'Brien – 2016-12-11T23:36:43.347
@ConorO'Brien It outputs a program that outputs the original program – dkudriavtsev – 2016-12-11T23:37:12.173
1I see. The wording is quite vague. – Conor O'Brien – 2016-12-11T23:40:36.453
2Why wont the shortest answer be selected? Is this a Catalog? – ATaco – 2016-12-12T00:38:31.373
We already have another definition for semiquine. This is a cyclic quine. Related. – Mego – 2016-12-12T00:45:16.727
I'd call it square root of quine. (Because I like how quantum computing has the operator square root of not.) – Christian Sievers – 2016-12-12T00:52:55.360
Really, the only differences between this and the challenge I linked are that cheating quines are allowed and the chain needs to be of length 2. I'm not going to VTC this as a dupe (since my vote is a hammer), but I will put forward the proposal that this might be a dupe. – Mego – 2016-12-12T06:46:16.750
2
Just for the record, I'd call it a mutual quine (that challenge requires different languages though).
– Martin Ender – 2016-12-12T08:02:59.370@Mego I'll give my two cents, This is not a dupe as this is Code-Golf, not Code-Challenge. The Quine Permutation Group challenge asks for a very different solution, as is obvious by Dennis's. – ATaco – 2016-12-13T05:08:43.193
@ATaco The other challenge scored based on code length and chain length. With the exception of the few solutions that are infinitely extendable, most of the chain lengths were small, so the score was dominated by code length. – Mego – 2016-12-13T13:09:50.137
Since this is a cheating quine, can we choose to not follow the rules? – Keeta - reinstate Monica – 2016-12-13T18:58:49.317
@Keeta No, cheating only means that you can read the source code – dkudriavtsev – 2016-12-13T19:10:17.203