Métaprogrammation

The metaprogrammation is, by analogy with the metadata and the métaclasses, the programming of métaprogrammes. It is the writing of programs which handle data describing of the programs (others or themselves).

It can be employed to generate code interpreted by a Compilateur and to give a constant result, in order to avoid a manual calculation. It also makes it possible to reduce the execution time of the program if the constant result had been classically calculated by the program as for the variable results.

This method does not apply solely to calculations but also to the filling of constant data such as more complex tables or structures. However this technique functions only for constant values. Indeed, if a data handled by the métaprogramme is an entry of the program (such as for example, here number N given by the user), it cannot be known before the execution of the program. It is thus impossible that such a métaprogramme is interpreted by a compiler. Optimization by metaprogrammation is then completely lost.

The metaprogrammation is not limited only to the writing of data containing an intended program with the compiler. It can simply be the handling of a program according to variable entries. For example, a program can, according to its entries, to transfer the code of a métaprogramme. This métaprogramme can then be intended for a later execution or a generation of code.

Process

There exist various ways of proceeding:

  • the use of generating of code ,

  • programming with templates , or models (in C++ or Common Lisp),
  • the use of Protocol to méta-objects (in: MOP) as the MOP of CLOSED or Smalltalk,
  • the use of Macro S.

The first two techniques are available for the languages to static Typage. It is about a powerful but limited form méta-programming. The principle of the generator of code indeed amounts building a compiler including/understanding the semantics of a given language, with additions. This approach is thus not easily portable. The programming containing templates makes it possible to build operators for completely heterogeneous types of data - it is useful in C++. The templates of Common Lisp are more generals. These two techniques relate to only the compilation run. Certain academic languages (as MetaOcaml for example) provide a typing which guarantees that the programs generated by the méta-program are correctly typified.

The languages with dynamic Typage and thus reflexive offer means of Introspection and modification in the course of execution, not only of the values and objects of the field of an application but of the behavior of the system (to hear like the language + its libraries standards). The protocols with méta-objects make it possible to specify the behavior on the level of the classes themselves (one considers the classes, the methods there, like objects of a particular field).

A system of Macro-definition (or macros) makes it possible to carry out transformations of source to source: one can thus add operators new to a language without deteriorating his specification nor to modify the compiler (contrary to the principle of the generators of code). Only the languages represented with S-expression S offer a system of macros satisfactory and usable, because of convergence between the Abstract syntax of the programs and them Syntaxe concretes.

Some expert systems clarify or direct the operation of their or Inference engine by méta-rules méta-knowledge which can be regarded as méta-programs.

Example

For example, to carry out the calculation of a finished sum of the integers lower than 10 (or any other constant value), calculation should be made 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, one can:

  • to create a function which calculates the sum of I going of 1 to N, then to give N = 10 in parameter.

  • to calculate with the hand to use the result directly: 55
  • to make a métaprogramme which will calculate the sum during compilation

In the first case, one creates a function which will make it possible the program to make calculation. This technique can appear useless and to make waste time with the execution of the program because all is constant. The program would be optimized more if calculation were already made before its execution.

In the second case, one calculates oneself the value and one puts it in the source code of the program. This poses two disadvantages:

  • calculation can be false. This would thus cause errors in the results provided by the program. Moreover, it is difficult of déboguer and to find the origin of the problem.

  • calculation can be long and tiresome. Indeed, on the example, only 10 values are calculated, but a greater sum would take much more time. Even if, on this example, one can show that \ sum_ {k=1} ^n K = \ frac {N (n+1)}{2} ( arithmetic continuation ), there does not exist always a simplifying mathematical formula, or then one does not know any.

In the third case, calculation is carried out by programming an intended program with the compiler. This one carries out this métaprogramme to transform it into constant data. It is the method most optimized for the human and data-processing calculators, because the métaprogramme is only one data which, in this case, is evaluated without carrying out the final program.

See too

Random links:Dimitri Yachvili | Frémécourt | Sallebray | Buzhug | Lee Young-ae | Vidoll