On Sun, Nov 19, 2023 at 07:58:50PM +0800, Qian Yun wrote:
> "real? sqrt (-sqrt 2)" returns "true" by default.
You mean should return false? We get this when working
with expressions:
(2) -> real? sqrt (-sqrt 2::EXPR(INT))
(2) false
> Because interpreter prefers CTRIGMNP over TRIGMNIP
> as shown by ")set mes bot on":
>
> Function Selection for real?
> Arguments: AN
>
> [1] signature: EXPR(COMPLEX(INT)) -> BOOLEAN
> implemented: slot (Boolean)(Expression (Complex (Integer))) from
> CTRIGMNP(INT,EXPR(COMPLEX(INT)))
> [2] signature: EXPR(INT) -> BOOLEAN
> implemented: slot (Boolean)(Expression (Integer)) from
> TRIGMNIP(INT,EXPR(INT))
>
>
> Shall we consider this a bug?
I do not think so. IIUC 'real?' in CTRIGMNP is quite different
function than 'real?' in TRIGMNIP. Namely, CTRIGMNP deals
with COMPLEX(EXPR(INT)), which is formal linear combination
'a + %i*b' where 'a' and 'b' are expressions and 'real?'
here tests if 'b' equals 0, which AFAICS is true.
'real?' in TRIGMNIP is making reasonable effort to find out if
"real" transformations of functions will give correct results.
Your expression is affected by trouble, what is meant by
'sqrt'? '-sqrt(2)' have two possible values, positive and negative.
So numerical value depends on choice of branches. One possible
meaning of 'real?' could be "argument is real for all possible choices
of branches". Another could be "argument is real for some choice
of branches". Yet another (and it is more or less what we use)
is that argument is "formally real", that is consists of
applications of "real" functions to "formally real" argument
with exception made for roots: even root of negative constant
is considered not-real. Each meaning have some sense. The first
two are problemantic, because in general branch questions are
undecidable. The third one is decidable and useful in some
contexts. But may be confusing for some users. And your case
is borderline for this definition: if sqrt(2) is considered as function
applied to a constant, than correct answer is "true". If
sqrt(2) is considered to be a positive constant, then
answer is "false". For our purposes it does not matter much.
We just need to be consistent: tests used in various places
should be "uniformly dumb", otherwise there is danger that
something which we consider real will be transformed into
something visibly complex by "too smart" transformation,
leading to possible bugs. And ideally we should not depend
on 'real?' at all: when we really care about expressions having
real values we should use stronger (or at least more reliable)
tests.
--
Waldek Hebisch