• No se han encontrado resultados

Antecedentes y sentencias emitidas en contra de las normas que limitan el

2. OBJETIVOS

4.2 Antecedentes y sentencias emitidas en contra de las normas que limitan el

The proofs are entirely standard, culminating in a syntactic type soundness proof (Theorem 4.2.7) by progress and preservation (Lemmas 4.2.3 and 4.2.6).

4.2.1 Lemma [Determinism]: If e −→ce1 and e −→ce2 then e1 =e2.

Proof: By induction on e −→c e1, observing that in each case the same rule must

have applied to find e −→ce2.

4.2.2 Lemma [Canonical forms]: If Γ`v :T, then:

• T =Dynimplies that v =v0B! orv =v0Fun! for somev0.

• T =B implies thatv =kId.

• T =T1→T2 implies thatv =λx:T1. eId orv =v0hT0

1→T20⇒T1→T2i for some v

0.

• T ={x:T0 |e} implies that v =v0{x:T|e}? for some v0.

Proof: By case analysis on the typing derivation.

4.2.3 Lemma [Progress]: If ∅ ` e : T then there exists an e0 such that e −→c e0

ore is a result.

Proof: By induction on the typing derivation.

(T PreVal) uId is a result.

(T TagB) vB! is a result.

(T TagFun) vFun! is a result.

(T TagRefine) v{x:T|e}? is a result.

(T Wrap) vhT11→T12⇒T21→T22i is a result.

(T Var) Contradictory—variables aren’t well typed in the empty context.

(T Fail) fail is a result.

(T Cast) By the IH on ∅ `e :T1, eithere −→ce0 ore is a result. In the former

case, we step by G CastInner. If e is a result, then it is either failor a value v. In

the former case, we step by G CastRaise. Otherwise, we go by cases onT1 andT2.

(T1 =Dyn) By cases on T2.

(T2 =B) By canonical forms (Lemma 4.2.2),v is eitherv0B0!orv0Fun!. IfB =B0, we step by G CastBB. If not, we step by G CastBFailB orG CastBFailFun.

(T2 =T21→T22) By canonical forms (Lemma 4.2.2), v =v0B0! or v =v0Fun!. We step by G CastFunFailB orG CastFunFun.

(T2 ={x:T |e}) If T = Dyn, we step by G CastCheck. If T = B (the only

other option), we step by G CastPreCheck.

(T1 =B) By cases onT2.

(T2 =Dyn) We step by G CastB.

(T2 =B0) By inversion of `B kB0, we have B =B0. We step by G CastId.

(T2 =T21→T22) Contradictory, since it is not the case that `B kT21→T22.

(T2 ={x:T |e}) If T =B0 (and so B0 = B), we step by G CastCheck. Oth-

erwise we step by G CastPreCheck. (T1 =T11→T12) By cases on T2.

(T2 =Dyn) We step by G CastFun orG CastFunDyn.

(T2 =B) Contradictory, since it is not the case that `T21→T22 kB.

(T2 =T21→T22) We step by G CastFunWrap.

(T2 ={x:T |e}) It must be that T = Dyn, since it is not the case that `

T21→T22 kB. We step by G CastPreCheck.

(T1 ={x:T |e}) By cases onT2.

(T2 =Dyn) We step by G CastPredPred.

(T2 =B) We step byG CastPredPred.

(T2 =T21→T22) We step by G CastPredPred.

(T2 ={x:T0 |e0}) We step by G CastPredPred orG CastId.

(T App) We have ∅ `e1 e2 : T2. By the IH on∅ ` e1 : T1→T2, either e1 steps,

or it is a result. In the former case, we go by G AppL. In the latter, e1 is either fail

(and we step by G AppRaiseL) or e1 is a value.

Similarly, by the IH on ∅ ` e2 : T1, either e2 steps or is a result. We can

apply G AppR or G AppRaiseR (using that e1 is a value), unless e2 is some

value v2. In that case, we use canonical forms to see that e1 is either λx:T1. e1Id0

orv1hT10→T20⇒T1→T2i. We step by G Betaand G Fun, respectively.

(T Op) By induction on n, applying the IH to step by either G OpInner or

G OpFail. If all of the arguments are values, we step by G Op.

(T CheckCast) By the IH, we can step the active check by G CastInner or G CastRaise. If it’s a value, we have ∅ ` v2 :Bool, so v2 is either trueId or falseId.

We step by G CheckOK and G CheckFail, respectively.

4.2.4 Lemma [Regularity]: • If Γ`e :T then `Γ and `T.

• If Γ`u :T then ` Γ and `T.

Proof: By mutual induction on the derivations.

(T Var) ` Γ by assumption, which gives us `T.

(T Const) ` Γ By assumption, and we assume that `ty(k).

(T Abs) We have ` T1 and Γ,x:T1 ` e12 : T2 by assumption. By the IH,

` Γ,x:T1 and `T2. By inversion, ` Γ. We have`T1→T2 by WF Fun.

(T PreVal) By the IH.

(T TagB) By the IH and WF Dyn.

(T TagFun) By the IH and WF Dyn.

(T TagRefine) By the IH and assumption.

(T Wrap) By the IH and assumption.

(T Cast) By the IH and assumption. (T Fail) By assumption.

(T App) By the IH.

(T Op) By the IH and the assumption that operators have well formed types.

(T CheckCast) By assumption.

4.2.5 Lemma [Substitution]: If ∅ `v :T then:

• If Γ1,x:T,Γ2 `e :T0 then Γ1,Γ2 `e[v/x] :T0.

• If Γ1,x:T,Γ2 `u :T0 then Γ1,Γ2 `u[v/x] :T0.

Proof: By mutual induction on the typing derivations for terms and pre-values,

leaving Γ2 general.

(T Const) Immediate by T Const.

(T Abs) By T Abs, using the IH on Γ1,x:T,Γ2,y:T1 `e :T2.

(T Var) If x is the variable in question, then by weakening. If not, then by

(T PreVal) By the IH and T PreVal.

(T TagB) By the IH and T TagB.

(T TagFun) By the IH and T TagFun.

(T TagRefine) Immediate by T TagRefine, since the terms themselves are

actually closed.

(T Wrap) By the IH and T Wrap.

(T Cast) By the IH and T Cast.

(T Fail) Immediate by T Fail. (T App) By the IH and T App.

(T Op) By the IH andT Op.

(T CheckCast) Immedate by T CheckCast, since the terms themselves are

actually closed.

4.2.6 Lemma [Preservation]: If ∅ `e :T and e −→ce0, then∅ `e0 :T.

Proof: By induction on the evaluation derivation.

(G Beta) By inversion, x:T1 ` e1 : T2 and ∅ ` v2 : T1. By substitution

(Lemma 4.2.5).

(G Fun) By inversion of T Wrap, we know that ` T11→T12 k T21→T22. By T Cast, T App, and T Cast; we find the similarities necessary for the T Cast

rules by inversion, since only P Fun could have applied. (G Op) By assumption.

(G CastId) Immediate.

(G CastFunFailB) We have `T1→T2 by inversion; we are done byT Fail.

(G CastFunFun) We have ` DynkTi by P DynL; by P Fun, T Cast, and

assumption.

(G CastB) By assumption and T TagB.

(G CastFun) By assumption and T TagFun.

(G CastBFailB) We have `B immediately; by T Fail.

(G CastBFailFun) We have `B immediately; by T Fail.

(G CastFunDyn) We use P DynR, P Fun in two applications of T Cast.

(G CastFunWrap) By T Wrap.

(G CastPredPred) By assumption and T Cast, using ` T1 k T2 from the

inversion of ` {x:T1 |e} kT2.

(G CastPreCheck) By assumption andT Cast. We use `T1 kT2 from the

inversion of `T1 k {x:T2 |e} in the first case andP Id (it can’t be a function type!)

with P RefineR in the second.

(G CastCheck) By T CheckCast, using e[v/x]−→∗c e[v/x].

(G CheckOK) By T TagRefine, usinge[v/x]−→∗c trueId.

(G CheckFail) By T Fail, using the assumption that ` {x:T |e}.

(G AppL) By T App and the IH.

(G AppR) By T App and the IH.

(G OpInner) By T Op and the IH.

(G CastInner) By T Cast and the IH.

(G CheckInner) ByT CheckCastand the IH, extendinge[v/x]−→∗c e2 −→c e20.

(G AppRaiseL) By regularity (Lemma 4.2.4) and T Fail.

(G AppRaiseR) By regularity (Lemma 4.2.4) and T Fail.

(G OpRaise) By regularity (Lemma 4.2.4) andT Fail. (G CastRaise) By inversion, `T2; then, by T Fail.

(G CheckRaise) By inversion,` {x:T |e}; then, by T Fail.

4.2.7 Theorem [Type soundness]: If ∅ ` e : T, then either e −→∗ r such that

∅ `r :T ore diverges.

Proof: Using progress (Lemma 4.2.3) and preservation (Lemma 4.2.6). Unsurpris-

Types and base types

T ::= B | T1→T2 | {x:B|e} | Dyn | {x:Dyn|e}

B ::= Bool | Int | . . .

Coercions, primitive coercions, and type tags

c ::= d1;. . .;dn

d ::= D! | D? | c17→c2 | Fail

D ::= B | Fun | {x:B|e} | {x:Dyn|e}

Terms, results, values, and pre-values

e ::= x | r | op(e1, . . . ,en) | e1 e2 | hcie | h{x:T |e1},e2,vi r ::= v | fail v ::= uId | vB! | vFun! | v{x:T|e}? | vc17→c2 u ::= k | λx:T.e Typing contexts Γ ::= ∅ | Γ,x:T

Figure 4.8: Naive syntax

Documento similar