[Aldor-l] How to emulate new infix and postfix operators in Aldor
Page, Bill
Bill.Page at drdc-rddc.gc.ca
Tue Aug 29 19:25:33 EDT 2006
Ralf,
On Friday, August 25, 2006 4:01 PM you wrote:
>
> I think some of you might find this interesting ...
>
> *Question:* Aldor has a number of infix operators (AUG Sect 4.4).
> Can one define other infix operators?
>
> *Answer:* No.
>
> *Question:* Aldor has a number of postfix operators (notably
> ".."). Can one define other postfix operators?
>
> *Answer:* No.
>
In a general purpose compiler these limitations seem very
desirable to me. But of course in Axiom or other computer
algebra system one might wish for more flexibility in order
to better model common mathematical usage.
> *Question:* Can one experiment with syntactic sugar in
> Aldor?
>
> *Answer:* *YES!!!*
>
> ---BEGIN aaaInfixPostfix.as
> #include "aldor"
> #include "aldorio"
>
> macro Z == Integer;
>
> extend Z: with {
> apply: (Z, Z -> Z) -> Z;
> } == add {
> apply(z: Z, f: Z -> Z): Z == f z;
> }
> main(): () == {
> _42 := "I am forty-two."; -- _42 is an identifier.
> stdout << _42 << newline;
>
> _!(z: Z): Z == {zero? z => 1; z * (z-1)_!;}
> z: Z := 4 _!;
> stdout << z << newline;
>
> x(a: Z)(b: Z): Z == a*b;
> z := 4 x 5 x 2;
> stdout << z << newline;
> }
> main();
> ---END aaapostfix.as
>
> >aldor -grun -laldor aaaInfixPostfix.as
> --rhx: ignore the compiler warnings...
> I am forty-two.
> 24
> 40
>
> Hope, you like that...
>
Nice.
But I don't see much "syntactic sugar" here. Using _42 as an
identifier just seems weird. Can you think of a reason to do
that?
The other two examples are not a matter of syntax but rather of
semantics. Defining the application of an integer to a function
as evaluation of the function on that integer is clever. Because
application is denoted by simple juxtaposition, this only looks
like "syntax" but really it just makes all unary functions commute
with their arguments. E.g.
import from Integer;
z abs = abs z
We can even extend this to binary functions:
extend Z: with {
apply: (Z, Z -> Z) -> Z;
apply: (Z, Z) -> Cross(Z,Z);
apply: (Z, (Z,Z) -> Z) -> (Z->Z);
} == add {
apply(z: Z, f: Z -> Z): Z == f z;
apply(z: Z, w: Z): Cross(Z,Z) == (z,w);
apply(z: Z, f: (Z,Z) -> Z): (Z->Z) == ((x:Z):Z +-> f(z,x));
}
Then we have
sum(z:Z,w:Z):Z == z+w;
1 2 sum = sum 1 2
Unfortunately although we can now write
+ 1 2
the over jealous compiler rejects
1 2 +
as a syntax error (although it would seem ok to me).
Regards,
Bill Page.
More information about the Aldor-l
mailing list