CIS 705 — Programming Languages — Spring 2010
Assignment 1 (100 Points)
Due by 2:30 p.m. on Thursday, February 11 The context for this assignment is Chapters 2-3 ofTAPL. Terms
The set ofterms is inductively defined by:
t::= terms:
true constant true
false constant false iftthentelset conditional
0 constant zero
succt successor
predt predecessor
iszerot zero test error constant error
totherwiset error recovery
We treat true, false, if • then • else, 0, succ •, pred •, iszero •, error and • otherwise • as constructors.
The inversion lemma for terms says that, for all termss, either: (True) s=true; or
(False) s=false; or
(Conditional) s=ift1 thent2 elset3, for some terms t1,t2 and t3; or (Zero) s=0; or
(Successor) s=succt, for some termt; or (Predecessor) s=predt, for some termt; or (Zero Test) s=iszerot, for some termt; or (Error) s=error; or
(Error Recovery) s=t1otherwiset2, for some terms t1 and t2.
SupposeP is a predicate on terms. Theprinciple of structural induction on terms says that,
for all termst, P(t) follows from showing:
(True) P(true); (False) P(false);
(Conditional) for all terms t1, t2 and t3, if (†) P(t1), P(t2) and P(t3), then
P(ift1 thent2 elset3); (Zero) P(0);
(Successor) for all termst, if (†) P(t), then P(succt); (Predecessor) for all termst, if (†) P(t), then P(predt); (Zero Test) for all termst, if (†)P(t), then P(iszerot); (Error) P(error);
(Error Recovery) for all termst1andt2, if (†)P(t1) andP(t2), thenP(t1 otherwiset2). We refer to (†) as theinductive hypothesis.
Values and Answers
The set ofboolean values is defined by:
bv ::= boolean values: true true boolean value false false boolean value The set of numeric values is inductively defined by:
nv ::= numeric values:
0 zero numeric value succnv successor numeric value
(Zero) nv =0; or
(Successor) nv =succnv′, for some numeric value nv′.
Suppose P is a predicate on numeric values. The principle of structural induction on numeric values says that,
for all numeric valuesnv, P(nv) follows from showing:
(Zero) P(0);
(Successor) for all numeric valuesnv, if (†) P(nv), then P(succnv). We refer to (†) as theinductive hypothesis.
The set of values is defined by:
v ::= values: bv boolean value nv numeric value The set of answers is defined by:
a::= answers:
v value answer error error answer
It is easy to prove that every answer is a term. Evaluation Relation
Theevaluation relation t→t′ on terms is inductively defined by:
if true thent2 elset3 →t2 (E-IfTrue) if false thent2 elset3 →t3 (E-IfFalse) if nv thent2 elset3 →error (E-IfNum) if error thent2elset3→error (E-IfError)
t1 →t′
1
if t1 thent2elset3 →if t′
1thent2elset3
succbv →error (E-SuccBool) succ error→error (E-SuccError)
t1→t′
1
succt1→succt′
1
(E-Succ) predbv →error (E-PredBool) pred 0→error (E-PredZero) pred(succnv)→nv (E-PredSucc) pred error→error (E-PredError)
t1→t′
1
predt1→predt′
1
(E-Pred) iszerobv →error (E-IszeroBool) iszero 0→true (E-IszeroZero) iszero(succnv)→false (E-IszeroSucc) iszero error→error (E-IszeroError)
t1→t′ 1 iszerot1→iszerot′ 1 (E-Iszero) votherwiset→v (E-OtherwiseValue) error otherwiset→t (E-OtherwiseError)
t1 →t′
1 t1otherwiset2 →t′
1 otherwiset2
(E-Otherwise)
The inversion lemma for the evaluation relation says that, for all terms s1 and s2, if
s1 →s2, then either:
(IfTrue) s1 =if true thent2 elset3 and s2 =t2, for some termst2 and t3; or (IfFalse) s1=if false thent2 elset3 and s2 =t3, for some terms t2 and t3; or
(IfNum) s1=if nv thent2 elset3 and s2=error, for some numeric value nv and terms
t2 and t3; or
(IfError) s1 =if error thent2 elset3 and s2 =error, for some terms t2 and t3; or (If ) there are terms t1, t′
1, t2 and t3 such that s1 = if t1 then t2 else t3, s2 =
ift′
1 thent2 elset3 andt1 →t′1; or
(SuccError) s1=succ errorand s2 =error; or (Succ) there are terms t1 and t′
1 such that s1 =succt1,s2 =succt′1 and t1→t′1; or
(PredBool) s1=predbv ands2 =error, for some boolean valuebv; or (PredZero) s1 =pred 0and s2=error; or
(PredSucc) s1 =pred(succnv) and s2 =nv, for some numeric value nv; or (PredError) s1=pred errorand s2 =error; or
(Pred) there are terms t1 and t′
1 such that s1=predt1,s2 =predt′1 and t1 →t′1; or
(IszeroBool) s1=iszerobv ands2 =error, for some boolean valuebv; or (IszeroZero) s1 =iszero 0and s2 =true; or
(IszeroSucc) s1 =iszero(succnv) and s2=false, for some numeric valuenv; or (IszeroError) s1=iszero errorand s2 =error; or
(Iszero) there are termst1 andt′
1 such thats1 =iszerot1,s2 =iszerot′1 andt1 →t′1; or
(OtherwiseValue) s1 =votherwisetand s2=v, for some termt and valuev; or (OtherwiseError) s1 =error otherwiset ands2 =t, for some termt; or
(Otherwise) there are termst1andt′
1such thats1 =t1 otherwiset2,s2=t′1 otherwiset2
andt1 →t′
1.
SupposeP is a binary relation on terms. We sometimes write “P(t1, t2)” for “(t1, t2)∈
P”. The principle of induction on the evaluation relation says that, for all termst1 andt2, ift1 →t2, thenP(t1, t2), follows from showing:
(IfTrue) for all termst2 and t3,P(if true thent2 elset3, t2); (IfFalse) for all termst2 andt3,P(if false thent2elset3, t3);
(IfNum) for all numeric values nv and termst2 andt3,P(ifnv thent2elset3,error); (IfError) for all terms t2 and t3,P(if error thent2elset3,error);
(If ) for all terms t1, t′
1, t2 and t3, if t1 → t1′ and (†) P(t1, t′1), then P(ift1 thent2 elset3,ift′
(SuccBool) for all boolean valuesbv,P(succbv,error); (SuccError) P(succ error,error);
(Succ) for all termst1 and t′
1, ift1 →t′1 and (†) P(t1, t′1), then P(succt1,succt′1);
(PredBool) for all boolean valuesbv,P(predbv,error); (PredZero) P(pred 0,error);
(PredSucc) for all numeric valuesnv,P(pred(succnv),nv); (PredError) P(pred error,error);
(Pred) for all termst1 and t′
1, ift1→t′1 and (†) P(t1, t′1), then P(predt1,predt′1);
(IszeroBool) for all boolean valuesbv,P(iszerobv,error); (IszeroZero) P(iszero 0,true);
(IszeroSucc) for all numeric valuesnv,P(iszero(succnv),false); (IszeroError) P(iszero error,error);
(Iszero) for all termst1 and t′
1, ift1→t′1 and (†) P(t1, t′1), then P(iszerot1,iszerot′1);
(OtherwiseValue) for all termstand values v,P(v otherwiset, v); (OtherwiseError) for all terms t,P(error otherwiset, t);
(Otherwise) for all terms t1 and t′
1, if t1 → t′1 and (†) P(t1, t′1), then P(t1otherwiset2, t′
1 otherwiset2).
We refer to (†) as theinductive hypothesis.
Reflexive-Transitive Closure of Evaluation Relation
Thereflexive-transitive closure of the evaluation relation t→∗ t is inductively defined by:
t1 →t2
t1→∗t2 (RTCE-Eval)
t→∗ t (RTCE-Refl)
t1→∗t2 t2 →∗ t3
t1 →∗ t3 (RTCE-Trans)
The inversion lemma for the reflexive-transitive closure of the evaluation relation says that, for all termst1 and t2, ift1 →∗ t2, then either:
(Eval) t1→t2; or (Refl) t1 =t2; or
(Trans) there is a termt′ such that t1 →∗t′ and t′ →∗t2.
Suppose P is a binary relation on terms. The principle of induction on the reflexive-transitive closure of the evaluation relation says that,
for all terms t1 and t2, ift1→∗t2, then P(t1, t2),
follows from showing:
(Eval) for all termst1 and t2, ift1 →t2, thenP(t1, t2); (Refl) for all terms t,P(t, t);
(Trans) for all terms t1, t2 and t3, if t1 →∗ t2 and t2 →∗ t3, and (†) P(t1, t2) and
P(t2, t3), then P(t1, t3).
We refer to (†) as theinductive hypothesis. Complete Evaluation Relation
Thecomplete evaluation relation t⇒a between terms and answers is inductively defined by:
true⇒true (CE-True) false⇒false (CE-False)
0⇒0 (CE-Zero)
error⇒error (CE-Error)
t1⇒true t2 ⇒v
if t1thent2 elset3⇒v (CE-IfTrue) t1 ⇒false t3⇒v
if t1thent2 elset3⇒v (CE-IfFalse) t1⇒nv
if t1thent2 elset3⇒error (CE-IfNum)
t1 ⇒error
if t1thent2 elset3⇒error (CE-IfError)
t⇒bv
t⇒nv
succt⇒succnv (CE-SuccNum)
t⇒error
succt⇒error (CE-SuccError)
t⇒bv
predt⇒error (CE-PredBool)
t⇒0
predt⇒error (CE-PredZero)
t⇒succnv
predt⇒nv (CE-PredSucc)
t⇒error
predt⇒error (CE-PredError)
t⇒bv
iszerot⇒error (CE-IszeroBool)
t⇒0
iszerot⇒true (CE-IszeroZero)
t⇒succnv
iszerot⇒false (CE-IszeroSucc)
t⇒error
iszerot⇒error (CE-IszeroError)
t1 ⇒v
t1 otherwiset2 ⇒v (CE-OtherwiseValue) t1 ⇒error t2 ⇒v
t1otherwiset2 ⇒v (CE-OtherwiseError)
Theinversion lemma for the complete evaluation relation says that, for all termssand answersa, ifs⇒a, then either:
(True) s=true and a=true; or (False) s=false and a=false; or (Zero) s=0 anda=0; or
(Error) s=errorand a=error; or
(IfTrue) there are terms t1, t2 and t3 and a value v such that s =if t1 thent2 elset3,
a=v,t1 ⇒true andt2 ⇒v; or
(IfFalse) there are terms t1, t2 and t3 and a value v such that s=if t1 then t2 elset3,
a=v,t1 ⇒false and t3 ⇒v; or
(IfNum) there are terms t1, t2 and t3 and a numeric value nv such that s = ift1 thent2 elset3,a=errorand t1⇒nv; or
(IfError) there are terms t1,t2 and t3 such that s=if t1 thent2 elset3, a=error and
t1 ⇒error; or
(SuccBool) there is a termtand a boolean valuebv such thats=succt,a=errorand
t⇒bv; or
(SuccNum) there is a termtand a numeric valuenv such thats=succt,a=succnv andt⇒nv; or
(SuccError) there is a termtsuch that s=succt,a=error and t⇒error; or
(PredBool) there is a term t and a boolean value bv such that s = pred t, a = error andt⇒bv; or
(PredZero) there is a termt such thats=predt,a=errorand t⇒0; or
(PredSucc) there is a termtand a numeric valuenv such that s=predt,a=nv and
t⇒succnv; or
(PredError) there is a termtsuch that s=predt,a=errorand t⇒error; or
(IszeroBool) there is a term tand a boolean valuebv such thats=iszero t, a=error andt⇒bv; or
(IszeroZero) there is a termt such thats=iszerot,a=true andt⇒0; or
(IszeroSucc) there is a termt and a numeric valuenv such that s=iszerot, a=false andt⇒succnv; or
(IszeroError) there is a termtsuch that s=iszerot,a=errorand t⇒error; or (OtherwiseValue) there are termst1andt2and a valuevsuch thats=t1otherwiset2,
a=v and t1⇒v; or
(OtherwiseError) there are termst1andt2 and a valuevsuch thats=t1otherwiset2,
a=v,t1 ⇒errorand t2 ⇒v.
Suppose P is a predicate on a term and an answer. The principle of induction on the complete evaluation relation says that,
for all termstand answers a, ift⇒a, thenP(t, a), follows from showing:
(True) P(true,true); (False) P(false,false);
(Zero) P(0,0);
(Error) P(error,error);
(IfTrue) for all terms t1, t2 and t3 and values v, if t1 ⇒ true and t2 ⇒ v, and (†)
P(t1,true) and P(t2, v), thenP(ift1 thent2 elset3, v);
(IfFalse) for all terms t1, t2 and t3 and values v, if t1 ⇒ false and t3 ⇒ v, and (†)
P(t1,false) and P(t3, v), thenP(ift1 thent2 elset3, v);
(IfNum) for all termst1,t2 and t3 and numeric valuesnv, ift1⇒nv and (†) P(t1,nv), thenP(if t1 thent2elset3,error);
(IfError) for all terms t1, t2 and t3, if t1 ⇒ error and (†) P(t1,error),
P(ift1 thent2 elset3,error);
(SuccBool) for all terms t and boolean values bv, if t ⇒ bv and (†) P(t,bv), then
P(succt,error);
(SuccNum) for all terms t and numeric values nv, if t ⇒ nv and (†) P(t,nv), then
P(succt,succnv);
(SuccError) for all termst, ift⇒errorand (†) P(t,error), thenP(succt,error); (PredBool) for all termst, ift⇒bv and (†) P(t,bv), then P(predt,error); (PredZero) for all terms t, ift⇒0 and (†) P(t,0), then P(predt,error);
(PredSucc) for all termstand numeric valuesnv, if t⇒succnv and (†)P(t,succnv), thenP(predt,nv);
(PredError) for all termst, ift⇒errorand (†)P(t,error), thenP(predt,error); (IszeroBool) for all terms t and boolean values bv, if t ⇒ bv and (†) P(t,bv), then
P(iszerot,error);
(IszeroZero) for all terms t, ift⇒0 and (†) P(t,0), then P(iszerot,true);
(IszeroSucc) for all terms t and numeric values nv, if t ⇒ succ nv and P(t,succ nv), thenP(iszerot,false);
(IszeroError) for all termst, ift⇒errorand (†)P(t,error), thenP(iszerot,error); (OtherwiseValue) for all termst1 andt2 and valuesv, ift1 ⇒vand (†) P(t1, v), then
(OtherwiseError) for all terms t1 and t2 and valuesv, if t1 ⇒error and t2 ⇒ v, and (†) P(t1,error) and P(t2, v), then P(t1otherwiset2, v).
We refer to (†) as theinductive hypothesis. Exercise 1 (25 Points)
Prove that the evaluation relation (→) isdeterministic: for all termst1,t2 andt′
2, ift1 →t2
andt1 →t′
2, thent2 =t′2.
Exercise 2 (25 Points)
Prove that the complete evaluation relation (⇒) isdeterministic: for all termst, and answers
aand a′, ift⇒aand t⇒a′, thena=a′.
Exercise 3 (50 Points)
Define a relation; between terms and answers by: for all termstand answersa,t;aiff
t→∗ a.