axiom-developer
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Axiom-developer] A{ld,xi}o{r,m}-Combinat


From: Ralf Hemmecke
Subject: Re: [Axiom-developer] A{ld,xi}o{r,m}-Combinat
Date: Sun, 30 Apr 2006 01:31:18 +0200
User-agent: Thunderbird 1.5.0.2 (X11/20060420)

Oooop, no it was not intentional that I forgot the code in my last mail on this thread. Sorry.

Ralf


-------------------------------------------------------------------
----
---- CombClass
---- Copyright (C) Ralf Hemmecke (address@hidden)
---- http://www.hemmecke.de/aldor
----
-------------------------------------------------------------------

#include "aldor"
macro CCC == CombinatorialClassCategory;
import from Integer;

CombinatorialClassCategory: Category == with {
        count: Integer -> Integer;
}

Primitive(T: Type, n: Integer): CombinatorialClassCategory == add {
        Rep == T;
        count(i: Integer): Integer == if i=n then 1 else 0;
}
Epsilon(T: Type): CombinatorialClassCategory == Primitive(T, 0) add;
Atom(T: Type): CombinatorialClassCategory == Primitive(T, 1) add;

UnionClass(T: Type, S1: CCC, S2: CCC): CCC == add {
        Rep == Record(t1: T, t2: T);
        count(i: Integer): Integer == count(i)$S1 + count(i)$S2;
}

CrossClass(T: Type, S1: CCC, S2: CCC): with {
        CombinatorialClassCategory
} == add {
        Rep == Record(t1: T, t2: T);

        count(i: Integer): Integer == {
--              c := count(0)$S1;
--              result := if zero? c then 0 else c * count(i)$S2;
                result := 0;
                for k in 1 .. i-1 repeat {
                        result := result + count(k)$S1 * count(i-k)$S2;
                }
--              c := count(0)$S2;
--              result + (if zero? c then 0 else count(i)$S1) * c;
                result;
        }
}

TreeClass: CombinatorialClassCategory == UnionClass(Integer,
    Atom Integer,
    CrossClass(Integer, TreeClass, TreeClass)
) add;
--rhx: THIS "add" MUST BE HERE. Otherwise the code segfaults.
--rhx: The "reason": with "add" the compiler does a fix point
--rhx: computation.
--rhx: My guess: Without the "add" it is a simple constant
--rhx: assignment, and when the domain is the constant assignment
--rhx: takes place at runtime, the right hand side is evaluated
--rhx: completely before it gets assigned to the lefthand side.
--rhx: Well, but there then clearly is unevaluatable code on the
--rhx: righthand side.
--rhx: In my eyes, it's a but that the compiler does not complain
--rhx: if there is recursion and no "add".

combinat(args: Array String): () == {
        import from TreeClass;
        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;
};

combinat(arguments$CommandLine);


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






reply via email to

[Prev in Thread] Current Thread [Next in Thread]