unevaluated boolean and conditional expressions (take 1)



On 1/29/06, Stavros Macrakis <macrakis at gmail.com> wrote:

> Previously, in programming constructs, an unevaluable conditional
> would cause an error, which was often the right thing.  Now it does
> nothing, silently.

The prederror flag is still observed in the code, so the
previous mode is still available. Setting prederror : true
banishes unevaluated conditionals.

> So for example:
>
>          if x>0 then print(1) else print(2)
>
> previously would print either 1 or 2, or cause an error.  Now it can
> silently do nothing.

"If" evaluates its arguments, so evaluating the above
expression actually causes both 1 and 2 to be printed
(when prederror = false).

But that's life with side effects. Alternatives here would be
to call MEVALATOMS on the arguments or to quote them
verbatim. There is some potential, in the current version,
for confusion and anguish involving side effects, but
I see greater potential due to evaluating arguments less.

> I presume you haven't modified "for" and "while", though....

That is correct.

> Another problem is with variable scope:
>
>      f(x,y) := if y=0 then 0 else 1/x;

I think you must have meant equal (y, 0) here;
is(y = 0) always has a definite value, so
if y = 0 ... won't yield an unevaluated conditional.

> What is the value of f(0,n)?  It had better not be
>
>       if y=0 then 0 else 1/x

No, it's not. It wants to be "if equal (n, 0) then 0 else 1/0"
but simplification of 1/0 triggers an error.

> You could return a block, e.g.
>
>       block([x:0,y:n],if y=0 then 0 else 1/x)
>
> If you try to substitute the variables, you get a different problem --
> Maxima has no way of deferring an error....
>
>       if n=0 then 0 else 1/0
>
> which will cause a simplification error (division by zero).

I'm not convinced that we should try to avoid side effects
(such as printing stuff and errors triggered from simplification).

My primary consideration here is that the current policy
(evalute the condition and arguments and paste them
into another "if" expression) is easy to describe,
and therefore more comprehensible.

In the presence of side effects, I can see that MEVALATOMS
could be useful, but I am wondering how to explain that.

Thanks for your comments,

Robert