Karim BELABAS on Tue, 5 Dec 2000 09:41:33 +0100 (MET)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: inequalities


[Fernando Rodriguez Villegas:]
> We were fooled by the following
> 
> ? 2<4<3
> %108 = 1
> ? 2<1<0
> %109 = 0
> ? 1<2<3
> %110 = 1
> ? -1<2<3
> %111 = 1
> ? -2<-1<0
> %112 = 0
> 
>  We should write these with &&'s but shouldn't we have been warned by
>  the GP parser?

"Could", maybe; "should", definitely not. Operators of identical priority (in
particular identical operators...) are left associative. Hence

   2 < 4 < 3

is parsed as 

   ((2 < 4) < 3)

which evaluates first to (1 < 3)   [since the result of the first
comparison is 1]  then to 1.

In this particular case, associativity doesn't make any mathematical sense,
so it could be disabled. But this would complicate (a lot) the parser [there
would certainly be a number of operators whose use should be likewise
restricted] for a very small gain in my opinion.


On the other hand, it could be useful to have a "GP compiler" (distinct from
the main interpreter, and only used to check scripts) able to detect such
pitfalls, e.g:

  a < b < c
  Warning: dubious associativity, you probably want to use &&

  if (a = 1, ....
  Warning: assignment used as truth value

  a+1; b ...
  Warning: statement with no effect

As a matter of fact, Bill Allombert has written a GP --> C converter [soon to
be announced...], and gcc -Wall might be able to do just that, at least for
_some_ potential problems.  He might be willing to complicate the grammar of
his parser to catch some of the other things, which are valid given the
current GP grammar, but really shouldn't be allowed in a "clean" programming
language. Of course, the whole point is to issue Warnings upon user's
request, not to disallow existing "obfuscated" constructs.

Cheers,

    Karim.
-- 
Karim Belabas                    email: Karim.Belabas@math.u-psud.fr
Dep. de Mathematiques, Bat. 425
Universite Paris-Sud             Tel: (00 33) 1 69 15 57 48
F-91405 Orsay (France)           Fax: (00 33) 1 69 15 60 19
--
PARI/GP Home Page: http://www.parigp-home.de/