This preview shows pages 1–3. Sign up to view the full content.
CS 3110 Lecture 6
The Substitution Model of Evaluation
In this lecture, we examine how OCaml programs evaluate more closely, building a more formal and precise
description of the evaluation process. This is a model of evaluation based on the basic notion of
substitution
,
in which variable names are replaced by values that they are bound to. This corresponds to the mathematical
notion that two equal things are interchangeable.
A tricky example
What is the value of the following expression? (Note that this is not just a definition of a function, it binds
two names
evil
and
dummy
to functions and then applies
evil
to three arguments, returning the value of
that expression. The names
evil
and
dummy
are bound only in the body and not at top level.)
let rec evil(f1, f2, n) =
let f(x) = 10 + n in
if n = 1 then f(0) + f1(0) + f2(0)
else evil(f, f1, n1)
and dummy(x) = 1000
in
evil(dummy, dummy, 3)
We can see that the function
evil
calls itself recursively, and the result of the function is the result when it
is called with
n=1
. But what are the values returned by the applications of functions
f
,
f1
and
f2
? To
understand what those values are, we need to better understand the OCaml evaluation model, and how
variable names like
n
are bound.
Evaluation
The OCaml prompt lets you type either a term or a declaration that binds a variable to a term. It
evaluates
the
term to produce a
value
: a term that does not need any further evaluation. We can define values
v
as a
syntactic class too. Values include not only constants, but tuples of values, variant constructors applied to
values, and functions.
Running an ML program is just evaluating a term. What happens when we evaluate a term? In an imperative
(nonfunctional) language like Java, we sometimes imagine that there is an idea of a "current statement" that
is executing. This isn't a very good model for ML; it is better to think of ML programs as being evaluated in
the same way that you would evaluate a mathematical expression. For example, if you see an expression like
(1+2)*4, you know that you first evaluate the subexpression 1+2, getting a new expression 3*4. Then you
evaluate 3*4. ML evaluation works the same way. As each point in time, the ML evaluator
rewrites
the
program expression to another expression. Assuming that evaluation eventually terminates, eventually the
whole expression is a value and then evaluation stops: the program is done. Or maybe the expression never
reduces to a value, in which case you have an infinite loop.
Rewriting works by performing simple steps called
reductions
. In the arithmetic example above, the rewrite
is performed by doing the reduction
1+2
→
3
within the larger expression, replacing the occurrence of the
subexpression
1+2
with the righthand side of the reduction,
3
, therefore rewriting
(1+2)*4
to
3*4
.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThe next question is which reduction OCaml does. Fortunately, there is a simple rule. Evaluation works by
always performing the
leftmost
reduction that is allowed. So we can describe evaluation precisely by simply
This is the end of the preview. Sign up
to
access the rest of the document.
 '07
 GIAMBATTISTA,A

Click to edit the document details