axiom-developer
[Top][All Lists]

## [Axiom-developer] [MutualRecursion]

 From: billpage Subject: [Axiom-developer] [MutualRecursion] Date: Tue, 18 Jan 2005 14:35:41 -0600

??changed:
-Here is an example of defining functions by mutual recursion in Axiom
-
-We start with a "bootstrap" definition of 'parity(n)\$Even'. All that is -really needed here is a package that exports a function named 'parity' Here we give two examples of defining functions by mutual recursion in Axiom Recursion between Separate Domains First we show how to define two separate domains **EVEN** and **ODD** both of which have an attribute represented by the polymorthic function **parity**. We must start with a "bootstrap" definition of 'parity(n)\$even'. All that
is really needed here is a package that exports a function named 'parity'

??changed:
-)abbrev package EVEN Even
-Even(): E == I where
)abbrev domain EVEN even
even(): E == I where

??changed:
-Now we can define 'parity(n)\$Odd'. It depends on **EVEN**. Now we can define 'parity(n)\$odd'. It depends on **EVEN**.

??changed:
-)abbrev package ODD Odd
-Odd(): E == I where
)abbrev domain ODD odd
odd(): E == I where

??changed:
-      (n>0) => parity(n-1)$Even - (n<0) => parity(n+1)$Even
(n>0) => parity(n-1)$even (n<0) => parity(n+1)$even

??changed:
-depends (recusively) on **ODD**. So finally we need the full (re-)definition
-of 'parity(n)\$Even' depends (recusively) on **ODD**. So finally we need the full (re-)definition of 'parity(n)\$even'

??changed:
-)abbrev package EVEN Even
-Even(): E == I where
)abbrev domain EVEN even
even(): E == I where

??changed:
-      n>0 => parity(n-1)$Odd - n<0 => parity(n+1)$Odd
n>0 => parity(n-1)$odd n<0 => parity(n+1)$odd

??changed:
-Now we can test the new functions:
-
Now we can test the new function:

??changed:
-parity(10)$Even -parity(8)$Odd
-parity(-1111)$Odd parity(10)$even
parity(8)$odd parity(-1111)$odd

Recursion within a Single Domain

It is possible to write this same recursion as a domain that
exports two functions **Even** and **Odd**. In this case we do not
need to supply any initial *bootstrap* code because the compiler is
able to resolve both functions simultaneously.

\begin{axiom}
)abbrev domain PARITY Parity
Parity(): Exports == Implements where
Exports == with
Even: Integer -> Boolean
Odd: Integer -> Boolean
Odd(n: Integer) ==
n>0 => Even(n-1)
n<0 => Even(n+1)
false
Even(n: Integer) ==
n>0 => Odd(n-1)
n<0 => Odd(n+1)
true
\end{axiom}

Test
\begin{axiom}
Even(10)$Parity Odd(8)$Parity
Odd(-1111)\$Parity
\end{axiom}

--