10 KiB
N.B. The definition of multistep reduction I use is composed of a reflexive case (t →* t
) and a transitive case (t₁ → t₂ →* t₃
). I need the following two lemmas throughout.
Lemma (transitivity): If t₁ →* t₂ →* t₃
then t₁ →* t₃
.
Proof: By induction on t₁ →* t₂
. The reflexive case is trivial and the transitive case uses the induction hypothesis to combine the two multistep reductions.
Lemma (congruence): Multistep reduction is congruent in head positions, i.e. if t₁ →* t₁'
then if t₁ then t₂ else t₃ →* if t₁' then t₂ else t₃
, and similarly for succ
, pred
, and iszero
.
Proof: If the reduction is reflexive, then the proof is trivial. Otherwise, the reduction is of the form t₁ → t₁' →* t₁''
. By E-If
(likewise E-Succ
, E-Pred
, E-Iszero
), we have if t₁ then t₂ else t₃ → if t₁' then t₂ else t₃
. By the induction hypothesis, we have if t₁' then t₂ else t₃ →* if t₁'' then t₂ else t₃
(likewise forsucc
, pred
, iszero
). We have our goal by combining the two by transitivity.
Exercise 3.5.16: The two treatments of run-time errors agree if the following hold:
- If
t
evaluates to a stuck state (using the rules withoutE-*-Wrong
), then it evaluates towrong
(using the rules withE-*-Wrong
). More precisely, ift →* t' ↛
andt ≠ v
, thent →* wrong
. - If
t
evaluates towrong
and does not containwrong
as a subterm, thent
evaluates to a stuck state. This is the converse of the first statement, except we exclude cases wheret
was wrong to begin with.
Lemma 0: If t ↛
(using the rules without E-*-Wrong
) and t ≠ v
then t → wrong
(using the rules with E-*-Wrong
).
Proof: By induction on the shape of t
. The cases where t
is true
, false
, 0
, or succ nv
are impossible.
- Case
t = if t₁ then t₂ else t₃
: The term does not step byE-IfTrue
,E-IfFalse
, norE-If
, sot₁
is nottrue
norfalse
, and does not step. Ift₁
is a value, then it is either a numeric valuenv
; otherwise, if it is not a value, then by the induction hypothesis, it steps towrong
. In both cases,t₁
is abadbool
, so the whole expression steps towrong
byE-If-Wrong
. - Case
t = succ t₁
wheret₁ ≠ nv
: The term does not step byE-Succ
, sot₁
does not step. Ift₁
is a value, then it must betrue
orfalse
; otherwise, if it is not a value, then by the induction hypothesis, it steps towrong
. In both cases,t₁
is abadnat
, so the whole expression steps towrong
byE-Succ-Wrong
. - Case
t = pred t₁
: The term does not step byE-PredZero
,E-PredSucc
, norE-Pred
, sot₁ ≠ nv
and does not step. Then the argument for the previous case follows, using insteadE-Pred-Wrong
. - Case
t = iszero t₁
: The argument for the previous case follows, noting that the term does not step byE-IszeroZero
,E-IszeroSucc
, norE-Iszero
, and usingE-Iszero-Wrong
to step.
Lemma 1: If t → t'
(using the rules with E-*-Wrong
) and t'
contains a wrong
subterm, then either t
contains a wrong
subterm, or t ↛
(using the rules without E-*-Wrong
) and t ≠ v
.
Proof: By induction on t → t'
.
- Cases
E-If-True
,E-If-False
: The reduction isif true then t₁ else t₂ → t₁
orif false then t₁ else t₂ → t₂
. If the RHS contains awrong
subterm, then evidently the LHS does as well. - Cases
E-If
,E-Pred
,E-Iszero
: The three cases are similar; I useE-Iszero
as representative. The reduction isiszero t₁ → iszero t₂
, wheret₁ → t₂
, andt₂
must contain awrong
subterm. By the induction hypothesis, eithert₁
contains awrong
subterm, in which case so doesiszero t₁
, ort₁ ↛
andt₁ ≠ v
. In this latter case, we haveiszero t₁ ↛
, since none ofE-Iszero-Zero
,E-Iszero-Succ
, andE-Iszero
apply, andiszero t₁
is obviously not a value. - Case
E-Succ
: The reduction issucc t₁ → succ t₂
, wheret₁ → t₂
, andt₂
must contain awrong
subterm. By the induction hypothesis, eithert₁
contains a wrong subterm, in which case so doessucc t₁
, ort₁ ↛
andt₁ ≠ v
. In this latter case, we havesucc t₁ ↛
sinceE-Succ
does not apply, and we know thatsucc t₁
is not a value sincet₁
is not a value.
Lemma 2: If t →* wrong
(using the rules with E-*-Wrong
), then either t
contains a wrong
subterm, or t →* t' ↛
(using the rules without E-*-Wrong
) and t' ≠ v
.
Proof: By induction on t →* wrong
. The reflexive case wrong →* wrong
is trivial. In the transitive case t → t' →* wrong
, by the induction hypothesis, either t'
contains a wrong
subterm, or t' →* t'' ↛
and t'' ≠ v
. In the former case, we use Lemma 1 on the reduction t → t'
. In the latter case, we have t → t' →* t'' ↛
and t'' ≠ v
as required.
Proof of 3.5.16 (Condition 1): Suppose t →* t' ↛
and t' ≠ v
. Then t' → wrong
by Lemma 0, and t →* wrong
by transitivity.
Proof of 3.5.16 (Condition 2): Suppose t →* wrong
and t
does not contain a wrong
subterm. Then by Lemma 2, it must be that t →* t' ↛
and t' ≠ v
.
Exercise 3.5.17: t →* v
iff t ⇓ v
.
Lemma 3: If t₁ → t₂
and t₂ ⇓ v
, then t₁ ⇓ v
.
Proof: By induction on t₁ → t₂
.
- Case
E-IfTrue
: The reduction isif true then t₄ else t₅ → t₂
, witht₂ ⇓ v
. ByB-Value
,true ⇓ true
. Then byB-IfTrue
, we constructif true then t₄ else t₅ ⇓ v
. - Case
E-IfFalse
: Similar as above, but usingB-IfFalse
. - Case
E-If
: The reduction isif t₃ then t₄ else t₅ → if t₃' then t₄ else t₅
, wheret₃ → t₃'
, withif t₃' then t₄ else t₅ ⇓ v
. By inversion on the latter, the only possible cases areB-IfTrue
andB-IfFalse
.- Subcase
B-IfTrue
: We havet₃' ⇓ true
andt₄ ⇓ v
. By the induction hypothesis, we havet₃ ⇓ true
. Then byB-IfTrue
, we constructif t₃ then t₄ else t₄ ⇓ true
. - Subcase
B-IfFalse
: Similar as above, but the induction hypothesis givest₃ ⇓ false
, and we useB-IfFalse
instead.
- Subcase
- Case
E-Succ
: The reduction issucc t₁ → succ t₂
, wheret₁ → t₂
, withsucc t₂ ⇓ v
. By inversion on the latter, the only possible case isB-Succ
, witht₂ ⇓ v
. By the induction hypothesis, we havet₁ ⇓ v
. Then byB-Succ
, we constructsucc t₁ ⇓ v
. - Case
E-PredZero
: The reduction ispred 0 → 0
, with0 ⇓ v
. By inversion on the latter, the only possible case isB-Value
, withv = 0
. The goalpred 0 ⇓ 0
holds directly byB-PredZero
. - Case
E-PredSucc
: The reduction ispred (succ nv) → nv
, withnv ⇓ v
. By inversion on the latter, the only possible case isB-Value
, withv = nv
. The goalpred (succ nv) ⇓ nv
holds directly byB-PredSucc
, whose premisesucc nv ⇓ succ nv
holds byB-succ
, whose premisenv ⇓ nv
holds byB-Value
. - Case
E-Pred
: The reduction ispred t₁ → pred t₂
, wheret₁ → t₂
, withpred t₂ ⇓ v
. By inversion on the latter, the only possible cases areB-PredZero
andB-PredSucc
.- Subcase
B-PredZero
: We havepred t₂ ⇓ 0
, wheret₂ ⇓ 0
. By the induction hypothesis, we havet₁ ⇓ 0
. Then byB-PredZero
, we constructpred t₁ ⇓ 0
. - Subcase
B-PredSucc
: We havepred t₂ ⇓ nv
, wheret₂ ⇓ succ nv
. By the induction hypothesis, we havet₁ ⇓ succ nv
. Then byB-PredSucc
, we constructpred t₁ ⇓ nv
.
- Subcase
- Case
E-IszeroZero
: The reduction isiszero 0 → true
, withtrue ⇓ v
. By inversion on the latter, the only possible case isB-Value
, withv = true
. The goaliszero 0 ⇓ true
holds directly byB-IszeroZero
, whose premise0 ⇓ 0
holds byB-Value
. - Case
E-IszeroSucc
: The reduction isiszero (succ nv) → false
, withfalse ⇓ v
. By inversion on the latter, the only possible case isB-Value
, withv = false
. The goaliszero (succ nv) ⇓ false
holds directly byB-IszeroSucc
, whose premisesucc nv ⇓ succ nv
holds byB-Succ
, whose premisenv ⇓ nv
holds byB-Value
. - Case
E-Iszero
: The reduction isiszero t₁ → iszero t₂
, wheret₁ → t₂
, withiszero t₂ ⇓ v
. By inversion on the latter, the only possible cases areB-IszeroZero
andB-IszeroSucc
.- Subcase
B-IszeroZero
: We haveiszero t₂ ⇓ true
, wheret₂ ⇓ 0
. By the induction hypothesis, we havet₁ ⇓ 0
. Then byB-IszeroZero
, we constructiszero t₁ ⇓ true
. - Subcase
B-IszeroSucc
: We haveiszero t₂ ⇓ false
, wheret₂ ⇓ succ nv
. By the induction hypothesis, we havet₁ ⇓ succ nv
. Then byB-IszeroSucc
, we constructiszero t₁ ⇓ false
.
- Subcase
Proof of 3.5.17 (LtR direction): By induction on t →* v
. In the reflexive case, we have v → v
, and v ⇓ v
holds by B-Value
. In the transitive case, we have t₁ → t₂ →* v
. By the induction hypothesis, we have t₂ ⇓ v
. Then by Lemma 3, we have t₁ ⇓ v
.
Proof of 3.5.17 (RtL direction): By induction on t ⇓ v
. I freely use the transitivity and congruence lemmata to combine multistep reductions.
- Case
B-Value
: By reflexivity. - Case
B-IfTrue
: The evaluation isif t₁ then t₂ else t₃ ⇓ v
, wheret₁ ⇓ true
andt₂ ⇓ v
. By the induction hypothesis, we havet₁ →* true
andt₂ →* v
. Then we haveif t₁ then t₂ else →* if true then t₂ else t₃ →* t₂ →* v
, the middle step byE-IfTrue
. - Case
B-IfFalse
: Similar as above, but usingE-IfFalse
. - Case
B-Succ
: The evaluation issucc t ⇓ succ v
, wheret ⇓ v
. By the induction hypothesis, we havet →* v
. Then we havesucc t →* succ v
by congruence. - Case
B-PredZero
: The evaluation ispred t ⇓ 0
, wheret ⇓ 0
. By the induction hypothesis, we havet →* 0
. Then we havepred t →* pred 0 →* 0
, the last step byE-PredZero
. - Case
B-PredSucc
: The evaluation ispred t ⇓ nv
, wheret ⇓ succ nv
. By the induction hypothesis, we havet →* succ nv
. Then we havepred t →* pred (succ nv) →* nv
, the last step byE-PredSucc
. - Case
B-IszeroZero
: The evaluation isiszero t ⇓ true
, wheret ⇓ 0
. By the induction hypothesis, we havet →* 0
. Then we haveiszero t →* iszero 0 →* true
, the last step byE-IszeroZero
. - Case
B-IszeroSucc
: The evaluation isiszero t ⇓ false
, wheret ⇓ succ nv
. By the induction hypothesis, we havet →* succ nv
. Then we haveiszero t →* iszero (succ nv) →* false
, the last step byE-IszeroSucc
.