Send Close Add comments: (status displays here)
Got it!  This site "creationpie.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Programs that write programs
by RS  admin@creationpie.com : 1024 x 640


1. Programs that write programs

2. Code and data
An important part of computer science is that code and data are interchangeable.

That is, code can create data but code can create code and data can be code, etc.

Below are some examples of this in the form of programs that write programs.

3. Programs that write programs
In simple terms, (applied) programming language theory involves programs that write programs (that write programs, etc.).

That is, in systems and programming terms, it is sometimes easier to write a program that writes a program (that writes a program, etc.) to solve a class of problems then it is to write a program to solve the problem. In practical application, (applied) programming language theory involves the full range of programming methodology, from correctness, efficiency, and human issues of structured programming, modular programming, object-oriented programming, and software engineering to programming paradigms such as low-level machine and assembler languages, imperative (assignment-based) languages, and high level declarative, functional, logic, etc.

4. Choose a language
The current language being used is C (file type c).

You can select another language from the following choices.

5. Programs that write programs
Programs writing programsUsing the "Hello world" program in the programming language C (file type c), this page provides examples of programs that write programs.

Specifically, the goal is to write a C program that outputs a program that outputs a program that outputs a program that outputs "Hello world".

For this problem, it may be best to work backwards from the goal. That is, a top-down backward-chaining strategy, instead of a bottom-up forward chaining strategy.

6. Final text output
The final text output is the following.
Hello world


7. Program 1
Here is a C program that outputs "Hello world".
#include <stdio.h> int main(void) { print("Hello world"); return 0; }


8. Program 2
Here is a C program that outputs a program that outputs "Hello world". That is, the previous program.
#include <stdio.h> int main(void) { print("#include <stdio.h>"); print("\nint main(void) {"); print("\n\tprint(\"Hello world\");"); print("\n\treturn 0;"); print("\n\t}"); return 0; }

Note: At this level (and beyond), the double quote now needs to be escaped as a backslash followed by a double quote.

9. Program 3
Here is a C program that outputs a program that outputs a program that outputs "Hello world". That is, the previous program.
#include <stdio.h> int main(void) { print("#include <stdio.h>"); print("\nint main(void) {"); print("\n\tprint(\"#include <stdio.h>\");"); print("\n\tprint(\"\\nint main(void) {\");"); print("\n\tprint(\"\\n\\tprint(\\\"Hello world\\\");\");"); print("\n\tprint(\"\\n\\treturn 0;\");"); print("\n\tprint(\"\\n\\t}\");"); print("\n\treturn 0;"); print("\n\t}"); return 0; }

Note: At this level (and beyond), the backslash itself now needs to be escaped as two backslashes .

10. Program 4
Here is a C program that outputs a program that outputs a program that outputs a program that outputs "Hello world". That is, the previous program.
#include <stdio.h> int main(void) { print("#include <stdio.h>"); print("\nint main(void) {"); print("\n\tprint(\"#include <stdio.h>\");"); print("\n\tprint(\"\\nint main(void) {\");"); print("\n\tprint(\"\\n\\tprint(\\\"#include <stdio.h>\\\");\");"); print("\n\tprint(\"\\n\\tprint(\\\"\\\\nint main(void) {\\\");\");"); print("\n\tprint(\"\\n\\tprint(\\\"\\\\n\\\\tprint(\\\\\\\"Hello world\\\\\\\");\\\");\");"); print("\n\tprint(\"\\n\\tprint(\\\"\\\\n\\\\treturn 0;\\\");\");"); print("\n\tprint(\"\\n\\tprint(\\\"\\\\n\\\\t}\\\");\");"); print("\n\tprint(\"\\n\\treturn 0;\");"); print("\n\tprint(\"\\n\\t}\");"); print("\n\treturn 0;"); print("\n\t}"); return 0; }


11. Fixed point
This process can go on and on. A fixed point of this process is a program that outputs an exact copy of itself. This is left as a future exercise.

Fixed point theory in programming language formal verification is very important but left as a future topic.

Note that every program (that finishes) computes a fixed point. The question is, "Do you know which fixed point is being computed?". (paraphrase from quote of Edsger Dijkstra)

12. Escape characters
Note: Not all programming languages use the backslash as the escape character.

13. End of page

by RS  admin@creationpie.com : 1024 x 640