Re: [classad-users] Newbie Question: Evaluation of logicalexpressions


Date: Mon, 4 Feb 2002 09:02:04 -0600 (CST)
From: Marvin Solomon <solomon@xxxxxxxxxxxxxxxxxxx>
Subject: Re: [classad-users] Newbie Question: Evaluation of logicalexpressions
Alain Aslag Roy wrote:
> 
> >can someone tell me, if classadd Operators && (logical and) and || (logical
> >or) always compute their operands or if there's a shortcut as in the C
> >programming language?
> 
> They are always evaluated, there is no shortcut.
> 
> You can see this for yourself from the _Evaluate member functions in
> operators.h: all operands are evaluated, then the operator is evaluated.
> 

That's not how I understand it.

I'm sure you're wrong about "new" classads, and I suspect you're wrong about
"old" classads as well.

First of all, I don't understand your reference to operators.h.  I'm looking at
src/condor/condor_classad/operators.h, and all I see is a big enum declaration
and a couple of function prototypes.  If I look at operators.C, in
_doOperation, I see this

    // test for cases when evaluation is strict w.r.t. "error" and "undefined"
    if (op != META_EQUAL_OP && op != META_NOT_EQUAL_OP  &&
        op != LOGICAL_OR_OP && op != LOGICAL_AND_OP     &&
        op != TERNARY_OP)
    {
        ... return ...
    }
    ...
    // logical operators (binary, one unary)
    if (op >= __LOGIC_START__ && op <= __LOGIC_END__)
    {
        doLogical (op, val1, val2, result);
        return;
    }
and doLogical makes it clear that doLogical(LOGICAL_AND_OP, false, error)
returns false.

A very literal interpretation of the question results in the answer
"yes, both operands are evaluated", but that's very misleading.

Quoting from the original question:

> Expression:
> 
>    x == 1 && some_func(foo)
> 
> Is "some_func(foo)" called if "x == 1" evaluates to "false" or is it
> bypassed.
> 

In fact, it seems that some_func(foo) is always evaluated.  However, all
functions that can appear in classads are "pure" (they have no side effects),
and they return a special ERROR value on failure.  So if some_func(foo) fails,
the cited expression will evalute to

    false && ERROR

which evaluates to false.  The net effect is "as if" the function call
was bypased.

Indeed the logical operators are "better" than shortcut operators.  Both

    x isnt UNDEFINED && x == 1      // That's "x =/= UNDEFINED" in old classads

and

    x == 1 && x isnt UNDEFINED

evalute to false, not UNDEFINED, if x is undefined.  It would appear that the
same is true for errors in the old C++ classad code.  The semantics as I
understand them are a bit different for the Java version (and, I belive, new
classads).  The operators are "optimistic" for UNDEFINED (as in example above),
but "sequential" for ERROR. If x==0, then

    x == 0 || 1/x > 1000

evaluates to "true", but 

    1/x > 1000 || x == 0

evaluates to ERROR, because that's what most people would expect.
Condor Classads Info:
http://www.cs.wisc.edu/condor/classad/




[← Prev in Thread] Current Thread [Next in Thread→]