Skip to topic | Skip to bottom
Home
Projects
Projects.MetaGeneIntror1.7 - 20 Nov 2003 - 17:54 - FrancisMaes?topic end

Start of topic | Skip to actions
Many languages have of a two-layers evaluation model: some calculations are done at compile-time, some others at execution-time. More generally, the field of partial evaluation research is focused on performing computations as early as possible in the compilation and evaluation chain. The C++ language features advanced compile-time evaluation mechanisms. Recursive algorithms can be written using template classes. These C++ template meta-programs are entirely executed at compile-time. The template instantiation process allows many common programming structures to be emulated (e.g. if-then-else, for-each, ...). Veldhuizen has showed that it is possible to implement a Turing machine using C++ template instantiation.

Here is a simple example of a C++ meta-program:

template<unsigned N>
struct fact {
  enum {res = N * fact< N -  1 >::res};
};
template<>
struct fact<0> {
  enum {res = 1};
}; 

enum {fact4 = fact< 4 >::res};

Template classes were created first to fulfill a need of genericity. This is illustrated in the STL (Standard Template Library), where abstractions such as containers and iterators are generic according to the type of the element. However, those abstractions were not designed for writing compile time functions. As a consequence, developping, debugging or maintaining C++ meta-programs is a difficult task. Type checking is quasi inexistent, error messages are generally not adapted, some grammar incoherences occur, and so on. Because of these drawbacks, very few C++ developers use meta-programming techniques.

Metagene is a program transformation tool which allows one to write C++ template meta-programs in a friendly functional style. It consists in a translator which transforms functional algorithms into C++ template meta-programs. Using a functional style language is relevant for several reasons:

  • The C++ templates instantiation process is very close to the evaluation process of a functional program (pattern matching, nested scopes, no side-effects...). For this reason, most existing template meta-programs are written using a functional paradigm (use of recursive functions, no side-effects, higher order functions...).
  • Many open source implementations of functional languages exist, with more or less reflexivity. Our prototype is built on top of a complete existing architecture: Objective CAML and its powerful preprocessor CAMLp4.
  • Using a functional paradigm allows us to include a real type checking model in our language. This is an elegant solution for solving the lack of type checking in C++ meta-programming.

The previous example of C++ meta-program can be written with Metagene the following way:

let rec fact = function
    n -> n * (fact (n - 1))
  | 0 -> 1
let fact4 = fact 4

Metagene is made of several parts, evolving concurrently:

  • The Metagene language, a subset of OCAML.
  • Transformations specifications.
  • A translator prototype written in OCAML with CAMLP4.

The Metagene language includes the most important functional features: functions as value (in parameters or in results), partial application, pattern matching, recursive functions, nested scopes, builtin types (integer, Boolean, string) and variant types (such as lists and trees). The Metagene typing mechanism is the same as the Objective CAML one. Thanks to this proximity, some of the Objective CAML standard libraries are available in Metagene, such as the list module, or parts of the string module.

A meta-program is a program that manipulates or generates programs. Since Metagene is a typed language manipulating C++ pieces of code and C++ types, these structures are seen as particular Metagene values. Thus, compared to Objective CAML, we added two builtin types: cxxtype (C++ type seen as a value) and cxxprim (C++ piece of code seen as a value).

-- FrancisMaes? - 17 Nov 2003


MetaGeneZone


to top

You are here: Projects > TWikiWebsTable > Transformers > MetaGene > MetaGeneIntro

to top

Copyright © 1999-2010 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback