Evaluation Contexts ------------------- Motivation: A concise notation for communicating operational rules. The language with simplified operational semantics: t ::= bool | t1 -> t2 e ::= x | v | if e1 then e2 else e3 | e1 e2 v ::= true | false | \x:t.e E ::= [] | E e | v E | if E then e1 else e2 E is an evaluation context where E[e] means the context where [] is replaced with e. Eval rules: --------------------- (\x:t.e) v -> e[v/x] ----------------------------- ------------------------------ if true then e1 else e2 -> e1 if false then e1 else e2 -> e2 e -> e' -------------- (E-context) E[e] -> E[e'] The above definition of E eliminates the need to include a number of "search rules" from the original semantics and hence makes the semantics more compact. For example, E e corresponds to e1 -> e1' --------------- e1 e2 -> e1' e2 v E corresponds to e2 -> e2' --------------- v e2 -> v e2' if E then e2 else e3 corresponds to e1 -> e1' ------------------------ if e1 then e2 else e3 -> if e1' then e2 else e3 Suppose we have an expression: if (\x:bool.x) ((\x:bool.x) true) then false else true Let E1 = if (\x:bool.x) [] then false else true and e1 = ((\x:bool.x) true) We have according to (E-context): e1 -> true ---------------- E[e1] -> E[true] Let E2 = (\.x:bool.x) [], e2 = (\x:bool.x) true Let E3 = if (\x:bool.x) [] then false else true e2 -> true -------------------- E3[e2] -> E3[true] We define judgement e ->(toplevel) e', then e -> e' ------------------------ E[e] ->(toplevel) E[e'] Preservation Lemma: ------------------- If |- e : t and e -> e', then |- e' : t Proof: By induction on the derivation of e -> e' Case: e -> e' ----------------- E[e] -> E[e'] Given that |- E[e] : t, must prove that |- E[e'] : t How do we do that? We some additional lemmas. Typing Evaluation Contexts -------------------------- We define a typing rule for eval context: G, x : t1 |- E[x] : t2 ----------------------- (T-context) G |- E : t1 ==> t2 Lemma 1. If |- E[e] : t2 then there exists a type t1, such that x: t1 |- E[x] : t2 and |- e : t1 Proof: By induction on the structure of E Case: E = [] |- [][e] : t2 (given) same as: |- e : t2 Let t1 = t2, must prove |- e : t1 True since t1 = t2. x: t1 |- [][x] : t2 same as x : t1 |- x : t1 (by variable rule and t1 = t2) Case: E = E e |- (E1 e1)[e] : t2 Same as |- (E1[e]) e1 : t2 (1) |- (E1[e]) : t_a -> t2 (by inversion of typing rule) (2) |- e1 : t_a (by inversion of typing rule) (3) x: t1 |- e1 : t_a (by weakening form lemma) (4) x: t1 |- E1[x] : t_a -> t2 (by I.H.) and |- e : t1 (by I.H.) x: t1 | (E1[x]) e1 : t2 (by (3) and (4) and typing rule for application) ---------- Corollary 1. If |- E[e] : t2 then there exists a type t1, such that |- E: t1 ==> t2 and |- e : t1 Proof: follows immediately from Lemma 1 and T-context rule.