You are on page 1of 15

GROUP 7

 
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

 Before we can model the process of evaluating Scheme expressions, we need to


develop some notation for differentiating between objects that have already been
evaluated (e.g., intermediate values in a computation) and those that have not. In
the substitution model we will place braces { } around values in order to
distinguish them from objects that have not yet been evaluated. This notation is
used to designate the internal representation of an object used by the machine,
and implies that no further reduction is possible.
Primitive Data
 The value of a primitive numeric expression such as 4, -3.1, or 2.71828 is the
number that it names. We denote this by placing the number in braces { }.
 For example, the value of the numeral 2 is {2}.
Primitive procedures
The interpreter provides a set of primitive procedures that form the building blocks
for our own procedures, including things like addition and multiplication. These are
represented by Scheme expressions such as + and *. Thus, the value of the
expression + is the addition procedure, which roughly speaking is the code needed to
execute the procedure. We will represent these values by braces surrounding some
suggestive name; for example,
the value of + is {add}
the value of - is {sub}
the value of * is {mult}
the value of / is {div}
Compound Procedures
 Compound procedures are created by evaluating a lambda expression,
which takes a list of parameters and a body, and creates a procedure object.
We will use the special symbol proc and braces to denote the procedure that results from
evaluating
a lambda expression. We also include the arguments to the procedure and the body of
the procedure.
 For example, the value obtained by evaluating

(lambda (x) (* x x))

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.

 Note that this results in an alternation of evaluation and application stages, which continue until


reducing the expression to primitives.
The Rules for Primitive expressions are:

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.

 An expression that is being evaluated will be denoted by parentheses ( ). An expression


that denotes the application of a procedure will be represented by square brackets [ ].
Example

 Consider:
((lambda (x) (* x x)) 5)

The following table denotes the full evaluation:


1.((lambda (x) (* x x)) 5)
2.[{proc (x) (* x x)} {5}]
3.(* {5} {5})
4.[{mult} {5} {5}]
5.{25}
We begin with line 1. Since this is an evaluation of a compound expression, rule EVAL-C says
we must first evaluate the subexpressions. The value obtained by evaluating the lambda expression is a
procedure object, denoted by
{proc (x) (* x x)}

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

When evaluating a define expression, the rules change slightly. In particular, in evaluating

(define expression-1 expression-2)

we first check that expression-1 is a symbol. Then we evaluate expression-2 using the evaluation rules


above and associate the result with the name given in expression-1.

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}

square {proc (x) (* x x)}

cube {proc (x) (* x x x)}

You might also like