Theorem 7.5 Assn is expressive
9.2 Operational semantics of call-by-value Assume a declaration d of
9.2 Operational semantics of call-by-value
The evaluation relation is deterministic:
Proposition 9.2 If t ~~a nl and t ~~a n2 then nl
==
n2·Proof: By a routine application of rule induction. D
9.3 Denotational semantics of call-by-value
Terms will be assigned meanings in the presence of environments for the variables and function variables. An environment for variables is a function
p:Var~N
We shall write Envva
=
[Var ~ N] for the cpo of all such environments.An environment for the function variables
il, ... , ik
is a tuple ({J=
(({JI,"" ({Jk) whereWe write Fenv va for [Na, ~ N.d x ... x [Nak ~ N -L], the cpo of environments for func-tion variables. As expected, a declarafunc-tion determines a particular funcfunc-tion environment.
Given environments ({J, p for function variables and variables, a term denotes an element of N -L. More precisely, a term
t
denotes a function[t]va E [Fenv va ~ [Env va ~ N-Lll given by the following structural induction:
[n]va [X]va [tl op t2]va [if to then it else t2]va [!i(tl, ... , ta.)]va
A({JAp·l n
J
A({JAp·lp( x)
J
A'PAp. [tl]va({JP OP-L [t2]va({JP for operations op taken as
+, -,
xA({JAp. Cond([to]va({JP, [h]va({JP, [t2]va({JP) A({JAp.
(let VI {= [tl]va({JP'''''Vai {= [ta;]va({JP. ({Ji(Vl, ... ,VaJ) The definition has used the strict extensions
+
-L, - -L, X -L of the usual arithmetic opera-tions on N; recall, for instance, from 8.3.4 thatZl
= lntJ
and Z2= ln2J
for some nl,n2 EN, otherwise
for Zl, Z2 EN. The function
is used in defining the meaning of a conditional. It satisfies {
Zl if Cond(zo, Zl, Z2) = Z2 if
.1 otherwise
Zo = LOJ,
Zo = LnJ for some n E N with n
i=
0, for zo, Zl, Z2 E N.L. It can be obtained from the conditional introduced earlier in 8.3.5.Let is zero : N -; T take the value true on argument 0 and false elsewhere. The function iszero is continuous being a function between discrete cpo's, so its strict extension
iszero.L
=
Az E N.L. let n -¢= £. Liszero(n)J is continuous and acts soNow we see
{ LtrueJ iszero.L(z)
=
~alseJif z = LOJ,
if z = L n
J
& ni=
0,otherwise.
Cond(zo,Zl,Z2)
=
(iszero.L(zo) -; Zl[Z2)for Zo, Zl, Z2 E N.L. Thus certainly it is a continuous function by Section 8.4. Indeed, for any term t of REC, the semantic function [t]va is a continuous function. This follows directly from the following lemma:
Lemma 9.3 For all terms t of REC, the denotation [t] va is a continuous function in [Fenvva -; [Envva -; N.LJJ.
Proof: The proof proceeds by structural induction on terms
t
using the results fromSection 8.4. 0
We observe that the intuitively obvious fact that the result of the denotation of a term in an environment does not depend on the assignment of values to variables outside the term:
Lemma 9.4 For all terms t of REC, if environments p,p' E Envva yield the same result on all variables which appear in t then, for any <p E Fenv va,
In particular, the denotation [t]varpp of a closed term t is independent of the environment p.
Proof: A straightforward structural induction on terms t. D
The semantics above expresses the meaning of a term with respect to a function envi-ronment 'P
=
('Pl, ... ,'Pk). The exact function environment is determined by a declara-tion consisting of defining equadeclara-tionsThis can be understood as recursive equations in
II, ... ,
fk which must be satisfied by the function environment 0 = (01'···' Ok):We have used some new notation for updating the environment p. Define p[n/x], where x E Var and n E N, to be the environment such that
(p[n/x])(y) = {
~(y)
if y "=t x, if y==
x.Alternatively we can define the updated environment in the metalanguage of Section 8.4.
Notice that the discrete cpo Var can be regarded as a sum of the singleton {x} and Var \ {x} in which the injection functions in 1 : {x} -+ Var and in2 : (Var \ {x}) -+ Var are the inclusion functions. Now we see that p[n/xJ is equal to
>.y E Var. case y of indx). n I in2(w). p(w).
We have used terms like p[no/xo,
nI/xIJ
etc. to abbreviate (p[no/xo])[nI/xIJ etc.(Note that this argument assumes nothing special about the cpo of integers, and in fact similar updating operations can be defined in the metalanguage when variables are bound to elements of other more complicated cpo's.)
The equations will not in general determine a unique solution. However there is a least solution, that obtained as the least fixed point of the continuous function
F : Fenv va -+ Fenv va
given by
F(rp) (Anl, ... ,na1 EN. [dI]varpp[nl/x1, ... ,nal!Xal],""
AnI, .. " nak EN. [ddvarpp[nl/xI,"" nak!XaJ).
The function F is continuous because it is built up from the functions [dI]va,"" [dk]va, known to be continuous by Lemma 9.3, using the methods admitted in Section 8.4.
Now we can define the function environment determined by the declaration d to be the least fixed point
6 = fix(F).
A closed term t denotes a result [t]va6p in NJ. with respect to this function environment, independent of what environment p is used. Of course, we had better check it agrees with the value given by the operational semantics. F~t this we do in the next section.
We conclude our presentation of the denotational semantics for the call-by-value eval-uation of REC by considering some examples to illustrate how the semantics captures evaluation.
Example: To see how the denotational semantics captures the call-by-value style of evaluation, consider the declaration:
h =It +
1hex)
=1(Here
It
is a function taking no arguments, i. e. a constant, defined recursively.)According to the denotational semantics, the effect of this declaration is that
f
1, hare denoted by 6 = (61,82 ) E NJ. x [N -> NJ.J where(61,82) =J-trp.
([It +
l]varpp, Am E N. [l]varpp[mjx])=J-trp. (rpi
+
J.l1J,
Am EN.l1J)
In this case it is easy to see that
(1-, Am E N.
llJ)
is the required least fixed point (it can simply be checked that this pair is a fixed point of Arp. (rpi
+
J.llJ,
Am E N.llJ)
and has to be the least). Thus81
=1-82 =Am E N.
llJ
from which
[h(h)]vabp =let n1 ~ b1· b2(nt)
=-'-.
o
Example: This next example involves a more detailed analysis of a least fixed point.
Consider the declaration
f(x)
=
if x then 1 else x x f(x - 1).In this example we are only interested in
f,
so for simplicity we take the function en-vironment Fenv va to simply be [N --+ N l.]. According to the denotational semantics this declaresf
to be the function b where, letting t be the definition and p an arbitrary environment for variables:b /Lip. (Am. [t]vaipp[m/x]) fix(Aip. (Am. [t]vaipp[m/x]))
U
b(r).rEw
Above we have taken
F(ip)
=
(Am. [t]vaipp[m/x]).and defined
From the denotational semantics, recalling the definition of Cond, we obtain F(ip)(m) =Cond(Lmj, L1j, Lmj xl. ip(m -1))
=iszero 1. (l m J) --+ L 1 j
I
L m j x 1. ip( m - 1) for ip E [N --+ N1.J and mEN. Now noteF(ip)(m) = cond(iszero(m), Llj, Lmj xl. ip(m -1))
where we make use of the function cond : T x N l. X N 1. --+ N 1. from Section 8.3.5 on sums of cpo's. For an arbitrary mEN, we calculate:
cond(iszero(m), L1j, LmJ Xl. b(O)(m - 1))
Generally we have
ifm
=
0 otherwise.cond(iszero(m), llj, lmj Xl. 8(1)(m - 1)) { ..ill j if m
=
0 or m=
1otherwise.
8(r)(m)
=
F(8(r-l))(m) = cond(iszero(m), llj, lmj Xl. 8(r-l)(m -1)) and, by mathematical induction, we can obtainifO:::;m<r otherwise.
As we expect the least upper bound 8 is the factorial function on non-negative integers and ..i elsewhere:
8(m) = {
r!j
if 0:::; m otherwise.(This example is not changed substantially in moving to a call-by-name regime.) 0