# A program to encode Python programs as large integers
def encode(filename):
f = open(filename)
chars = f.read()
f.close()
encoding = "1"
for ch in chars:
val = ord(ch)
encodedVal = "%.3d" % val
encoding = encoding + encodedVal
return int(encoding)
# Example:
#
# >>> encode("prog1.py")
# 1100101102032112114111103049040041058010032032032032112114105110116032034104105034010L
# >>> encode("prog2.py")
# 1100101102032112114111103050040041058010032032032032110117109115032061032091049053044032049055044032049050044032049051093010032032032032115117109032061032048010032032032032102111114032110032105110032110117109115058010032032032032032032032032115117109032061032115117109032043032110010032032032032114101116117114110032115117109010L
# >>> encode("prog3.py")
# 1100101102032112114111103051040041058010032032032032110117109115032061032091049053044032049055044032049050044032049051093010032032032032102111114032110032105110032110117109115058010032032032032032032032032115117109032061032115117109032043032110010032032032032114101116117114110032115117109010L
# >>>
# prog1() prints "hi" and halts, prog2() computes 57 and halts, and
# prog3() crashes. Under such an encoding scheme (generally called
# "Goedel numbering"), the set of all Python programs that run
# correctly is just equivalent to a set of big numbers (the first two
# numbers above are members of this set). Likewise, the set of all
# Python programs that contain bugs also corresponds to a set of
# numbers (the third example above is such a number).
# The process of debugging and editing Python programs is entirely
# equivalent to transforming certain large numbers into other large
# numbers, under this encoding scheme. Likewise, the process of
# transforming certain strings of symbols of a formal system into
# other strings of symbols according to the formal system's rules is
# entirely equivalent to manipulating numbers. What Goedel discovered
# is that any formal system whatsoever can be regarded as just an
# arithmetical system in disguise.