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

Edit detail for SandBoxMyReduceInOpenAxiom revision 3 of 4

1 2 3 4
Editor: Bill Page
Time: 2008/04/08 17:28:10 GMT-7
Note: using = from Mapping

added:

Here is another version of the same thing using the '=' from the 'Mapping' domain.

\begin{spad}
)abbrev package MYRED2 MyReduce2
MyReduce(S:Type): with
    myred2: ((S,S)->S,List S) -> S
 == add
   import NonNegativeInteger
   myred2(f:(S,S)->S, x:List S):S ==
     if #x>1 then
       f(first x, myred2(f,rest x))
     else if #x=1 then
       first x
     else
       if S has AbelianMonoid then
         -- Must force "newGoGet" by applying operations before comparison!
         -- f(0,0)=(0+0)$S and (f = _+$S) => return 0$S
         (f = _+$S)$Mapping(S,S,S) => return 0$S
       if S has Monoid then
         -- f(1,1)=(1*1)$S and (f = _*$S) => return 1$S
         (f = _*$S)$Mapping(S,S,S) => return 1$S
       error "reducing over an empty list needs the 3 argument form"
\end{spad}

Notice that it fails in the same way as above, but it would work if we used the original trick of applying the function before comparing it.
\begin{axiom}
myred2(+,[1,2,3,4])
myred2(+,[]$List Integer)
myred2(*,[1,2,3,4])
myred2(*,[]$List Integer)
\end{axiom}

axiom
)version Value = "Tuesday January 29, 2008 at 18:03:22 "

spad
)abbrev package MYRED MyReduce MyReduce(S:Type): with myred: ((S,S)->S,List S) -> S == add -- Waldek's local helper function ((f:(S,S)->S) = (g:(S,S)->S)):Boolean == print(coerceMap2E(f)$Lisp::OutputForm) print(coerceMap2E(g)$Lisp::OutputForm) EQ(f,g)$Lisp -- import NonNegativeInteger myred(f:(S,S)->S, x:List S):S == if #x>1 then f(first x, myred(f,rest x)) else if #x=1 then first x else if S has AbelianMonoid then -- Must force "newGoGet" by applying operations before comparison! -- f(0,0)=(0+0)$S and (f = _+$S) => return 0$S (f = _+$S) => return 0$S if S has Monoid then -- f(1,1)=(1*1)$S and (f = _*$S) => return 1$S (f = _*$S) => return 1$S error "reducing over an empty list needs the 3 argument form"
spad
   Compiling OpenAxiom source code from file 
      /var/zope2/var/LatexWiki/6114676696124184375-25px002.spad using 
      old system compiler.
   MYRED abbreviates package MyReduce 
------------------------------------------------------------------------
   initializing NRLIB MYRED for MyReduce 
   compiling into NRLIB MYRED 
   compiling local = : ((S,S) -> S,(S,S) -> S) -> Boolean
Time: 0 SEC.
   importing NonNegativeInteger
   compiling exported myred : ((S,S) -> S,List S) -> S
****** Domain: S already in scope
augmenting S: (AbelianMonoid)
****** Domain: S already in scope
augmenting S: (Monoid)
Time: 0.10 SEC.
(time taken in buildFunctor:  0)
;;;     ***       |MyReduce| REDEFINED
;;;     ***       |MyReduce| REDEFINED
Time: 0.05 SEC.
   Cumulative Statistics for Constructor MyReduce
      Time: 0.15 seconds
   finalizing NRLIB MYRED 
   Processing MyReduce for Browser database:
--->-->MyReduce((myred (S (Mapping S S S) (List S)))): Not documented!!!!
--->-->MyReduce(constructor): Not documented!!!!
--->-->MyReduce(): Missing Description
------------------------------------------------------------------------
   MyReduce is now explicitly exposed in frame initial 
   MyReduce will be automatically loaded when needed from 
      /var/zope2/var/LatexWiki/MYRED.NRLIB/code

axiom
myred(+,[1,2,3,4])
LatexWiki Image(1)
Type: PositiveInteger?
axiom
myred(+,[]$List Integer) theMap(INT;+;3$;37,536) theMap(newGoGet) theMap(INT;+;3$;37,536) theMap(newGoGet) >> Error detected within library code: reducing over an empty list needs the 3 argument form

Here is another version of the same thing using the = from the Mapping domain.

spad
)abbrev package MYRED2 MyReduce2 MyReduce(S:Type): with myred2: ((S,S)->S,List S) -> S == add import NonNegativeInteger myred2(f:(S,S)->S, x:List S):S == if #x>1 then f(first x, myred2(f,rest x)) else if #x=1 then first x else if S has AbelianMonoid then -- Must force "newGoGet" by applying operations before comparison! -- f(0,0)=(0+0)$S and (f = _+$S) => return 0$S (f = _+$S)$Mapping(S,S,S) => return 0$S if S has Monoid then -- f(1,1)=(1*1)$S and (f = _*$S) => return 1$S (f = _*$S)$Mapping(S,S,S) => return 1$S error "reducing over an empty list needs the 3 argument form"
spad
   Compiling OpenAxiom source code from file 
      /var/zope2/var/LatexWiki/8074394904773086877-25px004.spad using 
      old system compiler.
   MYRED2 abbreviates package MyReduce2 
------------------------------------------------------------------------
   initializing NRLIB MYRED for MyReduce 
   compiling into NRLIB MYRED 
   importing NonNegativeInteger
   compiling exported myred2 : ((S,S) -> S,List S) -> S
****** Domain: S already in scope
augmenting S: (AbelianMonoid)
****** Domain: S already in scope
augmenting S: (Monoid)
****** Domain: (Mapping S S S) already in scope
Time: 0.01 SEC.
(time taken in buildFunctor:  0)
;;;     ***       |MyReduce| REDEFINED
;;;     ***       |MyReduce| REDEFINED
Time: 0 SEC.
   Cumulative Statistics for Constructor MyReduce
      Time: 0.01 seconds
   finalizing NRLIB MYRED 
   Processing MyReduce for Browser database:
--->/var/zope2/var/LatexWiki/6114676696124184375-25px002.spad-->MyReduce((myred2 (S (Mapping S S S) (List S)))): Not documented!!!!
--->/var/zope2/var/LatexWiki/6114676696124184375-25px002.spad-->MyReduce(constructor): Not documented!!!!
--->/var/zope2/var/LatexWiki/6114676696124184375-25px002.spad-->MyReduce(): Missing Description
------------------------------------------------------------------------
   MyReduce is already explicitly exposed in frame initial 
   MyReduce will be automatically loaded when needed from 
      /var/zope2/var/LatexWiki/MYRED.NRLIB/code

Notice that it fails in the same way as above, but it would work if we used the original trick of applying the function before comparing it.

axiom
myred2(+,[1,2,3,4])
LatexWiki Image(2)
Type: PositiveInteger?
axiom
myred2(+,[]$List Integer) >> Error detected within library code: reducing over an empty list needs the 3 argument form