Programs that print themselves

Status
Not open for further replies.


<tt>printf()</tt> and its ilk are evil, as are preprocessor tricks. I have eschewed them.


Code:
#include <iostream>
#include <string>
using namespace std;int main(int argc, char* argv[]){string s = "using namespace std;int main(int argc, char* argv[]){string s = ",i1="#include <iostream>",i2="#include <string>",b="\\",q="\"",c=";",n="\n";s += q + s + q + s.substr(37, 1);s += 105;s += 49;s += s.substr(62, 1) + q + i1 + q + s.substr(37, 1);s += 105;s += 50;s += s.substr(62, 1) + q + i2 + q + s.substr(37, 1);s += 98;s += s.substr(62, 1) + q + b + b + q + s.substr(37, 1);s += 113;s += s.substr(62, 1) + q + b + q + q + s.substr(37, 1);s += 99;s += s.substr(62, 1) + q + c + q + s.substr(37, 1);s += 110;s += s.substr(62, 1) + q + b;s += 110;s += q + c;string t="s += q + s + q + s.substr(37, 1);s += 105;s += 49;s += s.substr(62, 1) + q + i1 + q + s.substr(37, 1);s += 105;s += 50;s += s.substr(62, 1) + q + i2 + q + s.substr(37, 1);s += 98;s += s.substr(62, 1) + q + b + b + q + s.substr(37, 1);s += 113;s += s.substr(62, 1) + q + b + q + q + s.substr(37, 1);s += 99;s += s.substr(62, 1) + q + c + q + s.substr(37, 1);s += 110;s += s.substr(62, 1) + q + b;s += 110;s += q + c;";string f="cout << i1 << n << i2 << n << s << t << s.substr(53, 7) << (char)116 << s.substr(62, 1) << q << t << q << c << s.substr(53, 7) << (char)102 << s.substr(62, 1) << q << f << q << c << f << endl;return 0;}";cout << i1 << n << i2 << n << s << t << s.substr(53, 7) << (char)116 << s.substr(62, 1) << q << t << q << c << s.substr(53, 7) << (char)102 << s.substr(62, 1) << q << f << q << c << f << endl;return 0;}
 


Hm, that was unnecessarily complicated. I was working around an issue that I eventually made a non-issue anyway (literal strings are limited to around 2 kibytes of text, which is why I was building the string -- but I didn't need the string to be that long after removing a load of redundant bits, so in the end I didn't need to build it).


Code:
#include <iostream>
#include <string>
using namespace std;int main(){string s = "using namespace std;int main(){string s = ", i1 = "#include <iostream>", i2 = "#include <string>", b = "\\", q = "\"", n = "\n",f = "cout << i1 << n << i2 << n << s << q << s << q << ',' << 'i' << '1' << '=' << q << i1 << q << ',' << 'i' << '2' << '=' << q << i2 << q << ',' << 'b' << '=' << q << b << b << q << ',' << 'q' << '=' << q << b << q << q << ',' << 'n' << '=' << q << b << 'n' << q << ',' << 'f' << '=' << q << f << q << ';' << f << endl;return 0;}";cout << i1 << n << i2 << n << s << q << s << q << ',' << 'i' << '1' << '=' << q << i1 << q << ',' << 'i' << '2' << '=' << q << i2 << q << ',' << 'b' << '=' << q << b << b << q << ',' << 'q' << '=' << q << b << q << q << ',' << 'n' << '=' << q << b << 'n' << q << ',' << 'f' << '=' << q << f << q << ';' << f << endl;return 0;}
 

newton_meter

Ars Scholae Palatinae
1,424
Originally posted by stickboy:
This self-replicating Scheme code was presented in the SICP course at Berkeley:

As much as I love Scheme and Lisp, that's a different beast. It's not a program that prints itself, it's an expression that evaluates to itself. If it's run in a REPL, something else (the REPL) may print it.

What you've given is a fixpoint of the procedure "eval". There are many. Here's another:

code:<HR>
Code:
3

(kg*m*m)/(s*s)
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Originally posted by newton_meter:
It's not a program that prints itself, it's an expression that evaluates to itself.

Whoops. Right, I missed that subtle point.

Ok, how about this one:

code:<HR>
Code:
(define (a x) (write x) (newline))
(define (b x) (map a x) (display "(b '(") (map a x) (display "))"))

(b '((define (a x) (write x) (newline))
(define (b x) (map a x) (display "(b '(") (map a x) (display "))"))
))
 

newton_meter

Ars Scholae Palatinae
1,424
Originally posted by reboot:
Why do I remember this as being impossible in theory :confused:

The opposite. I wasn't sure before, but I am now ;). If a programming language can express all primitive recursive functions and has the S-m-n property (basically: substitution of values for variables in the programming language is computable), then Kleene's Recursion Theorem guarantees that self reproducing programs exist.

A sketch of a proof is in "Vicious Circles" by Jon Barwise and Larry Moss.

(kg*m*m)/(s*s)
 

reboot

Ars Legatus Legionis
10,818
Oh cool, thanks for the info, I'll look it up because apparently I need a refresher.

..and you get another
notworthy.gif
for the day, congrats :D
 
Status
Not open for further replies.