Prove that a program that prints its own source can be written in any programming language whose output...
logic/self.reproducing.program New
New entry, edited from material collected for the archive and not yet in a released version.
Problem
Prove that a program that prints its own source can be written in any
programming language whose output alphabet includes its coding alphabet
and which can simulate a Universal Turing Machine.
-- Seth Breidbart <sethb@panix.com>
[ Editor: problem statement reconstructed from the postings. Seth
Breidbart posed it ("Prove that this can be done in any programming
language ...") in a thread on writing such a program in C, after
remarking "It's quite simple in APL." ]
Show solution
A function which takes one input string can be coded as
{codeA} {Input string} {codeB}
Now construct a process F that takes two arbitrary strings from the
machines coding alphabet and code them into a unique input string I.
Now the reversed process is clearly a codable algorithm and can
be coded as above. Also the process of writing out the first decoded
string, then the undecoded input, and last the second decoded string
is a codable algorithm.
So we implement this thing, with the input string I = F(codeA, codeB)
Done.
Here is a straightforward example of this principle in perl:
#!/usr/bin/perl
$b="#!/usr/bin/perl
\$b=\"::\";
\@a=split(/\\:\\:/,\$b);\$b=~s/([\\\$\\\"\\\@\\\\])/\\\\\$1/g;print \"\$a[0]\$b\
$a[1]\\n\";";
@a=split(/\:\:/,$b);$b=~s/([\$\"\@\\])/\\$1/g;print "$a[0]$b$a[1]\n";
Line 1 is codeA Lines 5 and 6 is codeB, while Lines 2,3,4 is the input
string, properly backslash coded. Perl does the backslash decoding
automatically, and we have to recode the input string for writing
the undecoded version.
... and while APL is inherently impossible to understand, it can be
shown that perl, or C, can be made equally impossible to understand,
by cautious coding.
-- Stein Kulseth <stein.kulseth@nta.no>
[ Editor: from a March 1996 thread "Programs that output themselves",
started by Bill Vanyo <vanyo@ibcco.com>: ]
> Produce a program (C, Pascal, whatever) which prints as its output an
> exact copy of its source code. No tricks like reading the source file
> and printing it.
>
> More challenging: produce a 2 language loop - for instance, a Pascal
> program which outputs a C program which outputs the original Pascal
> program.
>
> I've done the first (In Pascal and C), but not the latter.
This shell script works for Unix sh & ksh:
b=\\;bb=$b$b$b$b;q=\';x='echo "b=$bb;bb=\$b\$b\$b\$b;q=$b$q;x=$q$x$q";echo $x'
echo "b=$bb;bb=\$b\$b\$b\$b;q=$b$q;x=$q$x$q";echo $x
-- Brian A.E. Meekings <baem@mink.att.com>
The more challenging one is actually easier than using only Pascal,
especially when my Delphi refused to execute a line which is longer
than 127 characters. :)
main(){char*p="program p;begin writeln(%cmain(){char*p=%c%s%c;
printf(p,39,34,p,34,39);}%c)end.";printf(p,39,34,p,34,39);}
(in one line, no new-line character at the end.)
-- Wang TianXing <gztxwang@public1.guangzhou.gd.cn>
[ Editor: two more replies in the September 1994 thread "C puzzle". ]
Seth Breidbart writes:
> It's quite simple in APL.
> '''[1 1,26RI13]'[1 1,26RI13]
Easier, in fact:
1U23R11R'''1U23R11R'''
or 1|^HO22R11R'''1|^HO22R11R'''
Where R is rho, U is drop, and |^HO is rotate.
Also, don't forget that Lisp has the Lisplet, the Lambdamatic, and the
Formatic.
(let ((let '`(let ((let ',let)) ,let)))
`(let ((let ',let)) ,let))
((lambda (lambda) `(,lambda ',lambda))
'(lambda (lambda) `(,lambda ',lambda)))
(format t "~a~s ~2:*~s ~s)" "(format t " "~a~s ~2:*~s ~s)")
I think Mike McMahon is the author of the Lisplet.
Traditionalists hold with the variableless Fortran IV version.
PRINT 1
1 FORMAT(6X,7HPRINT 1/ 2(36H 1 FORMAT(6X,7HPRINT 1/ 2(36H
.), 2(/5X,1H.,66H), 2(/5X,1H.,66H
.), 2(/5X,1H.,66H), 2(/5X,1H.,66H
.) /6X,4HSTOP/6X,3HEND)
.) /6X,4HSTOP/6X,3HEND)
STOP
END
If you want a C version that prints with the trailing newline, assuming
'\n'==10 is bletcherous. Instead, use:
main(){char*a="main(){char*a=%c%s%c,q='%c',b='%c%c';
printf(a,q,a,q,q,b,b,b,'%cn');}%c",q='"',b='\\';
printf(a,q,a,q,q,b,b,b,'\n');}
(ignoring the internal line breaks).
-- Dan Hoey <Hoey@AIC.NRL.Navy.Mil>
Here is a one-liner. I have broken it up in three lines, but if you
want to compile and run it, you should merge them into one line.
main(){
char c='"',*s="main(){char c='%c',*s=%c%s%c;printf(s,c,c,s,c,10);}%c";
printf(s,c,c,s,c,10);}
The printout will end with a new line, and it is assumed that the value
for '\n' is 10. If it is acceptable that the printout ends with no new
line, this code will do:
main(){
char c='"',*s="main(){char c='%c',*s=%c%s%c;printf(s,c,c,s,c);}";
printf(s,c,c,s,c);}
-- Goran Wicklund <euagwd@eua.ericsson.se>
[ Editor: more replies in the March 1996 thread "Programs that output
themselves" started by Bill Vanyo. ]
On Fri, 15 Mar 1996 15:25:12 GMT, Bill Vanyo (vanyo@ibcco.com) wrote:
: Produce a program (C, Pascal, whatever) which prints as its output an
: exact copy of its source code. No tricks like reading the source file
: and printing it.
I didn't program any of the following myself, but anyway:
C programs:
--
char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
Used by Mike Davis ..!att!ihlpm!jmdavis
--
A shorter one, in Miranda by Maarten Fokkinga:
t c=c++show c;self=s"t c=c++show c;self=s"
--
Another self printing program by Teemu Suikki
main() {char *p,a;printf(p="main() {char *p,a;printf(p=%c%s%c,a='",a='"',p,
a);printf(p="%c',p,%ca);printf(p=%c%s%c%c,a,10,a,p,a,10,a,%c%s%c,a,10);}%c"
,a,10,a,p,a,10,a," *** Email: zuikkis@sci.fi *** IRC: Zuikkis *** ",a,10);}
--
-- Hanspeter Schmid <schmid@isi.ee.ethz.ch>, March 1996
Here's a self-reproducing C program that is also a palindrome!
/**/char q='"',*a="*//**/char q='%c',*a=%c%s%c*/};)b(stup;]d[b=]d-472[b)--d(elihw;)q,a,q,q,2+a,b(ftnirps;)b(stup{)(niam;731=d tni;]572[b,"
,b[275];int d=137;main(){puts(b);sprintf(b,a+2,q,q,a,q);while(d--)b[274-d]=b[d];puts(b);}/*c%s%c%=a*,'c%'=q rahc/**//*"=a*,'"'=q rahc/**/
The shortest self-reproducing C program is as follows:
Wasn't that short? The C compiler actually accepts a zero-byte file
as a valid C program. The output of the program is itself, namely
nothing.
-- Glenn Rhoads <rhoads@george.rutgers.edu>
> The shortest self-reproducing C program is as follows:
>
>
> Wasn't that short? The C compiler actually accepts a zero-byte file
> as a valid C program. The output of the program is itself, namely
> nothing.
The C *standard*, however, requires (in sections 5.1.2 - 5.1.2.2.1)
that:
# "program startup" occurs when a designated C function is called by
# the execution environment.
...
# In a freestanding environment ... the name and type the function
# called at program startup are implementation-defined.
...
# In a hosted environment ... The function called at program startup
# is called main.
So if *a* C compiler accepts a zero-length program and produces an
executable that does nothing, then it is accepting an extended version
of the language.
A zero-length *source file* is permissible, but as explained above, it
cannot constitute the entire program.
-- Mark Brader <msb@sq.com>, March 1996