CSC/ECE 517 Spring 2013/ch1b 1i lh

From Expertiza_Wiki
Jump to navigation Jump to search


"I rather write a x86 program in binary using a butterfly than writing this article" - Hao Liu

This page is a discuss about meta-programming and its implementation and uses in dynamically typed languages.

Meta-programming in Dynamically Typed Languages

Introduction to Meta-Programming

In essence meta-programs are computer programs that can write or manipulate other programs or themselves as their data. Meta-programming is the general term for techniques use to write such program. The language in which the meta-program is written is called the metalanguage. The language of the programs that are manipulated is called the object language.[1]


This simplistic definition of meta-program and meta-programming tend to give reader a very intimidating first impression and render a image of a sentient self modifying program that ultimately take over the world. In reality meta-program and meta-programming techniques can very in complexity from the extreme of sentient self modifying program to simple and everyday thing such as a pre-processor macro. In most cases meta-programming are use to improve the quality of life of the programmer by improving the readability and re-usability of the code and by providing a higher level of abstraction.

Any programming language, even the simple shell script, can be use for meta-programming, so long as they have the ability to reading and modifying the source code of another program or it's own source code as a text file.

For example:

#!/usr/bin/env bash
echo -e "#include <stdio.h>" >> program.c
echo -e "int main() {"       >> program.c
for ((I=1; I<=5; I++)) do
    echo -e "  printf(\042 meta \042);" >> program.c
done
echo -e "return 0}" >> program.c
g++ program.c
a.out

This simple bash script generates a program that prints out

meta meta meta meta meta

by generating a c source code as a text file, than compile to source code into a executable binary file using a compiler.


In this article we will be focusing more on the implementation and use of reflection to facilitate meta-programming in dynamically typed languages.

Reflection is the ability of a computer program to examine and modify the structure and behavior of an object at runtime.[2]

Introduction to Dynamically Typed Programming Languages

In general programming languages can be split into two categories base on their type system, statically typed and dynamically typed. A programming language is said to be statically typed if type checking is performed during compile-time. In comparison, a programming language is said to be dynamically typed deferring type checking until run-time as opposes to compile-time. [3]


In dynamically typed languages, the variables and parameters do not have a designated type and may take different type of values at different times. In all the operations, the operands must be type checked at run-time just before performing the operation. Dynamically typed languages don’t need to make a distinction between classes created at compile time and classes provided. It is possible to define classes at run time and in fact, classes are always defined at run time. These eliminate many developer constraints by avoiding the need of book keeping, declarations etc. Due to this flexibility these languages make an ideal candidate for prototyping and are widely used in agile development environments. However, dynamic languages are known to have performance issues. Static languages have code optimization features at compile time, but dynamic languages allow run-time code optimizations only. [4] In dynamically typed languages, the interpreter deduces type and type conversions, this makes development time faster, but it also can provoke run-time failures. These run-time failures are caught early on during compile time for statically typed languages.


Examples of dynamically typed languages

LISP

Perl

Perl is a high-level, general-purpose, interpreted, dynamic programming language originally developed as a general-purpose Unix scripting language to make report processing easier. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities without the arbitrary data-length limits of many contemporary Unix tools, facilitating easy manipulation of text files. Perl is used for CGI scripting, graphics programming, system administration, network programming, finance, bioinformatics, and other applications.

Python

Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. It supports multiple programming paradigms, including object-oriented, imperative and functional programming styles. It features a fully dynamic type system and automatic memory management, similar to that of Scheme, Ruby, Perl and Tcl. Like other dynamic languages, Python is often used as a scripting language, but is also used in a wide range of non-scripting contexts. Using third-party tools, Python code can be packaged into standalone executable programs.

JavaScript

PHP

Ruby

Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. It was also influenced by Eiffel and Lisp[8]. Ruby was first designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. Ruby supports multiple programming paradigms, including functional, object oriented, imperative and reflective. It also has a dynamic type system and automatic memory management; it is therefore similar in varying respects to Smalltalk, Python, Perl, Lisp, Dylan, Pike, and CLU. The standard 1.8.7 implementation is written in C, as a single-pass interpreted language. Starting with the 1.9 branch, and continuing with the current 2.0 branch, YARV has been used, and will eventually supersede the slower Ruby MRI. The language specifications for Ruby were developed by the Open Standards Promotion Center of the Information-Technology Promotion Agency (a Japanese government agency) for submission to the Japanese Industrial Standards Committee and then to the International Organization for Standardization. It was accepted as a Japanese Industrial Standard (JIS X 3017) in 2011[9] and an international standard (ISO/IEC 30170) in 2012.[10] As of 2010, there are a number of complete or upcoming alternative implementations of Ruby, including YARV, JRuby, Rubinius, IronRuby, MacRuby (and its iOS counterpart, RubyMotion), mruby, and HotRuby. Each takes a different approach, with IronRuby, JRuby, MacRuby and Rubinius providing just-in-time compilation and MacRuby and mruby also providing ahead-of-time compilation.

SmallTalk

Example of Meta-Programming in Dynamically Typed Languages

Ruby

In ruby you can write methods using metaprogramming to insert code into the program. A widely use example of such metaprogramming method is the attr_accessor method in the class named Module.

In this example attr_accessor method is use to insert the accessor codes to the class

class MyClass   
  attr_accessor :info
end

Is the same as

class MyClass
 def variable=(value)
   @variable = value
 end
 def variable
   @variable
 end
end

The method method_missing() is another example of metaprograming in Ruby. In ruby if the interpreter can’t find the method anywhere up the object’s chain of inheritance, it will go back to the object and call the method named method_missing(). The interpreter looks for method_missing() in the object’s methods and up the object’s chain of inheritance until it reaches the Object class where method_missing() is defined.

class MyClass
  def method_missing(name, *args)
    puts "#{name} was called with arguments: #{args.join(', ')}"
  end
end
m = MyClass.new
m.undefined_method("one", "two") # => undefined_method was called with arguments: one, two
m.another_undefined_method("three", "four") # => another_undefined_method was called with arguments: three, four

The class MyClass does not have a method named undefined_method() or another_underfined_method() but the method_missing() method created responds to these methods.

Implementation of Meta-Programming in Dynamically Typed Languages

-macro

-self

-metaclass A metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances.

In ruby each object metaclass, a class that can have methods, but is only attached to the object itself.

-eval

References