Here we give two examples of defining functions by mutual recursion in FriCAS Recursion between Separate DomainsMutual recursion between domains is allowed. However, compiling such domains is a little tricky. Fist one need to compile files in "bootstrap mode". That is one needs to give: )boot $bootStrapMode := true Then compile domains first time using )boot $bootStrapMode := false and angain use Note, that goal of "bootstrap mode" compilation is to break cycles, so for example if there are two mutially dependent domains, then it is enough to compile in "bootstrap mode" just one. This process can not be used on the wiki, because wiki software automatically issues compilation commands. Instead one can use manual workaround which we describe below. 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 fricas (1) -> <spad> fricas )abbrev domain EVEN even
even(): E == I where
E == with
parity: Integer -> Boolean
I == add
parity(n) == true</spad>
fricas Compiling FriCAS source code from file
/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/3342984326569348053-25px001.spad
using old system compiler.
EVEN abbreviates domain even
------------------------------------------------------------------------
initializing NRLIB EVEN for even
compiling into NRLIB EVEN
compiling exported parity : Integer -> Boolean
EVEN;parity;IB;1 is replaced by QUOTET
Time: 0 SEC.Now we can define spad )abbrev domain ODD odd
odd(): E == I where
E == with
parity: Integer -> Boolean
I == add
parity(n:Integer) ==
-- output("ODD",
spad Compiling FriCAS source code from file
/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/7392352806104595030-25px002.spad
using old system compiler.
ODD abbreviates domain odd
------------------------------------------------------------------------
initializing NRLIB ODD for odd
compiling into NRLIB ODD
compiling exported parity : Integer -> Boolean
Time: 0 SEC.But the bootstrap definition of EVEN is incomplete. It really
depends (recusively) on ODD. So finally we need the full
(re-)definition of spad )abbrev domain EVEN even
even(): E == I where
E == with
parity: Integer -> Boolean
I == add
parity(n) ==
-- output("EVEN",
spad Compiling FriCAS source code from file
/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/8584362905457126194-25px003.spad
using old system compiler.
Illegal NRLIB
EVENNRLIB claims that its constructor name is the domain even but
even is already known to be the for package EVEN .
EVEN abbreviates domain even
------------------------------------------------------------------------
initializing NRLIB EVEN for even
compiling into NRLIB EVEN
compiling exported parity : Integer -> Boolean
Time: 0 SEC.Now we can test the new function: fricas parity(10)$even
Type: Boolean
fricas parity(8)$odd
Type: Boolean
fricas parity(-1111)$odd
Type: Boolean
Recursion within a Single DomainIt 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. spad )abbrev domain PARITY Parity
Parity(): Exports == Implements where
Exports == with
Even: Integer -> Boolean
Odd: Integer -> Boolean
Implements == add
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
spad Compiling FriCAS source code from file
/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/2188084076688500475-25px005.spad
using old system compiler.
PARITY abbreviates domain Parity
------------------------------------------------------------------------
initializing NRLIB PARITY for Parity
compiling into NRLIB PARITY
compiling exported Odd : Integer -> Boolean
Time: 0 SEC.Test fricas Even(10)$Parity
Type: Boolean
fricas Odd(8)$Parity
Type: Boolean
fricas Odd(-1111)$Parity
Type: Boolean
|