Claude Can Quine
A quine is a program that produces its own source code as output. Examples of quines in various languages can be found here. The typical way to write a quine is to break the program down into two sections: data and printer code.
The data section defines a string variable that contains an interpolation placeholder (that is, a place to insert a new string later) and the literal text of the printer code. The printer code prints the string while also interpolating the string into itself. The trickiest part is probably encoding the nested quotes (or other characters) that denote a string literal. For example, consider the Python example from the list of quines linked above:
s='s=%r;print(s%%s)';print(s%s)The data section is s='s=%r;print(s%%s)'; and the printer code is
print(s%s). The modulo sign % is used for interpolation. In the data
section, %r marks the placeholder for the data string itself, which
automatically single-quotes its argument. The printer code prints the string
s with s as the interpolation argument as well.
Some time ago I became curious if a coding LLM could produce a quine in a language in which it had never seen one written before. Luckily, many years ago I implemented my own programming language called stercus, which performs simple operations on a block of memory. I wrote a simple compiler in Python that converts stercus to C, which can then be compiled to an executable binary by your C compiler of choice (I’ve been using gcc).
Using stercus to write any non-trivial program is an interesting challenge due to its extreme simplicity and user-unfriendliness. I have only written very simple stercus programs by hand. For example, here is an implementation of a stercus program to multiply two numbers:
# program to multiply 10 x 25
# note all values in stercus memory are always initialized to zero
[0 10] # put first value in memory position 0
[1 25] # put second value in position 1
(0 # while value in position 0 is non-zero, do:
[2 [1]] # add value in position 1 to the value in position 2
(2 # while value in position 2 is non-zero, do:
[3 +] # add 1 to position 3 (the ultimate result of the multiplication)
[2 -]) # subtract 1 from position 2
[0 -]) # subtract 1 from position 0
[3 .] # output resultThen one can compile and run using:
# compile with int data type and gcc compiler
stercusc multiply.cus -d int -c gcc
# outputs 250
./multiply Back in March I asked Claude to write a quine in this stercus language, based on the language’s documentation (i.e., the README file) and the compiler source code. I forget which model was being used at the time, but Claude spent a lot of time trying to write a Python program to generate the quine, without success. Then in August I tried again with the Claude Opus 5 model, and it quickly created this Python script to generate the following stercus quine:
[1 91][2 50][3 48][4 48][5 32][6 49][7 93][8 40][9 91][10 50][11 48][12 48][13 93][14 91][15 48][16 32][17 57][18 49][19 32][20 58][21 93][22 91][23 50][24 48][25 48][26 32][27 46][28 93][29 91][30 48][31 32][32 51][33 50][34 32][35 58][36 93][37 91][38 91][39 50][40 48][41 48][42 93][43 32][44 46][45 93][46 91][47 48][48 32][49 57][50 51][51 32][52 58][53 93][54 91][55 50][56 48][57 48][58 32][59 43][60 93][61 41][62 91][63 50][64 48][65 48][66 32][67 49][68 93][69 40][70 91][71 50][72 48][73 48][74 93][75 91][76 91][77 50][78 48][79 48][80 93][81 32][82 58][83 93][84 91][85 50][86 48][87 48][88 32][89 43][90 93][91 41][200 1]([200][0 91 :][200 .][0 32 :][[200] .][0 93 :][200 +])[200 1]([200][[200] :][200 +])which is a pleasantly incomprehensible string of 719 characters that can easily be compiled and run to verify that it is indeed a quine. The generated script uses the general approach described above of breaking the program down into a data section and a printer section.
While writing this article, I asked Claude (still Opus 5) to make the quine shorter. It did so, and after a few additional rounds of asking it to try to shorten further, it settled on a revised Python script that produces
[1 41][2 93][3 58][4 32][5 93][6 48][7 91][8 91][9 93][10 45][11 32][12 48][13 91][14 40][15 41][16 93][17 58][18 32][19 50][20 91][21 93][22 46][23 32][24 93][25 48][26 91][27 91][28 93][29 58][30 32][31 52][32 91][33 93][34 46][35 32][36 48][37 91][38 93][39 58][40 32][41 55][42 91][43 93][44 43][45 32][46 48][47 91][48 40]([0 +][7 :][0 .][4 :][[0] .][2 :])([0 -][[0] :])which is another quine but this time only 375 characters long. Aside from some white space tricks to save a few more bytes, Claude does not think a stercus quine can be much shorter than this.
Overall, it was interesting to see the improvement in Claude’s capabilities in the roughly five months between when I first posed this request (and Claude failed to complete it) and when I asked again (and Claude was successful).
Thanks to Galen Leir-Taha for reading a draft of this post.