User Tools

Site Tools


at:tutorial:metaprogramming

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
at:tutorial:metaprogramming [2008/08/01 14:31]
tvcutsem *
at:tutorial:metaprogramming [2009/11/21 07:44] (current)
tvcutsem
Line 1: Line 1:
 ===== Metaprogramming ===== ===== Metaprogramming =====
  
-==== AT Zero - AmbientTalk without syntactic sugar ====+==== AmbientTalk without syntactic sugar ====
  
 In AmbientTalk, everything is an object, also native values such as numbers, booleans, strings and tables. Hence, the only way to interact with these values is by sending messages to them. However, there are some constructs in AmbientTalk that do not seem to send a message at all. Examples are table access/assignment, control structures and arithmic operations: In AmbientTalk, everything is an object, also native values such as numbers, booleans, strings and tables. Hence, the only way to interact with these values is by sending messages to them. However, there are some constructs in AmbientTalk that do not seem to send a message at all. Examples are table access/assignment, control structures and arithmic operations:
Line 209: Line 209:
 >>4 >>4
 </code> </code>
 +
 +==== Multi-stage (Generative) Programming ====
 +
 +Here's a small example of "compile-time" metaprogramming or "multi-stage programming" inspired by the same example from the E language on [[http://www.erights.org/elang/examples/multi-stage.html|Multi-stage programming in E]]:
 +
 +Below is a regular power function. Given two numbers 'x' and 'n', it returns 'x^n':
 +
 +<code javascript>
 +def pow(x, n) {
 +  if: (n == 0) then: {
 +    1
 +  } else: {
 +    x * pow(x,n-1);
 +  }
 +};
 +</code>
 +
 +Let's see whether it works:
 +<code javascript>
 +system.println(pow(2,5)); // prints 32
 +</code>
 +
 +Now, consider the following 'expandPow' function that is very similar to the above function, but which, instead of //calculating the value// of the power function will //build an expression// that, when evaluated, yields the value of the power function. Note that this function is parameterized with the name of the variable that is used to calculate the power value:
 +
 +<code javascript>
 +def expandPow(var, n) {
 +  if: (n == 0) then: {
 +    `1 // `exp returns an abstract syntax tree for exp
 +  } else: {
 +    // within a quoted expression, #(exp) evaluates exp,
 +    // expects it to return an AST, and embeds that AST
 +    // in the quoted expression
 +    `(#var * #(expandPow(var, n-1)));
 +  }
 +};
 +
 +// this prints the expression 'y * y * y * 1'
 +system.println("expandPow(`y,3) = " + expandPow(`y,3));
 +</code>
 +
 +To be able to use the expression generated by the above function, let's define a small helper function that will embed this expression in a first-class function: 
 +
 +<code javascript>
 +def powMaker(n) {
 +  def ast := expandPow(`x, n);
 +  // the built-in function 'eval:in:' takes an expression and an object and
 +  // evaluates the expression in the scope of the given object
 +  // return a function that has the expanded expression as its body:
 +  eval: `({|x| #ast}) in: self;
 +};
 +</code>
 +
 +Now we can generate power functions that are fixed in their second argument, but that are more efficient to execute:
 +
 +<code javascript>
 +// pow5 is now bound to a function { |x| x*x*x*x*x*1 }
 +def pow5 := powMaker(5);
 +
 +system.println(pow5(2)); // yields 32, as expected
 +</code>
 +
 +You can measure the performance difference by timing the evaluation of both functions:
 +
 +<code javascript>
 +import /.at.support.timer
 +system.println("pow(2,5) takes " + (time: { pow(2,5) }) +"ms");
 +system.println("pow5(2) takes " + (time: { pow5(2) }) +"ms");
 +</code>
 +
 +The quoting and unquoting mechanism of AmbientTalk makes it really easy to "partially evaluate" a function in one of its arguments.
 +
 +The above example is available in the AmbientTalk library, under '/at/demo/metaprogramming.at'.
at/tutorial/metaprogramming.1217593896.txt.gz ยท Last modified: 2009/11/21 07:42 (external edit)