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

Edit detail for SandBoxLexicalScope revision 10 of 12

1 2 3 4 5 6 7 8 9 10 11 12
Editor: Bill Page
Time: 2008/05/15 20:14:16 GMT-7
Note: Gaby

changed:
-    foo: Integer -> Integer
    foo: Integer -> Union(Integer,Float)

changed:
-    foo(a: Integer): Integer ==
    foo(a: Integer): Union(Integer,Float) ==

changed:
-     x case Integer => (x*x)::Integer
-     wholePart(x*x)

     if x case Integer then x:=x*x
     else x:=x*x
     x

added:

**Gaby* wrote:

Try:
\begin{spad}
)abbrev package BILLPAGE BillPageUnion
BillPageUnion(): Public == Private where
 Public ==> with
   foo: (Integer,Integer) -> Union(Integer,Float)
 Private ==> add
   makeU(i:Integer): Union(Integer,Float) ==
    even? i => 1
    3.14

   foo(i:Integer,j:Integer):Union(Integer,Float) ==
     x: Union(Integer,Float) := makeU(i)
     y: Union(Integer,Float) := makeU(i)
     x case Integer =>
       xf := x::Integer::Float
       y case Integer => xf**y
       xf**y
     x case Float =>
       y case Integer => x ** y
       x ** y
     0
\end{spad}

\begin{axiom}
foo(1,1)$BillPageUnion
foo(2,1)$BillPageUnion
foo(1,2)$BillPageUnion
foo(2,2)$BillPageUnion
\end{axiom}

j is referenced out of scope

axiom
for i in 1..5 repeat j:Integer := i^2 output(i::OutputForm)$OutputPackage 1 2 3 4 5
Type: Void
axiom
output(j::OutputForm)$OutputPackage 25
Type: Void

The code above returns an error in OpenAxiom? 1.2.0 (Revision >= 563).

It sould be written like this:

axiom
j: Integer
Type: Void
axiom
for i in 1..5 repeat j := i^2 output(i::OutputForm)$OutputPackage 1 2 3 4 5
Type: Void
axiom
output(j::OutputForm)$OutputPackage 25
Type: Void

Oddly, using j out of scope is also allowed in Aldor:

aldor
#pile #include "axiom" import from PositiveInteger CheckScope1(): with checkscope1: () -> PositiveInteger == add checkscope1():PositiveInteger == for i in 1..5 repeat j:PositiveInteger := i^2 --output(i::OutputForm)$OutputPackage --output(j::OutputForm)$OutputPackage j
aldor
   Compiling FriCAS source code from file 
      /var/zope2/var/LatexWiki/8452782754204589047-25px003.as using 
      AXIOM-XL compiler and options 
-O -Fasy -Fao -Flsp -laxiom -Mno-AXL_W_WillObsolete -DAxiom -Y $AXIOM/algebra
      Use the system command )set compiler args to change these 
      options.
#1 (Warning) Deprecated message prefix: use `ALDOR_' instead of `_AXL'
   Compiling Lisp source code from file 
      ./8452782754204589047-25px003.lsp
   Issuing )library command for 8452782754204589047-25px003
   Reading /var/zope2/var/LatexWiki/8452782754204589047-25px003.asy
   CheckScope1 is now explicitly exposed in frame initial 
   CheckScope1 will be automatically loaded when needed from 
      /var/zope2/var/LatexWiki/8452782754204589047-25px003

axiom
checkscope1()
LatexWiki Image(1)
Type: PositiveInteger?

aldor
#pile #include "axiom" import from PositiveInteger CheckScope2(): with checkscope2: () -> PositiveInteger == add checkscope2():PositiveInteger == local j:PositiveInteger for i in 1..5 repeat j := i^2 --output(i::OutputForm) --output(j::OutputForm)$OutputPackage j
aldor
   Compiling FriCAS source code from file 
      /var/zope2/var/LatexWiki/8264748852590484820-25px005.as using 
      AXIOM-XL compiler and options 
-O -Fasy -Fao -Flsp -laxiom -Mno-AXL_W_WillObsolete -DAxiom -Y $AXIOM/algebra
      Use the system command )set compiler args to change these 
      options.
#1 (Warning) Deprecated message prefix: use `ALDOR_' instead of `_AXL'
   Compiling Lisp source code from file 
      ./8264748852590484820-25px005.lsp
   Issuing )library command for 8264748852590484820-25px005
   Reading /var/zope2/var/LatexWiki/8264748852590484820-25px005.asy
   CheckScope2 is now explicitly exposed in frame initial 
   CheckScope2 will be automatically loaded when needed from 
      /var/zope2/var/LatexWiki/8264748852590484820-25px005

axiom
checkscope2()
LatexWiki Image(2)
Type: PositiveInteger?

Ralf Hemmecke wrote:

Ooops, what in would be illegal? I just had to replace (as I said) the braces by parentheses, since parens are used to group sequences of commands. (Same as braces in Aldor.)

spad
)abbrev domain AAA Aaa Aaa: with bar: Integer -> Integer == add (x: Integer := 4; y: Integer := 3) bar(a: Integer): Integer == x + a + z z: Integer := 3 + y
spad
   Compiling FriCAS source code from file 
      /var/zope2/var/LatexWiki/2292316700013189339-25px007.spad using 
      old system compiler.
   AAA abbreviates domain Aaa 
------------------------------------------------------------------------
   initializing NRLIB AAA for Aaa 
   compiling into NRLIB AAA 
   compiling exported bar : Integer -> Integer
****** comp fails at level 2 with expression: ******
error in function bar 
(+ (+ |x| |a|) | << z >> |)
****** level 2  ******
$x:= z
$m:= $EmptyMode
$f:=
((((|a| # #) (|y| # #) (|x| # #) (* # # #) ...)))
   >> Apparent user error:
   cannot compile (+ (+ x a) z)

Bill Page wrote:

But this works since z is now in "scope" of the function body of bar.

spad
)abbrev domain AAA Aaa Aaa: with bar: Integer -> Integer == add (x: Integer := 4; y: Integer := 3) z: Integer bar(a: Integer): Integer == x + a + z z: Integer := 3 + y
spad
   Compiling FriCAS source code from file 
      /var/zope2/var/LatexWiki/6187271087318241578-25px008.spad using 
      old system compiler.
   AAA abbreviates domain Aaa 
------------------------------------------------------------------------
   initializing NRLIB AAA for Aaa 
   compiling into NRLIB AAA 
   compiling exported bar : Integer -> Integer
Time: 0 SEC.
(time taken in buildFunctor:  0)
;;;     ***       |Aaa| REDEFINED
;;;     ***       |Aaa| REDEFINED
Time: 0 SEC.
   Warnings: 
      [1] bar:  z has no value
   Cumulative Statistics for Constructor Aaa
      Time: 0 seconds
   finalizing NRLIB AAA 
   Processing Aaa for Browser database:
--->-->Aaa((bar ((Integer) (Integer)))): Not documented!!!!
--->-->Aaa(constructor): Not documented!!!!
--->-->Aaa(): Missing Description
; (DEFUN |AAA;bar;2I;1| ...) is being compiled.
;; The variable |z| is undefined.
;; The compiler will assume this variable is a global.
------------------------------------------------------------------------
   Aaa is now explicitly exposed in frame initial 
   Aaa will be automatically loaded when needed from 
      /var/zope2/var/LatexWiki/AAA.NRLIB/code

An example of Union for branches of if that yield different types:

spad
)abbrev domain BBB Bbb Bbb: with foo: Integer -> Union(Integer,Float) == add foo(a: Integer): Union(Integer,Float) == x:Union(Integer,Float) if a>0 then x := 1 else x := 2.3 if x case Integer then x:=x*x else x:=x*x x
spad
   Compiling FriCAS source code from file 
      /var/zope2/var/LatexWiki/1133161573775318814-25px009.spad using 
      old system compiler.
   BBB abbreviates domain Bbb 
------------------------------------------------------------------------
   initializing NRLIB BBB for Bbb 
   compiling into NRLIB BBB 
   compiling exported foo : Integer -> Union(Integer,Float)
Time: 0.02 SEC.
(time taken in buildFunctor:  0)
;;;     ***       |Bbb| REDEFINED
;;;     ***       |Bbb| REDEFINED
Time: 0 SEC.
   Cumulative Statistics for Constructor Bbb
      Time: 0.02 seconds
   finalizing NRLIB BBB 
   Processing Bbb for Browser database:
--->-->Bbb((foo ((Union (Integer) (Float)) (Integer)))): Not documented!!!!
--->-->Bbb(constructor): Not documented!!!!
--->-->Bbb(): Missing Description
------------------------------------------------------------------------
   Bbb is now explicitly exposed in frame initial 
   Bbb will be automatically loaded when needed from 
      /var/zope2/var/LatexWiki/BBB.NRLIB/code

axiom
foo(0)
LatexWiki Image(3)
Type: Union(Float,...)
axiom
foo(1)
LatexWiki Image(4)
Type: Union(Integer,...)

*Gaby wrote:

Try:

spad
)abbrev package BILLPAGE BillPageUnion BillPageUnion(): Public == Private where Public ==> with foo: (Integer,Integer) -> Union(Integer,Float) Private ==> add makeU(i:Integer): Union(Integer,Float) == even? i => 1 3.14 foo(i:Integer,j:Integer):Union(Integer,Float) == x: Union(Integer,Float) := makeU(i) y: Union(Integer,Float) := makeU(i) x case Integer => xf := x::Integer::Float y case Integer => xf**y xf**y x case Float => y case Integer => x ** y x ** y 0
spad
   Compiling FriCAS source code from file 
      /var/zope2/var/LatexWiki/7194597887863935660-25px011.spad using 
      old system compiler.
   BILLPAGE abbreviates package BillPageUnion 
   processing macro definition Public ==> -- the constructor category 
   processing macro definition Private ==> -- the constructor capsule 
------------------------------------------------------------------------
   initializing NRLIB BILLPAGE for BillPageUnion 
   compiling into NRLIB BILLPAGE 
   compiling local makeU : Integer -> Union(Integer,Float)
Time: 0.01 SEC.
   compiling exported foo : (Integer,Integer) -> Union(Integer,Float)
Time: 0.01 SEC.
(time taken in buildFunctor:  0)
;;;     ***       |BillPageUnion| REDEFINED
;;;     ***       |BillPageUnion| REDEFINED
Time: 0 SEC.
   Cumulative Statistics for Constructor BillPageUnion
      Time: 0.02 seconds
   finalizing NRLIB BILLPAGE 
   Processing BillPageUnion for Browser database:
--->-->BillPageUnion((foo ((Union (Integer) (Float)) (Integer) (Integer)))): Not documented!!!!
--->-->BillPageUnion(constructor): Not documented!!!!
--->-->BillPageUnion(): Missing Description
------------------------------------------------------------------------
   BillPageUnion is now explicitly exposed in frame initial 
   BillPageUnion will be automatically loaded when needed from 
      /var/zope2/var/LatexWiki/BILLPAGE.NRLIB/code

axiom
foo(1,1)$BillPageUnion
LatexWiki Image(5)
Type: Union(Float,...)
axiom
foo(2,1)$BillPageUnion
LatexWiki Image(6)
Type: Union(Float,...)
axiom
foo(1,2)$BillPageUnion
LatexWiki Image(7)
Type: Union(Float,...)
axiom
foo(2,2)$BillPageUnion
LatexWiki Image(8)
Type: Union(Float,...)

Aldor (at least, the reference manual says) so makes variable declarations `local', unless modified by `free' or `fluid' modifiers.

Specification? --hemmecke, Tue, 13 May 2008 08:23:34 -0700 reply
Gaby, on which page of the AUG is this written?