User:Thepigdog/Evaluation strategy

The evaluation strategy determines the order of evaluation (reduction). The order of evaluation may effect the result calculated, and may determine if a result is ever returned.

There are many different evaluation strategies that may be used. These strategies may be classified as eager (also called strict) or lazy.

Lazy evaluation is commonly used in modern functional languages, because lazy evaluation may return a value for an expression when eager evaluation will not. A lazy strategy evaluates values only if the value is needed in calculating the final value.

Eager strategies have advantages in simplicity of implementation, and in speed. But these advantages are considered less important in modern languages.

Technically, in the Lambda Calculus, the values obtained from the two strategies are slightly different, because eager strategies perform more reduction.

Theoretically multiple processors may be applied to the evaluation of functions because the order of evaluation of functions does not effect the result.

History
Historically in languages the evaluation strategy has referred to how the parameters to a function are treated. The different approaches are affected by there use in impure (imperative languages).

The various approaches are summarized below,

Evaluation strategy
The definitions given here assume that the first definition that matches the lambda expression will be used. This convention is used to make the definition more readable. Otherwise some if conditions would be required to make the definition precise.

Running or evaluating a lambda expression L is,
 * $$ \operatorname{eval}[L] $$

where eval is defined by,
 * $$ \operatorname{eval}[x\ y] = \operatorname{eval}[\operatorname{apply}[\operatorname{eval}[x]\ \operatorname{strategy}[y]]] $$
 * $$ \operatorname{apply}[(\lambda x.y)\ z] = \operatorname{beta-redex}[(\lambda x.y)\ z] $$
 * $$ \operatorname{apply}[x] = x $$ If x does match the above.
 * $$ \operatorname{eval}[\lambda x.(f\ x)] = \operatorname{eval}[\operatorname{eta-redex}[\lambda x.(f\ x)]] $$
 * $$ \operatorname{eval}[L] = L $$
 * $$ \operatorname{lazy}[X] = X $$
 * $$ \operatorname{eager}[X] = \operatorname{eval}[X] $$

Then the evaluation strategy may be chosen as either,
 * $$ \operatorname{strategy} = \operatorname{lazy} $$
 * $$ \operatorname{strategy} = \operatorname{eager} $$

The result may be different depending on the strategy used. Eager evaluation will apply all reductions possible, leaving the result in normal form, while lazy evaluation will omit some reductions in parameters, leaving the result in "weak head normal form".

Normal form
All reductions that can be applied have been applied. This is the result obtained from applying eager evaluation.
 * $$ \operatorname{normal}[\lambda x.y)\ z] = \operatorname{false} $$
 * $$ \operatorname{normal}[\lambda x.(f\ x)] = \operatorname{false} $$
 * $$ \operatorname{normal}[x\ y] = \operatorname{normal}[x] \land \operatorname{normal}[y] $$

In all other cases,
 * $$ \operatorname{normal}[x] = \operatorname{true} $$

Weak head normal form
Reductions to the function (the head) have been applied, but not all reductions to the parameter have been applied. This is the result obtained from applying eager evaluation.


 * $$ \operatorname{whnf}[\lambda x.y)\ z] = \operatorname{false} $$
 * $$ \operatorname{whnf}[\lambda x.(f\ x)] = \operatorname{false} $$
 * $$ \operatorname{whnf}[x\ y] = \operatorname{whnf}[x] $$

In all other cases,
 * $$ \operatorname{whnf}[x] = \operatorname{true} $$

Links

 * Lambda Calculus