Professional Documents
Culture Documents
The Substitution Model
The Substitution Model
John Michael Rono SCT211-0033/2019
Olympiah Otieno SCT211-0456/2019
Vincent Gitonga SCT211-0032/2019
Sonia Onyimbo SCT211-0418/2019
The Substitution Model
The substitution model provides us with a means of determining the value of
Scheme expressions. In learning and using the substitution model, please keep in
mind it is intended as a tool for understanding the evolution of the evaluation of
expressions. It is not a direct model of how the computer actually performs the
evaluation. We will use some shorthand notation to bury unneeded detail with
which you need not be concerned, but which the computer must actually
manipulate.
Representations of Values
is represented by
{proc (x) (* x x)}
Rules of Evaluation
The rules for compound expressions are:
EVAL-C
To evaluate any compound expression other than a special form, evaluate the subexpressions of the
compound expression, then apply the procedure that is the value of the leftmost subexpression to
the arguments that are the values of the other subexpressions.
APPLY-C
To apply a compound procedure to a set of arguments, evaluate the body of the procedure with each
formal parameter replaced by the corresponding argument value.
EVAL-P1
The value of a primitive numeric expression is the number that it names; e.g., the value of 5 is
{5}.
EVAL-P2
The value of a built-in primitive operator is a sequence of machine instructions to carry out
the operation. We denote this by placing braces around a name for the instructions (e.g., the
value of + is denoted {add}.
EVAL-P3
The value of any other name is the object that is associated with that name. This value is
obtained by looking up the name in a table and extracting the associated object. Objects can
come to be associated with names in various ways; for example, by using the special
form define. If no object is associated with the name, it is an error .
EVAL-P4
The value of an object enclosed in braces is itself. In other words, once an expression has
been evaluated, any further evaluation will just give the same value. An evaluated
expression may be used in some expression that still requires further evaluation, but the
object inside the braces is already fully evaluated.
APPLY-P
To apply a built-in procedure such as addition to its (evaluated) arguments, Scheme just
runs the code for that procedure on those arguments. In the substitution model, we just
supply the resulting value directly.
Consider:
((lambda (x) (* x x)) 5)
and the value of the expression 5 is {5}. Thus, after the first stage of evaluation, our model reduces the
evaluation in line 1 to the application of a procedure object to a set of values shown in line 2.
Now, rule APPLY-C reduces this to the evaluation of a simpler expression, namely the body of the
procedure with the value of the argument substituted (hence the name of the model) for the formal
parameter, as shown in line 3. Note that we have substituted the object {5} directly into the expression.
We now have an evaluation again. Since this is a compound expression, rule EVAL-C
applies again. Here, we already have values (or objects) for the second and third
subexpressions, as indicated by the braces. Thus rule EVAL-P4 holds, implying that no
further evaluation of these expressions is needed. The value of the first subexpression is
simply the machine instructions associated with this primitive operation, denoted by
{mult}, and is found by using rule EVAL-P3.
Hence, the evaluation of this expression reduces to the application shown in line 4. At this
point, we have a primitive built-in operation applied to primitive values. This situation is
governed by rule APPLY-P. We just replace the expression with its value, which is the final
value given in line 5.
Special Forms
The rules we have described deal with the evaluation of most expressions. There are, however,
a small number of special forms that do not obey these rules, and we need to describe a method for
dealing with them.
define
Note that only expression-2 is evaluated, not expression-1. We can think of this association occurring
by placing the pairing of the name and the value in a lookup table, so that evaluating the name later on
allows us to find the associated value.
Example
Discuss the Substitution model evaluation and construct example look-up tables
starting with:
(define a 2)
(define square (lambda (x) (* x x)))
(define cube (lambda (x) (* x x x)))
Name Value
* {mult}
+ {add}
. .
. .
. .
a {2}