7.4.2 Evaluation order

Prev Up Next Page 183 of 800 Search internet

Most of the constructs presented so far compute their arguments left to right and inspect each resulting value for being an exception. If an argument evaluates into an exception, the remaining arguments are not evaluated, and the exception becomes the value of the expression. Some examples read:

2 + 3 == 5

2 raise + 3 == 2 raise

2 + 3 raise == 3 raise

2 raise + 3 raise == 2 raise

The examples above show that the plus operation 'relays' exceptions in the sense that if an argument evaluates to an exception, that exception is 'relayed' to become the return value. When more than one argument evaluates to an exception, the leftmost exception is relayed.

The following examples show the evaluation order more explicitly:

2 raise + bottom == 2 raise

bottom + 3 raise == bottom

Computation of bottom takes infinite time. In the first case above, 2 raise is evaluated yielding an exception which is relayed without the second argument being evaluated. In the second case above, bottom is evaluated which takes forever.

Most of the constructs presented so far are 'eager left-relaying' in the following sense:

They are eager in the sense that they evaluate all their arguments unless argument evaluation is aborted by an exception.

They are relaying in the sense that if an argument returns an exception, that exception becomes the result of the operation.

They are left-to-right in the sense that arguments are evaluated left to right.

The comments above should be taken with a grain of salt since an implementation is free to do computation in any order as long as the return value of each operation is as specified. As an example, consider this:

print ( !"A" ) + print ( !"B" )

Evaluation of the sum above will force both print statements to be evaluated. On some implementations, evaluation of a print statement has the side effect that the argument of the print statement is printed to the users terminal. On such implementations, one will typically see the A before the B because, typically, implementations actually do compute arguments left-to-right. But nothing prevents an implementation to do 'speculative parallelism' in which it computes both arguments in parallel and aborts the second computation if the first one raises an exception.

Three constructs presented so far in the present Programming language chapter are not eager left-relaying. They are:

if x then y else z

x .and. y

x .or. y

All three fail to be eager in that for some non-exceptional values of x they do not evaluate y.

Note that x raise itself is relaying. As an example, we have:

2 raise raise == 2 raise

On the left hand side above, the first raise construct raises an exception which the second raise construct relays.

The Logiweb programming language itself is a lazy language. It is just the constructs presented so far which are eager.

Furthermore, to Logiweb, exceptions are data structures like any other data structures. It is just the constructs presented so far which treat exceptions specially. As an example, it is possible to forge a pair whose first component is 2 and whose second component is 3 raise. But it is not possible to use x :: y to forge such a pair since the 2 :: 3 raise relays the 3 raise exception. That, however, is due to the behavior of the x :: y construct. Logiweb itself has no reservations against exceptions as components of pairs.

Prev Up Next Page 183 of 800 Search logiweb.eu

Copyright © 2010 Klaus Grue, GRD-2010-01-05