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

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


=== Metaprogramming in C++ ===
=== Metaprogramming in C++ ===
C++ supports a type of metaprogramming called template metaprogramming.Template Metaprogramming is a generic programming technique that uses extremely early binding. The compiler acts as an interpreter or a "virtual computer" that emits the instructions that make up the final program. It can be used for static configuration, adaptive programs, optimization and much more.[http://www.codeproject.com/KB/cpp/crc_meta.aspx]


C++ provides for a type of metaprogramming called as [http://en.wikipedia.org/wiki/Template_metaprogramming 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" [http://www.codeproject.com/KB/cpp/crc_meta.aspx] as it replaces parts of the program with instructions, which would otherwise have been done at run time.


In template metaprogramming  In this a piece of code called template is used. The template has to be defined before use. Later whenever a simialr piece of code is to be used the template is instasiatied. The idea is to group simialr lines of code as a template.  
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.


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

Revision as of 16:38, 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."


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.

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.

Illustrating the differences

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