Choosing What to Evaluate and When

An expression describes a computation, but its written form does not always specify when each component runs. An evaluation strategy supplies those rules: which subexpressions are evaluated, in what order, and when their values are used. You need these rules when tracing programs, comparing language implementations, or deciding whether a proposed simplification preserves behaviour. This lesson concentrates on expression evaluation rather than how functions receive arguments.

Order is different from grouping

For an arithmetic expression, parentheses and precedence determine which operands belong to each operator. Evaluation order determines when those operands are computed. Changing evaluation order does not exchange operand positions: subtraction still subtracts the right operand from the left operand.


Example: In mark(4) - mark(9), suppose mark(n) records n in a log and returns n. Left-to-right evaluation records 4, 9; right-to-left evaluation records 9, 4. Both produce 4−9=−54-9=-5, because the returned values still occupy their original positions.


The log is a side effect. When evaluation changes shared state, order can also change the numerical result. If two calls each read and increment the same counter, the call evaluated first receives the earlier counter value.


For pure, deterministic arithmetic whose operands all finish without error, reversing operand evaluation order preserves the value when grouping and operand positions stay fixed. This statement does not extend automatically to expressions that modify state, report errors, or run forever.



Which values are required?

Strictness describes whether an operation requires an operand's value before it can produce its result. Ordinary integer addition is strict in both operands. Even when one operand is already known, the other value is still required to perform the addition.


A conditional expression is selective. In if test then a else b, the evaluator first obtains the test result and then evaluates the selected branch. It does not evaluate the unselected branch merely because that branch appears in the source.


Expression form

Values required for a result

Order information still needed

Strict addition a + b

Both operand values

Whether a or b is evaluated first

Selective conditional

The test and the selected branch

The test precedes the selected branch

Short-circuit conjunction

The left Boolean, then the right Boolean if the left is true

Left precedes right whenever right is needed


Here, Boolean identifies the kind of value used by tests and logical operators. Short-circuit conjunction can return false as soon as its left operand is false.


Two diagrams compare strict left-to-right evaluation with selection of a conditional branch.



Trace only the work that runs

For a selective conditional, a simple timing model is


Tconditional=Ttest+Tselected branch.T_{\text{conditional}}=T_{\text{test}}+T_{\text{selected branch}}.


This equation assumes that the stated times include all relevant work, or that other overhead is explicitly ignored. Adding the times of both branches would count computation that never runs.


The same reasoning determines whether an expression finishes. Suppose spin() runs forever. With strict multiplication, 0 * spin() does not produce zero, because the evaluator still needs the second operand's value. By contrast, if true then 0 else spin() returns zero under selective conditional evaluation. An unused branch contributes neither its work nor its failure.


These examples concern the immediate rules for operators and conditionals. They do not require a general mechanism for postponing and sharing computations.

What to remember

  • Grouping fixes operand positions; evaluation order fixes when operands run.
  • Strictness identifies required values, while order sequences their evaluation.
  • Selective conditionals evaluate the test and one branch.
  • Compare results, observable actions, errors, and termination when comparing strategies.

Example: Tracing a telemetry score

A telemetry tool evaluates if ready() then sample() * 3 + sample() else sample(). Multiplication groups before addition. The conditional is selective, and both arithmetic operators are strict. An execution trace records the evaluations that actually occur and the values they produce.


For this benchmark, ready() returns true and takes 1 ms. A counter starts at 4; each call to sample() increments it by 1, returns its new integer value, and takes 2 ms. Calls run sequentially. Integer constants take no time, and all other evaluation costs are ignored. Reset the counter to 4 before each separate evaluation.


Keep each returned value attached to its original operand, even when the evaluation order changes.


a) Find the returned score when every binary arithmetic operator evaluates its left operand before its right operand.


First evaluate ready(). It returns true, so evaluate sample() * 3 + sample() and leave the else branch untouched.


The outer addition evaluates its left operand first. Within that multiplication, the first sample() call increments the counter to ________ and returns that value. The product is ________.


5×3=15.5\times3=15.


The addition then evaluates its right operand. The second call increments the counter from 5 to 6 and returns 6. Therefore,


15+6=21.15+6=21.


The returned score is 21.


b) Reset the counter and find the returned score when every binary arithmetic operator instead evaluates its right operand before its left operand.


The test still returns true and selects the same branch. The outer addition now evaluates its right-hand sample() first. That call returns ________.


Next evaluate the left operand, sample() * 3. Right-to-left evaluation of this multiplication evaluates the constant 3 first, then calls sample(). That second call returns 6, so the product is ________.


6×3=18.6\times3=18.


The product remains the left operand of addition, and the earlier returned 5 remains its right operand:


18+5=23.18+5=23.


The returned score is 23. The changed result comes from assigning successive counter values to different positions in the expression.


c) Calculate the total evaluation time for each strategy under the stated benchmark assumptions.


Each strategy calls ready() once and evaluates the selected branch, which makes ________ to sample(). The unselected branch makes no call.


The sampling calls take ________ altogether:


Tsamples=2×2 ms=4 ms.T_{\text{samples}}=2\times2\ \text{ms}=4\ \text{ms}.


Add the test's time:


T=1 ms+4 ms=5 ms.T=1\ \text{ms}+4\ \text{ms}=5\ \text{ms}.


The total is 5 ms for each strategy. The returned scores differ, but the specified calls and their fixed costs are the same in this benchmark.

A language specification says that integer addition is strict in both operands. For record(3) + record(8), each call records its argument and returns that integer. Both calls finish normally. Which further rule is needed to predict the recording sequence?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A language is strict and evaluates by value: every argument is evaluated fully before a function body starts, and the operands of + and the arguments of a call are evaluated left to right. Each function records a trace entry the moment its body begins executing.


Plain text
log(scale(3), shift(4)) + finish()


Arrange the events in the order they occur when this expression is evaluated.

  1. 1.

    The body of scale(3) begins executing.

  2. 2.

    The body of shift(4) begins executing.

  3. 3.

    The addition combines the result of log with the result of finish().

  4. 4.

    The body of log begins executing with both argument values.

  5. 5.

    The body of finish() begins executing.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An evaluator uses strict integer arithmetic and evaluates the right operand before the left operand at every binary operator. A counter starts at 3. Each call to take() increments the counter by 1 and returns its new value. Evaluate take() - (2 * take()). Give the returned integer to zero decimal places.


An expression tree has subtraction at its root and multiplication in its right subtree.


Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice