Meta Intelligence - Writing Programs That Write Programs (Part 1: Genetic Evolution)
In today’s article, we are going to learn how to write programs that write programs. The notion of programs that can generate other programs is called metaprogramming and by the end of this article, you will be able to create your own code-generating system.
Take a look at the following example of a self-generated program that prints ‘HI’ to the console.
If you are confused by the above code, don’t worry - you are not alone. It’s written in an esoteric, though Turing complete programming language called Brainf*ck which is notorious for i’s unreadability. However, while being extra hard to understand for humans, it’s very simple to understand for computers.
It has only 8 instructions
“>” Increment the pointer.
“<” Decrement the pointer.
“+” Increment the byte at the pointer.
“-” Decrement the byte at the pointer.
“.” Output the byte at the pointer.
“[“ Jump forward past the matching ] if the byte at the pointer is zero.
“]”] Jump backward to the matching [ unless the byte at the pointer is zero.
“,” Input a byte and store it in the byte at the pointer.
so it’s relatively easy to write an interpreter for it. If you are curious, take a look at the Python interpreter I wrote for this project.
But why do we need this weird language in the first place?
Let’s define our project’s problem first.
We are striving to generate a program that does something simple, like for example printing some text on a screen.
Okay, but what is a ‘program’?
Without going too much into the technical details, we can define a program as a collection of instructions that are being executed by a computer.
With that being said, our goal is to find this specific collection of instructions that after being executed by a computer, will output a specific text on a screen.