HANDBOOK / GENTLE PRIMER / Describing Computations /

# Evaluation Strategy

In the examples we have discussed in this chapter one rule at most was applicable for a given input. This is typical for many predicates because they are often written in a way that follows the type definition of their arguments.

There may also be definitions where more than one rule is applicable for a given input. For example, the definition of `favorite` could contain

       'rule' favorite (julia -> blue)
       'rule' favorite (julia -> red)

Several interpretations would be possible:

Shallow Backtracking. The first applicable rule is selected and it determines the result. For example, in

       favorite(julia -> X)

`X` is defined as `blue`. This approach treats predicates as functions.

Deep Backtracking. All results are delivered, one after the other. For example, in

       favorite(julia -> X) IsJimsColor(X)

`X` is first defined as `blue`, but `IsJimsColor` fails for this value, so `favorite` is resumed and then defines `X` as `red`. This approach treats predicates as relations.

Most of the concepts a compiler has to deal with are functions of other items: e.g. ``the type of an expression'', ``the offset of a variable'' (an expression has unique type, a variable has a unique offset). Hence, the usual strategy in Gentle is shallow backtracking.

However, there is one important concept that is not functional by nature: a given source construct may have various possible representations in the target language. Here, one wishes to list possible translations of pieces which are automatically selected and combined into a globally optimal translation. This is also supported by Gentle. Since the number of possibilities in general is exponential, Gentle code does not enumerate them, but computes the best rule selection using a linear time algorithm. This will be discussed later.