axiom-developer
[Top][All Lists]
Advanced

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

[Axiom-developer] [Fraction] A suggestion for MyFraction in Aldor (draft


From: Ralf Hemmecke
Subject: [Axiom-developer] [Fraction] A suggestion for MyFraction in Aldor (draft)
Date: Tue, 07 Feb 2006 06:27:45 -0600

Changes http://wiki.axiom-developer.org/Fraction/diff
--
Here is the code that demonstrates how to use conditional representations in 
Aldor for the domain constructor [Fraction]. I prepended the constructors by 
"My" in order to avoid naming conflicts with existing names in Axiom.

Doing it in that way, 'MyFraction MyFraction Integer' behaves as efficiently as 
just 'MyFraction Integer'.
\begin{aldor}
---- frac.as ----
-- Compile with 
--   aldor -mno-mactext -fx -laldor frac.as
-- results in

--: "frac.as", line 51: MyFraction(R: MyIntegralDomain): MyFractionField R == {
--:                       .........^
--: [L51 C10] #1 (Warning) Function returns a domain that might not be constant 
(which may cause problems if it is used in a dependent type).

-- Ignore this warning.

-- Then running the program gives

--: Integer:   2
--: Rational:  (2)/(1)
--: Rational1: (2)/(1)
--: Rational2: ((2)/(1))/((1)/(1))

-- which shows that the domain constructor "MyFraction" does not construct 
-- another representation in case its argument is already a field.

#if Axiom
#include "axiom"
#else
#include "aldor"
#endif

define MyIntegralDomain: Category == 
#if Axiom
CoercibleTo OutputForm
#else
OutputType
#endif
with {
        0: %;
        1: %;
        +: (%, %) -> %;
        -: % -> %;
        *: (%, %) -> %;
}

define MyField: Category == with {
        MyIntegralDomain;
        inv: % -> %;
}

define MyFractionField(R: MyIntegralDomain): Category == with {
        MyField;
        numerator: % -> R;
        denominator: % -> R;
}

MyFraction0(R: MyIntegralDomain): MyFractionField R == add {
        Rep == Record(num: R, den: R);
        import from Rep;
        0: % == per [0, 1];
        1: % == per [1, 1];
        numerator(x: %): R == rep(x).num;
        denominator(x: %): R == rep(x).den;
        -(x: %): % == per [- numerator x, denominator x];
        inv(x: %): % == per [denominator x, numerator x];
        (x: %) + (y: %): % == {
                n := numerator x * denominator y + denominator x * numerator y;
                d := denominator x * denominator y;
                per [n, d];
        }
        (x: %) * (y: %): % == {
                n := numerator x * numerator y;
                d := denominator x * denominator y;
                per [n, d];
        }
#if Axiom
        coerce(x: %): OutputForm == {
                (numerator(x)::OutputForm) / (denominator(x)::OutputForm)
        }
#else
        (tw: TextWriter) << (x: %): TextWriter == {
                import from String;
                tw << "(" << numerator x << ")/(" << denominator x << ")";
        }
#endif
}
MyFraction1(F: MyField): MyFractionField F == F add {
        numerator(x: %): F == x pretend F;
        denominator(x: %): F == 1$F;
}

MyFraction(R: MyIntegralDomain): MyFractionField R == {
        if R has MyField then MyFraction1(R pretend MyField) else MyFraction0 R;
}

MyInteger:   MyIntegralDomain == Integer add;
MyRational:  MyFractionField MyInteger  == MyFraction  MyInteger;
MyRational1: MyFractionField MyRational == MyFraction  MyRational;
MyRational2: MyFractionField MyRational == MyFraction0 MyRational;

#if Axiom
#else
main(): () == {
        import from TextWriter, String, Character;
        i: MyInteger    := 1;
        r0: MyRational  := 1;
        r1: MyRational1 := 1;
        r2: MyRational2 := 1;
        stdout << "Integer:   " << i  + i  << newline;
        stdout << "Rational:  " << r0 + r0 << newline;
        stdout << "Rational1: " << r1 + r1 << newline;
        stdout << "Rational2: " << r2 + r2 << newline;
}

main();
#endif
\end{aldor}

Does anybody know why the following doesn't work? 
As can be seen from above the code works with just using Aldor and the libaldor 
library.
\begin{axiom}
i : MyInteger   := 1
r0: MyRational  := 1
r1: MyRational1 := 1
r2: MyRational2 := 1
\end{axiom}
--
forwarded from http://wiki.axiom-developer.org/address@hidden




reply via email to

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