Almost, but not quine

As my followers will observe (do I even have any?), this blog doesn’t get updated too much, but I recently wrote a fun little variant of a quine that I thought I’d share. A quine is a computer program that prints its own source code, and has its name after the philosopher Willard Van Orman Quine. This concept is somewhat esoteric, but the theory of such programs is well described elsewhere, including how to write one.

But, briefly put: a quine is a fixed point of a programming language, since it simply prints itself when executed. Now, since we’ll need to understand this later on, I’ll include a relatively readable quine in python. The idea behind this quine is simply to write the program into a string, and print that string twice. Here goes:

code = [
"code = [",
"fnutt = chr(34)",
"print code[0]",
"for line in code:",
"    print fnutt + line + fnutt + ','",
"print code[-1]",
"for line in code[1:-1]:",
"    print line",
"]",
]
fnutt = chr(34)
print code[0]
for line in code:
    print fnutt + line + fnutt + ','
print code[-1]
for line in code[1:-1]:
    print line

Now, that’s not very interesting at all – the program simply prints itself, and others have implemented more interesting python quines, I’m sure. But I figured – hey, can’t a program instead modify itself, in order to do something more interesting? As an example, I implemented Wolfram’s rule 30 in an “almost, but not quine”. Simply put, a part of the program is modified at each execution and updated according to rule 30. This is the initial state of the program:

code = [
"code = [",
"# Add new code below here",
"x = '111'",
"rule30 = {'111': '0', '110': '0', '101': '0', '100': '1', '011': '1', '010': '1', '001': '1', '000': '0'}",
"def step(x):",
"    x = '0' + x + '0'",
"    middle = ''",
"    for i in xrange(1, len(x) - 1):",
"        key = x[i-1:i+2]",
"        middle += rule30[key]",
"    return '1' + middle + '1'",
"code[2] = code[2].replace(x, step(x))",
"# Add new code above here",
"fnutt = chr(34)",
"print code[0]",
"for line in code:",
"    print fnutt + line + fnutt + ','",
"startpoint = '1'",
"print code[-1]",
"for line in code[1:-1]:",
"    print line",
"]",
]
# Add new code below here
x = '111'
rule30 = {'111': '0', '110': '0', '101': '0', '100': '1', '011': '1', '010': '1', '001': '1', '000': '0'}
def step(x):
    x = '0' + x + '0'
    middle = ''
    for i in xrange(1, len(x) - 1):
        key = x[i-1:i+2]
        middle += rule30[key]
    return '1' + middle + '1'
code[2] = code[2].replace(x, step(x))
# Add new code above here
fnutt = chr(34)
print code[0]
for line in code:
    print fnutt + line + fnutt + ','
startpoint = '1'
print code[-1]
for line in code[1:-1]:
    print line

After running it a few times, e.g. like this:

python rule30.py | python | python | python | python | python | python

…we get the following printout:

code = [
"code = [",
"# Add new code below here",
"x = '11001000111000001'",
"rule30 = {'111': '0', '110': '0', '101': '0', '100': '1', '011': '1', '010': '1', '001': '1', '000': '0'}",
"def step(x):",
"    x = '0' + x + '0'",
"    middle = ''",
"    for i in xrange(1, len(x) - 1):",
"        key = x[i-1:i+2]",
"        middle += rule30[key]",
"    return '1' + middle + '1'",
"code[2] = code[2].replace(x, step(x))",
"# Add new code above here",
"fnutt = chr(34)",
"print code[0]",
"for line in code:",
"    print fnutt + line + fnutt + ','",
"startpoint = '1'",
"print code[-1]",
"for line in code[1:-1]:",
"    print line",
"]",
]
# Add new code below here
x = '11001000111000001'
rule30 = {'111': '0', '110': '0', '101': '0', '100': '1', '011': '1', '010': '1', '001': '1', '000': '0'}
def step(x):
    x = '0' + x + '0'
    middle = ''
    for i in xrange(1, len(x) - 1):
        key = x[i-1:i+2]
        middle += rule30[key]
    return '1' + middle + '1'
code[2] = code[2].replace(x, step(x))
# Add new code above here
fnutt = chr(34)
print code[0]
for line in code:
    print fnutt + line + fnutt + ','
startpoint = '1'
print code[-1]
for line in code[1:-1]:
    print line

Notice that line 4 was changed several times, according to rule 30. Neat, huh?

I hope someone can tell me how this is useful for anything what so ever!

About these ads
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

One Response to Almost, but not quine

  1. Yes, you do have some ;)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s