Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A program that outputs itself

  1. May 25, 2008 #1

    daniel_i_l

    User Avatar
    Gold Member

    Is it possible to write a program (in any language) so that the output is the code of the program itself?
     
  2. jcsd
  3. May 25, 2008 #2

    Borek

    User Avatar

    Staff: Mentor

    10 LIST

    On ZX Spectrum :smile:
     
    Last edited: May 25, 2008
  4. May 25, 2008 #3

    OmCheeto

    User Avatar
    Gold Member

    Yes.

    I wrote a mini-compiler in interpreted basic about 30 years ago who's output was a machine level code that would perform the exact same function.

    Of course the machine level program was about 10,000 times faster.

    Though "10 LIST" is much simpler and doesn't take 10 hours to write. :tongue2:
     
  5. May 25, 2008 #4

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

    Such a program is called a "quine" (presumably after Willard Quine).
     
  6. May 25, 2008 #5

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    A formulaic way to do it is:

    Choose a specific function f so that f(S) is a program that outputs the string S.

    Let A be a program that inputs a string S, prints it, then prints f(S).

    Now, run the program:
    1. f(A)
    2. A​

    Tweak the details as appropriate for your programming language. An important variation on this method proves the important theoretical result that we can assume any program has access to its own source code.
     
  7. May 26, 2008 #6

    matt grime

    User Avatar
    Science Advisor
    Homework Helper

    #!/usr/bin/perl
    while(<>){
    print;
    }


    save in a file called "program", then do ./program program. Actually, it's entirely possible to do that without needing to pass the name as an argument to perl (this is stored in the variable $0, I think) so something like

    #!/usr/bin/perl
    open($_,"<$0");
    while(<>){
    print;
    }
    close($_);

    should do it, but I've no intention of trying it to find out if I got the right interpolation in the string"<$0".
     
  8. May 26, 2008 #7
    in c++ (or any language which produces a separate compiled file) you can do it easily..
    write a program which reads a text file with name same as that of itself..

    int main(int argc, char* argv[])
    {
    string filename = argv[0];
    readFile(filename);
    }

    it ll work coz the executable and the source code are in different files.. dunno how u ll get it work for a programme spanning multiple files..
     
  9. May 26, 2008 #8

    Borek

    User Avatar

    Staff: Mentor

    The idea (although not listed in the OP) is that the program doesn't accept any input nor doesn't read external files. Besides, you are making several faulty assumptions.

    It will not work - first of all, executable and the source files are different, second, files don't have to be in the same directory.

    This approach may work for interpreted languages. In fact LIST command does just that, although it doesn't need path nor filename.
     
  10. May 26, 2008 #9
    it will work if u have only 1 source code file(*.cpp) and keep the exe file in the same directory. of course, u ll have to get the filename correctly.

    ya i know, i made several assumptions. however, in the OP when he says code of the program, does he mean the machine code? i d be very interested to know of a way to do that in c++..
     
  11. Jun 13, 2008 #10

    Borek

    User Avatar

    Staff: Mentor

    Not only exe must be in the same directory, but both exe and cpp files have to share the same name. It won't work.



     
  12. Jun 13, 2008 #11
  13. Jun 14, 2008 #12
    I was going to recommend the Turing award acceptance speech of Ken Thompson, "Reflections on trusting trust",
    http://cm.bell-labs.com/who/ken/trust.html

    but I see it's one of the links in the Wikipedia page cited above. So I did it anyway. :)
     
  14. Jun 14, 2008 #13
    I would do it like this in C++:

    Code (Text):

    #include <iostream>  // Standard header

    int main()
    {
     
    std::cout << "Hello, world!\n";
     
    std::cout << "#include <iostream>  // Standard header\n";
    std::cout << "\n";
    std::cout << "int main()\n";
    std::cout << "{\n";
    std::cout << "\n";
    std::cout << "std::cout \<\< \"Hello, world!\\n\";\n";
    std::cout << "}\n";

    }
    but I just realised that now the parts that print out the code, would also be needed to be inlcuded, but in doing so would add more lines representing those lines, in effect it would be an infinate regress. I can't see how this would be solved?

    EDIT:::

    D'oh I just realised the solution is the method one poster above has done, just read a file and print it to screen, but just make sure that file is code itself!
     
    Last edited: Jun 14, 2008
  15. Jun 14, 2008 #14

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    That's not a program producing it's source code: that's a program asking an external process for some text, and then displaying it.
     
  16. Jun 14, 2008 #15
    Reading in a file is a boring semi-solution to a clever challenge. The solutions which are discussed and linked to on the wikipedia page exploit much more clever mechanisms to avoid the infinite regress that Anhar found above.
     
  17. Jun 15, 2008 #16

    Ben Niehoff

    User Avatar
    Science Advisor
    Gold Member

    I used to have a quine I wrote in C, but I lost it. This should do it in perl, without reading in a file:

    Code (Text):
    #!/usr/bin/perl
    use strict;

    sub quote;

    $string = <<"HERE";
    #!/usr/bin/perl
    use strict;

    sub quote;

    \$string = <<"HERE";
    placeholder

    \$quoted = quote(\$string);
    \$string ~= s/^placeholder\$/\$quoted/m;

    print \$string;

    sub quote {
      my \$s = shift;
      \$s ~= s/\\\$/\\\\\\\$/sg;
      \$s ~= s/\\\\/\\\\\\\\/sg;
      \$s .= "\\nHERE";
      return \$s;
    }
    HERE

    $quoted = quote($string);
    $string ~= s/^placeholder$/$quoted/m;

    print $string;

    sub quote {
      my $s = shift;
      $s ~= s/\$/\\\$/sg;
      $s ~= s/\\/\\\\/sg;
      $s .= "\nHERE";
      return $s;
    }
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: A program that outputs itself
  1. Zero raised to itself (Replies: 36)

  2. Sphere itself (Replies: 22)

Loading...