Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to write a program which prints its own source code utilizing a "sequence-generating-function"

is it possible to write a program which prints its own source code utilizing a "sequence-generating-function"?

what i call a sequence-generating-function is simply a function which returns a value out of a specific interval (i.e. printable ascii-charecters (32-126)). the point now is, that this generated sequence should be the programs own source-code. as you see, implementing a function which returns an arbitrary sequence is really trivial, but since the returned sequence must contain the implementation of the function itself it is a highly non-trivial task.

this is how such a program (and its corresponding output) could look like

#include <stdio.h>

int fun(int x) {
    ins1;
    ins2;
    ins3;
    .
    .
    .
    return y;
}

int main(void) {
    int i;
    for ( i=0; i<size of the program; i++ ) {
        printf("%c", fun(i));
    }
    return 0;
}

i personally think it is not possible, but since i don't know very much about the underlying matter i posted my thoughts here. i'm really looking forward to hear some opinions!

like image 740
guest Avatar asked May 17 '10 14:05

guest


2 Answers

If you know how to encode an array as a function (you seem to be saying you already know how to do this) then the Kleene Recursion theorem guarantees it can be done.

But for doubting Thomases, here's a C example. It has a program generating function that uses only +, -, *, / or calls other functions that use them.

Quines are always possible if you have Turing completeness and freedom to print what you like.

like image 162
sigfpe Avatar answered Nov 04 '22 11:11

sigfpe


What you're referring to is a QUINE. Wiki's article on it is pretty good, with some helpful links. http://en.wikipedia.org/wiki/Quine_%28computing%29

like image 28
corsiKa Avatar answered Nov 04 '22 10:11

corsiKa