CSC/ECE 517 Fall 2009/wiki2 1 ma: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 50: Line 50:


In TMP similar groups of lines of codes are replaced by a common template. Initially the template has to be defined with a base set of lines of code which are common in all. Then at the time of usage, templates have to be instantiated. Thus the templte definition specifies the general form of the code whereas the instantiation gives the specific lines of code formed from the template definition.
In TMP similar groups of lines of codes are replaced by a common template. Initially the template has to be defined with a base set of lines of code which are common in all. Then at the time of usage, templates have to be instantiated. Thus the templte definition specifies the general form of the code whereas the instantiation gives the specific lines of code formed from the template definition.
====Examples ====
The code to find summation of a number without using templates can be given as:
<source lang="cpp">
int summation(int n)
{
    if (n == 0 )
      return 0;
    return n + summation(n - 1);
}
void findsummation()
{
    int a = summation(5);  //a= (5+ 4 + 3 + 2 + 1 + 0) == 15
    int b = summation(0);  //b=0
}
</source>
Now using templates the same program can be written as:
<source lang="cpp">
template <int N>
//TEMPLATE DEFINITION
struct Summation
{
    enum { value = N + Summation<N - 1>::value }; 
};
template <>
struct Summation<0>
{
    enum { value = 0 };
};
Void findsummation()
{
    int a = Summation<5>::value; // == 15    // INSTANTIATION
    int b = Summation<0>::value; // == 1    // INSTANTIATION
}
</source>
Thus with templates, the values of a and b were calculated at compile time rather than at run time. This was possible because 5 and 0 were literals, if they had been variables then their values wouldn’t have been known at compile time and hence a and b could not have been calculated at compile time.


=== Illustrating the differences ===
=== Illustrating the differences ===

Revision as of 17:08, 8 October 2009

Metaprogramming is the writing of a program that writes other programs. Metaprogramming is not limited to o-o languages, but it seems that about half of the languages that support metaprogramming are object oriented. Consider how metaprogramming is done in the various languages, and give an example of writing a particular metaprogram in at least two of these languages to illustrate the differences. Since the Wikipedia article on metaprogramming is not very detailed, if you do a good job, you could submit this article to Wikipedia.

What is Metaprogramming?

Meta- (from Greek: μετά = "after", "beyond", "with", "adjacent", "self"), is a prefix used in English to indicate a concept which is an abstraction from another concept, used to complete or add to the latter. Source: Wikipedia

Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at runtime. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation."

Metaprogramming is divided into runtime metaprogramming, also known as reflection, and compile time metaprogramming. Reflection is more interesting since it allows interaction and modification of objects, components and structures that do not exist at compile time.

Examples of Metaprograms

  • the compiler or interpreter of your favourite language
  • Lex and Yacc
  • CORBA's IDL compiler
  • a Quine (a program that, when executed, prints a copy of its own source code - see QuineProgram for examples)
  • Programs to generate EJB code and XML from database metadata
  • Macros are nothing but special cases of metaprograms, as they are replaced by the actual code during macro expansion.


Metaprogramming style in various languages

Metaprogramming seems to have origniated in Lisp. John Foderaro mentions Lisp as a programmable programming language. Lisp facilitates reflection through the use of an interpreter implemented in the same language. Unlike Smalltalk and Lisp, Java and C++ do not feature extensive built-in reflection facilities.

Metaprogramming in Ruby

Ruby is a dynamically-typed, interpreted, object oriented language. One of the unique aspects of Ruby is its extensive reflexive metaprogramming facilities.

What makes Ruby good for metaprogramming

  • Ruby is Dynamic and reflective
  • Everything is open to change
  • Blocks allow writing new control structures
  • Most declarations are executable statements
  • Only slightly less malleable than Lisp (no macros)
  • A fantastic syntax

For instance attr_reader, attr_writer, attr_accessor are inbuilt keywords that declare the object properties. These are actually not just syntax, but methods defined in Module. These methods are written in C. In other words, the object language here is Ruby but the metalanguage is C - Refer Appendix.

Representing the functionality for the attr_reader method as a metaprogram in Ruby.

class Module
 def attr_reader (*syms)
   sys.each do |sym|
      class_eval %{def #{sym} @#{sym} end}
   end
 end
end

Metaprogramming in C++

C++ provides for a type of metaprogramming called as template metaprograming. Template Metaprogramming (TMP) is a programming technique that uses the priniciple of early binding . This means that pieces of codes called templates are replaced by actual code including compile-time constants, data structures, and complete functions. Then the compiler puts together all the code and compiles it. Hence the compiler in a sense acts as a "virtual computer" [1] as it replaces parts of the program with instructions, which would otherwise have been done at run time.

In TMP similar groups of lines of codes are replaced by a common template. Initially the template has to be defined with a base set of lines of code which are common in all. Then at the time of usage, templates have to be instantiated. Thus the templte definition specifies the general form of the code whereas the instantiation gives the specific lines of code formed from the template definition.

Examples

The code to find summation of a number without using templates can be given as:

int summation(int n) 
{
    if (n == 0 )
       return 0;
    return n + summation(n - 1);
}

void findsummation()
{
    int a = summation(5);   //a= (5+ 4 + 3 + 2 + 1 + 0) == 15
    int b = summation(0);  //b=0
}

Now using templates the same program can be written as:

template <int N>
//TEMPLATE DEFINITION
struct Summation
{
    enum { value = N + Summation<N - 1>::value };  
};
template <>
struct Summation<0> 
{
    enum { value = 0 };
};
Void findsummation()
{
    int a = Summation<5>::value; // == 15    // INSTANTIATION
    int b = Summation<0>::value; // == 1     // INSTANTIATION
}

Thus with templates, the values of a and b were calculated at compile time rather than at run time. This was possible because 5 and 0 were literals, if they had been variables then their values wouldn’t have been known at compile time and hence a and b could not have been calculated at compile time.

Illustrating the differences

Unlike Ruby, C++ does not support a built-in metaprogramming system aside from template metaprogramming. Template programming is also known as "programming at compile time". Templates allow the compiler to determine certain variable and object types at compile time. The disadvantages of templates are : i) it is not specialized enough (C++ templates are Turing-complete. Often they can be used for collection classes but they can also be employed in metaprogramming. This can make it hard to figure out how templates can be used to solve a specific problem), ii) poor error messages (when errors occur at compile time due to incorrectness of templates, the error messages can be cryptic)

Conclusion

Appendix

  • Metalanguage: The language in which the metaprogram is written
  • Object language: The language of the programs that are manipulated
  • Reflection or Reflexivity: The ability of a programming language to be its own metalanguage


References