$SPAD/src/input ifthenelse.input

Fabio Stumbo December 3, 2016

Abstract

if-then-else: what is wrong?

Contents

— * —

)set break resume
)spool ifthenelse.output
)set message test on
)set message auto off
)clear all

———-

I am having some problem with the if-then-else syntax in .input files

Problem 1: I refer to the Axiom book which ships with the program (version 2005/09). At page 204 it says that the usual rules for piling are suspended in conditional expressions and it gives 5 different ways which should be equivalent, namely:

  if i>0 then output("positive") else output("nonpositive")
  if i>0 then output("positive")
    else output("nonpositive")
  if i>0 then output("positive")
  else output("nonpositive")
  if i>0
  then output("positive")
  else output("nonpositive")
  if i>0
    then output("positive")
    else output("nonpositive")

Well, I tried all of them, creating each time a blank r.input file with inside only: — * —

--S 1 of 20
i:=2
--R
--R
--R   (1)  2
--R                                                        Type: PositiveInteger
--E 1

———-

followed by one of the “if…”.

The result of the first two are ok, whereas the other three give some syntax error (different each time).

— * —

--S 2 of 20
for i in 2..2 repeat
  if i>0 then output("positive") else output("nonpositive")
--R
--R   positive
--R                                                                   Type: Void
--E 2
--S 3 of 20
for i in 2..2 repeat
  if i>0 then output("positive")
    else output("nonpositive")
--R
--R   positive
--R                                                                   Type: Void
--E 3
--S 4 of 20
for i in 2..2 repeat
  if i>0 then output("positive")
  else output("nonpositive")
--R
--R   positive
--R                                                                   Type: Void
--E 4
--S 5 of 20
for i in 2..2 repeat
  if i>0
  then output("positive")
  else output("nonpositive")
--R
--R   positive
--R                                                                   Type: Void
--E 5
--S 6 of 20
for i in 2..2 repeat
  if i>0
    then output("positive")
    else output("nonpositive")
--R
--R  Line  48: --R
--R  Line  49: --R   positive
--R  Line  50: --R                                                                   Type: Void

--R  Line  51: --E 5
--R  Line  52:
--R  Line  53: --S 6 of 20
--R  Line  54: for i in 2..2 repeat
--R  Line  55:   if i>0
--R           ..A
--R  Error  A: (from #\A and on) Ignored from here
--R  Line  56:     then output("positive")
--R           ....A
--R  Error  A: Improper syntax.
--R  Error  A: (from #\A up to ) Ignored.
--R  Line  57:     else output("nonpositive")
--R           ....A........................B
--R  Error  A: Improper syntax.
--R  Error  A: (from #\A up to #\B) Ignored.
--R  Error  B: Possibly missing a then
--R  Error  B: (up to #\B) to here.
--R   7 error(s) parsing
--E 6
--S 7 of 20
i:=2
--R
--R
--R   (6)  2
--R                                                        Type: PositiveInteger
--E 7
--S 8 of 20
for i in 2..2 repeat
  if i>0 then
    output(i)
    output("positive")
  else
    output(i)
    else output("nonpositive")
--R
--R  Line  84: --R
--R  Line  85: --R
--R  Line  86: --R   (6)  2
--I  Line  87: --R
--R  Line  88: --E 7
--R  Line  89:
--R  Line  90: --S 8 of 20
--R  Line  91: for i in 2..2 repeat
--R  Line  92:   if i>0 then
--R  Line  93:     output(i)
--R  Line  94:     output("positive")
--R  Line  95:   else
--R  Line  96:     output(i)

--R  Line  97:     else output("nonpositive")
--R           ....A
--R  Error  A: (from #\A up to ) Ignored.
--R  Error  A: Improper syntax.
--R   2 error(s) parsing
--E 8
--S 9 of 20
i:=1.5
--R
--R
--R   (7)  1.5
--R                                                                  Type: Float
--E 9
--S 10 of 20
a:=
  if i > 0 then
    j:=sin(i*pi())
    exp(j+1/j)
  else
    j:=cos(i*0.5*pi())
    log(abs(j)**5+1)
--R
--R
--R   (8)  0.1353352832 3661269189
--R                                                                  Type: Float
--E 10

———-

Problem 2: again with if then else, in a complex function I noticed a wrong result (the function itself was ok). Simplifying, you can produce the same error with something like:

— * —

--S 11 of 20
test: (INT,INT) -> List(INT)
--R
--R                                                                   Type: Void
--E 11
--S 12 of 20
test(a,b) ==
  x:=0; y:=0
  if (a rem b = 0) and b < 0 then
    x := 1
    y := 1
  [x,y]
--R

--R                                                                   Type: Void
--E 12
--S 13 of 20
4 rem -2
--R
--R
--R   (11)  0
--R                                                     Type: NonNegativeInteger
--E 13
--S 14 of 20
test(4,-2)
--R
--R   Compiling function test with type (Integer,Integer) -> List(Integer)
--R
--R
--R   (12)  [1,1]
--R                                                          Type: List(Integer)
--E 14

———-

returns correctly [1,1], but — * —

--S 15 of 20
4 rem -3
--R
--R
--R   (13)  1
--R                                                        Type: PositiveInteger
--E 15
--S 16 of 20
test(4,-3)
--R
--R
--R   (14)  [0,0]
--R                                                          Type: List(Integer)
--E 16

———-

or — * —

--S 17 of 20
4 rem 2

--R
--R
--R   (15)  0
--R                                                     Type: NonNegativeInteger
--E 17
--S 18 of 20
test(4,2)
--R
--R
--R   (16)  [0,0]
--R                                                          Type: List(Integer)
--E 18

———-

wrongly returns [0,1]. The same happens if I write — * —

--S 19 of 20
test1: (INT,INT) -> List(INT)
--R
--R                                                                   Type: Void
--E 19
--S 20 of 20
test1(a,b) ==
  x := 0; y := 0
  if (a rem b = 0) and b < 0 then x := 1 ; y := 1
  [x,y]
--R
--R                                                                   Type: Void
--E 20

———-

Why?

— * —

)spool
)lisp (bye)

———-

References^{[1] nothing}

8