## `_lazy_and, _lazy_or` -- ``lazy evaluation'' of Boolean expressions

#### Introduction

`_lazy_and(b1, b2...)` evaluates the Boolean expression `b1 and b2 and ...` by ``lazy evaluation''.

`_lazy_or(b1, b2...)` evaluates the Boolean expression `b1 or b2 or ...` by ``lazy evaluation''.

#### Call(s)

`_lazy_and(b1, b2...)`
`_lazy_or(b1, b2...)`

#### Parameters

`b1, b2, ...` | - | Boolean expressions

#### Returns

`TRUE`, `FALSE`, or `UNKNOWN`.

### Overloadable:

`b1`, `b2`, `...`

#### Related Functions

`and`, `bool`, `if`, `is`, `or`, `repeat`, `while`, `FALSE`, `TRUE`, `UNKNOWN`

#### Details

- `_lazy_and(b1, b2...)` produces the same result as `bool(b1 and b2 and ...)`, provided the latter call does not produce an error. The difference between these calls is as follows:

  `bool(b1 and b2 and ...)` evaluates all Boolean expressions before combining them logically via 'and'.

  Note that the result is `FALSE` if one of `b1`, `b2` etc. evaluates to `FALSE`. ``Lazy evaluation'' is based on this fact: `_lazy_and(b1, b2...)` evaluates the arguments from left to right. The evaluation is stopped immediately if one argument evaluates to `FALSE`. In this case, `_lazy_and` returns `FALSE` without evaluating the remaining Boolean expressions. If none of the expressions `b1`, `b2` etc. evaluates to `FALSE`, then all arguments are evaluated and the corresponding result `TRUE` or `UNKNOWN` is returned.

  `_lazy_and` is also called ``conditional `and`''.

- `_lazy_or(b1, b2...)` produces the same result as `bool(b1 or b2 or ...)`, provided the latter call does not produce an error. The difference between these calls is as follows:

  `bool(b1 or b2 or ...)` evaluates all Boolean expressions before combining them logically via 'or'.

  Note that the result is `TRUE` if one of `b1`, `b2` etc. evaluates to `TRUE`. ``Lazy evaluation'' is based on this fact: `_lazy_or(b1, b2...)` evaluates the arguments from left to right. The evaluation is stopped immediately if one argument evaluates to `TRUE`. In this case, `_lazy_or` returns `TRUE` without evaluating the remaining Boolean expressions. If none of the expressions `b1`, `b2` etc. evaluates to `TRUE`, then all arguments are evaluated and the corresponding result `FALSE` or `UNKNOWN` is returned.

  `_lazy_or` is also called ``conditional `or`''.

- If any of the considered Boolean expressions `b1`, `b2` etc. cannot be evaluated to `TRUE`, `FALSE`, or `UNKNOWN`, then `_lazy_and`, `_lazy_or` produce errors.
- `_lazy_and` and `_lazy_or` are internally used by the `if`, `repeat`, and `while` statements. For example, the statement '`if b1 and b2 then ...`' is equivalent to '`if _lazy_and(b1, b2) then ...`'.

- `_lazy_and()` returns `TRUE`.
- `_lazy_or()` returns `FALSE`.
- `_lazy_and` is a function of the system kernel.
- `_lazy_or` is a function of the system kernel.

#### Example 1

This example demonstrates the difference between lazy evaluation and complete evaluation of Boolean conditions. For x = 0, the evaluation of sin(1/x) leads to an error:

    >> x := 0: bool(x <> 0 and sin(1/x) = 0)

          Error: Division by zero

With ``lazy evaluation'', the expression sin(1/x) = 0 is not evaluated. This avoids the previous error:

    >> _lazy_and(x <> 0, sin(1/x) = 0)

                                       FALSE

    >> bool(x = 0 or sin(1/x) = 0)

          Error: Division by zero

    >> _lazy_or(x = 0, sin(1/x) = 0)

                                       TRUE

    >> delete x:

#### Example 2

The following statements do no produce an error, because `if` uses lazy evaluation internally:

    >> for x in [0, PI, 1/PI] do
         if x = 0 or sin(1/x) = 0 then
            print(x)
         end_if;
       end_for:

                                         0

                                        1
                                        --
                                        PI

    >> delete x:

#### Example 3

Both functions can be called without parameters:

    >> _lazy_and(), _lazy_or()

                                    TRUE, FALSE

#### Changes

- no changes

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000