Print your own code.

Tim Peters tim_one at email.msn.com
Sun Sep 19 15:51:50 EDT 1999


[someone asks about programs that print themselves, Neil Schemenauer
 gives a Python example, François Pinard relates it to a theorem of
 Kleene, Magnus L. Hetland gives another Python example, and says:
]
> Being one of the (probably many) people who have done this in Python,
> I would be quite interested in the theorem...

Any good text on the theory of algorithms or the theory of recursive
functions (no, they're not really talking about bad factorial functions
<wink>) will eventually get to "the recursion theorem", less commonly called
"the fixed-point theorem".  This is a deep and general result (well,
actually a family of results) relating the things we can compute to the ways
we can "spell" computation.  That any sufficiently general programming
notation necessarily allows expressing a program that produces itself as
output is actually a minor consequence of the theorem.  Alas, it takes a
mountain of machinery to get to that point, so it defies easy explanation.

This field is packed with surprising results, so it's good therapy for the
self-satisfied soul <wink>.  One of my favorites is Rice's Theorem, which
basically says that if you can define some interesting I/O property of
program texts (like it always terminates, or always terminates on input 42,
or is capable of producing 3 as an output, or there exists some input for
which it doesn't terminate, or ...), then there's no effective way to
determine whether a program has that property -- unless the property is true
of all program texts ("may terminate on input 42"), or of none ("terminates
on input 42 and does not terminate on any input").

it's-not-that-functions-are-so-clever-it's-that-spelling-is-so-feeble-
    ly y'rs  - tim






More information about the Python-list mailing list