[Aldor-l] (to) add or not (to) add

Ralf Hemmecke ralf at hemmecke.de
Wed May 10 18:07:15 EDT 2006


I can understand (see section 7.3 "Type context" in the pdf version of 
the Aldor User Guide) why the following code works with the "add". But I 
somehow believe that it is a bug (in case there is no "add") when the 
compiler produces segfaulting code.

Does anyone know what the behaviour SHOULD be if I just define

Tree: Count == UnionCount(Atom, CrossCount(Tree, Tree));

? Should the compiler reject my program?

Ralf

---------- cnt.as ---------------------------
#include "aldor"
macro Z == Integer;
import from Z;
Count: Category == with {count: Z -> Z}
Atom: Count == add {count(i: Z): Z == if i=1 then 1 else 0}
UnionCount(S: Count, T: Count): Count == add {
	count(i: Z): Z == count(i)$S + count(i)$T;
}
CrossCount(S: Count, T: Count): Count == add {
	count(i: Z): Z == {
	        r := 0;
		for k in 1 .. i-1 repeat r := r + count(k)$S * count(i-k)$T;
	        r;
	}
}
#if WithAdd
Tree: Count == UnionCount(Atom, CrossCount(Tree, Tree)) add;
#else
Tree: Count == UnionCount(Atom, CrossCount(Tree, Tree));
#endif

count(args: Array String): () == {
         import from Tree;
         import from TextWriter, String, Character, Integer;
         stdout << "count 0 = " << count(0) << newline;
         stdout << "count 1 = " << count(1) << newline;
         stdout << "count 2 = " << count(2) << newline;
         stdout << "count 3 = " << count(3) << newline;
         stdout << "count 4 = " << count(4) << newline;
         stdout << "count 5 = " << count(5) << newline;
         stdout << "count 6 = " << count(6) << newline;
         stdout << "ENDE" << newline;
};


count(arguments$CommandLine);


#if COMMANDLINE
aldor -fx -laldor cnt.as
cnt
Segmentation fault
aldor -fx -DWithAdd -laldor cnt.as
cnt
count 0 = 0
count 1 = 1
count 2 = 1
count 3 = 2
count 4 = 5
count 5 = 14
count 6 = 42
ENDE
#endif




More information about the Aldor-l mailing list