[Aldor-l] Generators (destructuring cross)
Martin Rubey
martin.rubey at univie.ac.at
Wed Nov 7 13:47:42 EST 2007
Dear Peter, Francois,
well, I had to try it, and I'm quite amazed. It's beautiful.
It does not work for dependent crosses, but that's probably too much too ask
for, as the compiler doesn't like them too much...
Many thanks,
Martin
aldor -fx -lalgebra -laldor ~/aldor-test/crossiterator.as
-------------------------------------------------------------------------------
#include "aldor"
#include "algebra"
#include "aldorio"
--#include "axiom"
Cr ==> Cross(n: MachineInteger, SmallPrimeField n);
D ==> Record(n: MachineInteger, s:SmallPrimeField n);
f(p: MachineInteger, m: MachineInteger): D == {
import from SmallPrimeField p;
e := [p, m::SmallPrimeField p]$D;
}
g: Generator Cr == generate {
import from MachineInteger, Cr;
yield explode(f(3, 5))$D;
yield explode(f(5, 5))$D;
}
h: Generator Cross(MachineInteger, String, SmallPrimeField 5) == generate {
import from MachineInteger, String, SmallPrimeField 5;
yield (3, "drei", 3::SmallPrimeField 5);
yield (8, "acht", 8::SmallPrimeField 5);
}
i: Generator Cross(MachineInteger, MachineInteger) == generate {
import from MachineInteger;
yield (2, 3);
yield (4, 5);
yield (6, 7);
}
bug(): () == {
import from MachineInteger, Cr, List Cr;
d1: D := f(3, 5);
c1: Cr := explode(d1)$D;
l: List Cr := [c1];
}
main(): () == {
import from MachineInteger, Cr, Generator Cr;
#if WantSegfault
bug();
#endif
stdout << "test dependent, old style:" << newline;
for i in g repeat {
(a: MachineInteger, b: SmallPrimeField(a)) == i;
stdout << a << newline;
#if WantRuntimeError
stdout << b << newline;
#endif
}
#if DoesntCompile
stdout << "test dependent:" << newline;
for (a, b) in g repeat {
stdout << a << newline;
}
#endif
stdout << "test Cartesian:" << newline;
import from SmallPrimeField 5;
for (a, b, c) in h repeat {
stdout << a << " " << b << " " << c << newline;
}
stdout << "test Cartesian List:" << newline;
import from List MachineInteger;
stdout << [a*b for (a, b) in i] << newline;
}
main();
More information about the Aldor-l
mailing list