login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

Edit detail for SandBox Monoid Extend revision 1 of 2

1 2
Editor:
Time: 2007/11/18 18:29:09 GMT-8
Note:

changed:
-
Martin's beautiful idea:

  We can use the Aldor 'extend' construct to add 'MyMonoid(...)' as
a category to a previously defined domain.

\begin{aldor}[mymonoid]
#include "axiom"

MyMonoid(T: Type, m: (T, T) -> T): Category == with {
   square:T -> T;
   coerce:T -> OutputForm;
   default {
     -- This hack for output only works for domains that
     -- have a representation known to Lisp.
     coerce(x:T):OutputForm == x pretend OutputForm;
     square(t: T): T == m(t,t)
   }
}
\end{aldor}

\begin{aldor}
#include "axiom"
#library MyMonoid "mymonoid.ao";
import from MyMonoid;
MyWord: with { 
   coerce: String -> %;
   c:(%, %) -> %;
   d:(%, %) -> %;
}
   == add {
   Rep == String;
   import from String;
   coerce(a: String): % == per(a);
   c(a: %, b: %):%  == per(concat(rep(a), rep(b)));
   d(a: %, b: %):%  == a;
}

import from MyWord;
extend MyWord: MyMonoid(MyWord, c) == add;
\end{aldor}

This is what Axiom sees:
\begin{axiom}
)sh MyMonoid
)sh MyWord
\end{axiom}

Try it:
\begin{axiom}
a := "Bingo"::MyWord
square a
MyWord has MyMonoid(MyWord, c)
\end{axiom}

That's pretty cool that Axiom knows 'MyWord' is a 'MyMonoid'!

But:
\begin{axiom}
MyWord has MyMonoid(MyWord, d)
\end{axiom}
Oops, that's **not** cool! :( Aldor and the Axiom interpreter
ought to be able to do better than this.

Here are some more examples:
\begin{aldor}
#include "axiom"
#library MyMonoid "mymonoid.ao";
import from MyMonoid;
MyInt: with { 
   coerce: Integer -> %;
   +:(%, %) -> %;
}
   == add {
   Rep == Integer;
   import from Integer;
   coerce(a: Integer): % == per(a);
   (a:%) + (b:%):%  == per(rep(a)+rep(b))
}
import from MyInt;
extend MyInt: MyMonoid(MyInt, +) == add;
\end{aldor}

This is what Axiom sees:
\begin{axiom}
)sh MyInt
\end{axiom}

Try it:
\begin{axiom}
b := 3::MyInt
b+1
square b
\end{axiom}

This is very general. Notice that we can rename the monoid
operation from * to +!
\begin{aldor}
#include "axiom"
#library MyMonoid "mymonoid.ao";
import from MyMonoid;
MyFloat: with { 
   coerce: DoubleFloat -> %;
   +:(%, %) -> %;
}
   == add {
   Rep == DoubleFloat;
   import from DoubleFloat;
   coerce(a: DoubleFloat): % == per(a);
   (a:%) + (b:%):%  == per(rep(a)*rep(b))
}
import from MyFloat;
extend MyFloat: MyMonoid(MyFloat, +) == add;
\end{aldor}

This is what Axiom sees:
\begin{axiom}
)sh MyFloat
\end{axiom}

Try it:
\begin{axiom}
f := 3.1::DoubleFloat::MyFloat
g := 2::DoubleFloat::MyFloat
-- The operator + in MyInt is * in Integer!
f+g
square f
\end{axiom}


Here's a variation on the example 'dirprod.as' posted by Ralf Hemmecke on 'Mon, 13 Mar 2006 14:33:34 +0100'
'[Axiom-developer] Re: BINGO,Curiosities with Axiom mathematical structures'
            
\begin{aldor}[mydirprod]
#include "axiom"
define DirProdCat(n: Integer, R: Type): Category == with {
    identity: % -> %;
}
DirProd(n: Integer, R: Type): DirProdCat(n, R) == add {
    Rep == List Integer; -- dummy implementation
    import from Rep;
    identity(x: %): % == x;
 }
\end{aldor}

Try it:

\begin{axiom}
x:= Integer;
y:= NonNegativeInteger;
DPx ==> DirProd(2, x);
DPx has DirProdCat(2, x)
DPx has DirProdCat(2, y)
DPx has DirProdCat(3, x)
DPx has DirProdCat(3, y)
\end{axiom}

The results show that the Aldor compiler treats a domain (R) and an element of a domain (n) differently in terms of information retained for the 'has' operation. Had the 'Type' in 'R: Type' been replaced by 'Symbol' and the lines defining x, y removed, all results would have been 'true'. Notice here, contrary to Ralf's example, both the 'DirProdCat' and 'DirProd' constructors totally ignored the parameters. The two constructions 'DirProd(2,x)' and 'DirProd(2,y)' are really identical in implemetation. So the only information to distinguish 'DirProdCat(2,x)' and 'DirProdCat(2,y)' must have come from the declaration of the parameters (on the left of ':').

\begin{axiom}
)show DirProd(2,x)
)show DirProd(2,y)
\end{axiom}

Martin's beautiful idea:

We can use the Aldor extend construct to add MyMonoid(...) as a category to a previously defined domain.

aldor
#include "axiom"
MyMonoid(T: Type, m: (T, T) -> T): Category == with { square:T -> T; coerce:T -> OutputForm; default { -- This hack for output only works for domains that -- have a representation known to Lisp. coerce(x:T):OutputForm == x pretend OutputForm; square(t: T): T == m(t,t) } }
aldor
   Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/mymonoid.as 
      using Aldor compiler and options 
-O -Fasy -Fao -Flsp -lfricas -Mno-ALDOR_W_WillObsolete -DFriCAS -Y $FRICAS/algebra -I $FRICAS/algebra
      Use the system command )set compiler args to change these 
      options.
   The )library system command was not called after compilation.

aldor
#include "axiom"
#library MyMonoid "mymonoid.ao";
import from MyMonoid;
MyWord: with { 
   coerce: String -> %;
   c:(%, %) -> %;
   d:(%, %) -> %;
}
   == add {
   Rep == String;
   import from String;
   coerce(a: String): % == per(a);
   c(a: %, b: %):%  == per(concat(rep(a), rep(b)));
   d(a: %, b: %):%  == a;
}
import from MyWord; extend MyWord: MyMonoid(MyWord, c) == add;
aldor
   Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/9179840256381573157-25px002.as
      using Aldor compiler and options 
-O -Fasy -Fao -Flsp -lfricas -Mno-ALDOR_W_WillObsolete -DFriCAS -Y $FRICAS/algebra -I $FRICAS/algebra
      Use the system command )set compiler args to change these 
      options.
   The )library system command was not called after compilation.

This is what Axiom sees:

fricas
)sh MyMonoid
The )show system command is used to display information about types or partial types. For example, )show Integer will show information about Integer .
MyMonoid is not the name of a known type constructor. If you want to see information about any operations named MyMonoid , issue )display operations MyMonoid
fricas
)sh MyWord
The )show system command is used to display information about types or partial types. For example, )show Integer will show information about Integer .
MyWord is not the name of a known type constructor. If you want to see information about any operations named MyWord , issue )display operations MyWord

Try it:

fricas
a := "Bingo"::MyWord
MyWord is not a valid type.

That's pretty cool that Axiom knows MyWord is a MyMonoid!

But:

fricas
MyWord has MyMonoid(MyWord, d)
MyWord is not a valid type.

Oops, that's not cool! :( Aldor and the Axiom interpreter ought to be able to do better than this.

Here are some more examples:

aldor
#include "axiom"
#library MyMonoid "mymonoid.ao";
import from MyMonoid;
MyInt: with { 
   coerce: Integer -> %;
   +:(%, %) -> %;
}
   == add {
   Rep == Integer;
   import from Integer;
   coerce(a: Integer): % == per(a);
   (a:%) + (b:%):%  == per(rep(a)+rep(b))
}
import from MyInt;
extend MyInt: MyMonoid(MyInt, +) == add;
aldor
   Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/797595480097738355-25px006.as
      using Aldor compiler and options 
-O -Fasy -Fao -Flsp -lfricas -Mno-ALDOR_W_WillObsolete -DFriCAS -Y $FRICAS/algebra -I $FRICAS/algebra
      Use the system command )set compiler args to change these 
      options.
   The )library system command was not called after compilation.

This is what Axiom sees:

fricas
)sh MyInt
The )show system command is used to display information about types or partial types. For example, )show Integer will show information about Integer .
MyInt is not the name of a known type constructor. If you want to see information about any operations named MyInt , issue )display operations MyInt

Try it:

fricas
b := 3::MyInt
MyInt is not a valid type.

This is very general. Notice that we can rename the monoid operation from * to +!

aldor
#include "axiom"
#library MyMonoid "mymonoid.ao";
import from MyMonoid;
MyFloat: with { 
   coerce: DoubleFloat -> %;
   +:(%, %) -> %;
}
   == add {
   Rep == DoubleFloat;
   import from DoubleFloat;
   coerce(a: DoubleFloat): % == per(a);
   (a:%) + (b:%):%  == per(rep(a)*rep(b))
}
import from MyFloat;
extend MyFloat: MyMonoid(MyFloat, +) == add;
aldor
   Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/4949806727147037492-25px009.as
      using Aldor compiler and options 
-O -Fasy -Fao -Flsp -lfricas -Mno-ALDOR_W_WillObsolete -DFriCAS -Y $FRICAS/algebra -I $FRICAS/algebra
      Use the system command )set compiler args to change these 
      options.
   The )library system command was not called after compilation.

This is what Axiom sees:

fricas
)sh MyFloat
The )show system command is used to display information about types or partial types. For example, )show Integer will show information about Integer .
MyFloat is not the name of a known type constructor. If you want to see information about any operations named MyFloat , issue )display operations MyFloat

Try it:

fricas
f := 3.1::DoubleFloat::MyFloat
MyFloat is not a valid type.

Here's a variation on the example dirprod.as posted by Ralf Hemmecke on Mon, 13 Mar 2006 14:33:34 +0100 '[Axiom-developer]? Re: BINGO,Curiosities with Axiom mathematical structures'

aldor
#include "axiom"
define DirProdCat(n: Integer, R: Type): Category == with {
    identity: % -> %;
}
DirProd(n: Integer, R: Type): DirProdCat(n, R) == add {
    Rep == List Integer; -- dummy implementation
    import from Rep;
    identity(x: %): % == x;
 }
aldor
   Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/mydirprod.as 
      using Aldor compiler and options 
-O -Fasy -Fao -Flsp -lfricas -Mno-ALDOR_W_WillObsolete -DFriCAS -Y $FRICAS/algebra -I $FRICAS/algebra
      Use the system command )set compiler args to change these 
      options.
   The )library system command was not called after compilation.

Try it:

fricas
x:= Integer;
Type: Type
fricas
y:= NonNegativeInteger;
Type: Type
fricas
DPx ==> DirProd(2, x);
Type: Void
fricas
DPx has DirProdCat(2, x)
There are no library operations named DirProd Use HyperDoc Browse or issue )what op DirProd to learn if there is any operation containing " DirProd " in its name.
Cannot find a definition or applicable library operation named DirProd with argument type(s) PositiveInteger Type
Perhaps you should use "@" to indicate the required return type, or "$" to specify which version of the function you need.

The results show that the Aldor compiler treats a domain (R) and an element of a domain (n) differently in terms of information retained for the has operation. Had the Type in R: Type been replaced by Symbol and the lines defining x, y removed, all results would have been true. Notice here, contrary to Ralf's example, both the DirProdCat and DirProd constructors totally ignored the parameters. The two constructions DirProd(2,x) and DirProd(2,y) are really identical in implemetation. So the only information to distinguish DirProdCat(2,x) and DirProdCat(2,y) must have come from the declaration of the parameters (on the left of :).

fricas
)show DirProd(2,x)
The )show system command is used to display information about types or partial types. For example, )show Integer will show information about Integer .
DirProd is not the name of a known type constructor. If you want to see information about any operations named DirProd , issue )display operations DirProd
2 is not the name of a known type constructor. If you want to see information about any operations named 2 , issue )display operations 2
x is not the name of a known type constructor. If you want to see information about any operations named x , issue )display operations x
fricas
)show DirProd(2,y)
The )show system command is used to display information about types or partial types. For example, )show Integer will show information about Integer .
DirProd is not the name of a known type constructor. If you want to see information about any operations named DirProd , issue )display operations DirProd
2 is not the name of a known type constructor. If you want to see information about any operations named 2 , issue )display operations 2
y is not the name of a known type constructor. If you want to see information about any operations named y , issue )display operations y