[Aldor-l] Functions and Expression trees
Christian Aistleitner
tmgisi at gmx.at
Mon May 8 06:03:09 EDT 2006
Hello,
sadly enough, a suggestion from Stephen Watt did not make it to the
mailing list (although I could is a CC to aldor-l in the mail).
He suggested, to use a "partial solution that does not involve inspecting
the code":
> If the function is generic, say over a Ring, then you can build the
> expression tree by running the function instantiated over the expression
> type.
> E.g. If the function is exported by Package1(R), then
> Package1(SomeExpressionType) will have the + and * operations from
> SomeExpressionType, so f(symbol1, symbol2) will build the tree you want.
As I think, this approach is extremly nifty, I wanted to report it here.
However, as he already indicated, the solution has some drawbacks as well.
From my point of view, the essential drawbacks are:
1. The function has to be generic over some domain (As he had already
mentioned).
2. The result of the function has to the domain parameter.
3. A domain has to be created to reflect the required operations and
capture the expression trees.
4. Flow control statements ( if, while ) -- see below.
5. Tests might not be properly represented -- see below.
So using the technique described by Stephen Watt, a function func1 like
SomeDomain( R: Ring ): with {
func1: ( R, R ) -> R
} == add {
func1( a: R, b: R ): R == {
if zero? ( a + b ) then
{
a * b + a;
} else {
a + b;
}
}
}
would give either
( + ( * symbol1 symbol2 ) symbol1 )
or
( + symbol1 symbol2 )
. However, looking closer at the code, the best thing would probably be:
(if (zero? (+ symbol1 symbol2) ) ( + ( * symbol1 symbol2 ) symbol1 )
( + symbol1 symbol2 ) )
.
Although there are limitations to this approach, it shows _how_ elegant
generic programming can get. Great :)
--
Kind regards,
Christian
More information about the Aldor-l
mailing list