Metaprogramming is the process of creating metaprograms. Typically, programs read input data, process that data, and output some data. Metaprograms read another program, alter it, and then re-return the modified program. Metalanguage is the language to create metaprograms.
The AI known as Skynet becomes sentient in James Cameron’s blockbuster movie The Terminator and starts a full-scale war against humans. This event, referred to as J-Day, takes place on June 18, 2021, in one of the franchise’s numerous incarnations. The movie accurately depicted how interconnectedness and intelligent systems will grow to be essential to our way of life, even if complete sentience and general intelligence are still theoretical concepts in computer science.
What, though, is sentience?
That is a challenging topic that philosophy has been attempting to answer for hundreds of years. One that science has not even been able to address fully. Most people would agree that any sentience that resembles a human will, at the very least, allow for self-awareness and self-actualization.
For instance, I can examine how I view the world and consider my prejudices. I can alter my mental patterns and, as a result, my view on life if I work hard enough. A third party, such as an engineer or software developer, must rewrite the code to modify the software. At least, that is how it has been up until now, but self-writing software is not as far off as one may believe.
Metaprogramming: What Is It?
Through the metaprogramming approach, a program may read, analyze, and alter other programs as well as itself by treating them as data.
It is a program that writes other programs, in other words. The idea is not very novel, despite how absurd it may seem. For instance, I could create a script that scans my code and flags every line that needs an indent; I could even configure it to do so automatically.
Metaprogramming has existed at least since the 1970s. So why bring it up now?
As we have previously said, current CPUs and GPUs have seen an enormous increase in processing power and speed over the past two decades, contributing significantly to the exponential rise of artificial intelligence. We already have all the necessary components for a new paradigm in metaprogramming. Consider a repository of open-source software with hundreds of millions of lines of code. Imagine software that collects all that code, uses natural language processing to understand the comments, and then feeds that information to a machine learning algorithm that learns to anticipate the following line of code based on the last lines of code. Add intriguing advancements in inferential algorithms, extensive data, and data collection.
It resembles a chatbot or generative language algorithm that predicts the following step in a logical sequence using information from the past.
There are already programs like this. They go by names like Github Copilot, IntelliCode, SecondMate, Tabnine, and many others. These all promise an AI assistant to create safe code and assist software engineers in solving challenging problems. Some of them are paid, and others are open source. Is this a good thing, though?
What advantages does metaprogramming offer?
Metaprogramming offers several advantages:
Extensible: Since code is handled as data, extending applications is simple. You may add metadata to add code.
Performance: Everything is concatenated into a string and performed rather than having variables in memory and transferring them around. Abstractions are possible by metaprogramming with no performance cost. Pushing calculations from runtime to compile time may be possible with it.
Programmers may tune programs to specific architectures with the aid of metaprograms. Programs with specialized efficiency perform better than those with generic inefficiency.
Less Code: Over time, metaprogramming automates code writing and lowers manual labor. The usage of macros (such as those found in C or C++) speeds up development and encourages code reuse. Recurring code patterns can be abstracted and reused even when functions, generics, or classes cannot do so.
Correctness: We do not want to manually translate code from a high-level language to machine code or bytecode. Manual processes make it simple to add mistakes. Metaprograms assist with such time-consuming yet essential activities.
Reasoning: Metaprograms like flow analyzers and type checkers aid in learning program characteristics, validating behavior, and enhancing performance.
What are some of the difficulties or drawbacks?
There are a lot of unresolved issues in both theories and practices of metaprogramming. Metaprogramming using C++ templates is not appealing. For metaprogramming, Scala has to redesign its implementation completely. MetaOcaml has keyboarding issues. There is no standard nomenclature. There is no mature tooling. For instance, debugging metaprograms is challenging.
Strings from unreliable sources might result in unsafe code when executed as code. Languages, however, could offer some assistance to make it safer. For metaprogramming, for instance, decorators in Python are more secure than strings.
Writing code as strings makes it harder to read and increases the chance of problems. With strings, numerous regular expressions are used. Unexpected results may appear when strings cannot be digested owing to a programming fault. For novices, metaprogramming gets harder.
It will be challenging for someone who doesn’t grasp the language structures to comprehend other people’s metaprograms, such as macros in Lisp.