review and more details
play

Review (and more details) Week 3 November 8 November 1, 2006 - - PowerPoint PPT Presentation

Type Systems Winter Semester 2006 Review (and more details) Week 3 November 8 November 1, 2006 - version 1.0 Simple Arithmetic Expressions Inference Rule Notation The set T of terms is defined by the following abstract grammar: More


  1. Type Systems Winter Semester 2006 Review (and more details) Week 3 November 8 November 1, 2006 - version 1.0 Simple Arithmetic Expressions Inference Rule Notation The set T of terms is defined by the following abstract grammar: More explicitly: The set T is the smallest set closed under the following rules. t ::= terms constant true true true ∈ T false ∈ T 0 ∈ T constant false false conditional if t then t else t t 1 ∈ T t 1 ∈ T t 1 ∈ T constant zero 0 succ t 1 ∈ T pred t 1 ∈ T iszero t 1 ∈ T successor succ t predecessor t 1 ∈ T t 2 ∈ T t 3 ∈ T pred t zero test iszero t if t 1 then t 2 else t 3 ∈ T

  2. Generating Functions Let’s write these generating functions explicitly. Each of these rules can be thought of as a generating function F 1 ( U ) = { true } that, given some elements from T , generates some other element F 2 ( U ) = { false } of T . Saying that T is closed under these rules means that T F 3 ( U ) = { 0 } cannot be made any bigger using these generating functions — it F 4 ( U ) = { succ t 1 | t 1 ∈ U } already contains everything “justified by its members.” F 5 ( U ) = { pred t 1 | t 1 ∈ U } F 6 ( U ) = { iszero t 1 | t 1 ∈ U } true ∈ T false ∈ T 0 ∈ T F 7 ( U ) = { if t 1 then t 2 else t 3 | t 1 , t 2 , t 3 ∈ U } t 1 ∈ T t 1 ∈ T t 1 ∈ T succ t 1 ∈ T pred t 1 ∈ T iszero t 1 ∈ T Each one takes a set of terms U as input and produces a set of “terms justified by U ” as output. t 1 ∈ T t 2 ∈ T t 3 ∈ T if t 1 then t 2 else t 3 ∈ T If we now define a generating function for the whole set of Our alternate definition of the set of terms can also be stated inference rules (by combining the generating functions for the using the generating function F : individual rules), S 0 = ∅ F 1 ( U ) ∪ F 2 ( U ) ∪ F 3 ( U ) ∪ F 4 ( U ) ∪ F 5 ( U ) ∪ F 6 ( U ) ∪ F 7 ( U ) S i +1 = F ( S i ) F ( U ) = then we can restate the previous definition of the set of terms T S = � i S i like this: Compare this definition of S with the one we saw last time: Definition: S 0 = ∅ ◮ A set U is said to be “closed under F ” (or “F-closed”) if S i +1 = { true , false , 0 } F ( U ) ⊆ U . ∪ { succ t 1 , pred t 1 , iszero t 1 | t 1 ∈ S i } ◮ The set of terms T is the smallest F -closed set. ∪ { if t 1 then t 2 else t 3 | t 1 , t 2 , t 3 ∈ S i } (I.e., if O is another set such that F ( O ) ⊆ O , then T ⊆ O .) � S = i S i We have “pulled out” F and given it a name.

  3. Note that our two definitions of terms characterize the same set from different directions: ◮ “from above,” as the intersection of all F -closed sets; ◮ “from below,” as the limit (union) of a series of sets that start from ∅ and get “closer and closer to being F -closed.” Proposition 3.2.6 in the book shows that these two definitions Warning: Hard hats on for the next slide! actually define the same set. Structural Induction Structural Induction The principle of structural induction on terms can also be re-stated The principle of structural induction on terms can also be re-stated using generating functions: using generating functions: Suppose T is the smallest F-closed set. Suppose T is the smallest F-closed set. If, for each set U, If, for each set U, from the assumption “P ( u ) holds for every u ∈ U” from the assumption “P ( u ) holds for every u ∈ U” we can show “P ( v ) holds for any v ∈ F ( U ) ,” we can show “P ( v ) holds for any v ∈ F ( U ) ,” then P ( t ) holds for all t ∈ T. then P ( t ) holds for all t ∈ T. Why?

  4. Structural Induction Structural Induction Why? Because: Compare this with the structural induction principle for terms from last lecture: ◮ We assumed that T was the smallest F -closed set, i.e., that T ⊆ O for any other F -closed set O . If, for each term s , ◮ But showing given P ( r ) for all immediate subterms r of s for each set U, we can show P ( s ) , given P ( u ) for all u ∈ U then P ( t ) holds for all t . we can show P ( v ) for all v ∈ F ( U ) amounts to showing that “the set of all terms satisfying P ” (call it O ) is itself an F -closed set. ◮ Since T ⊆ O , every element of T satisfies P . Recall, from the definition of S , it is clear that, if a term t is in S i , then all of its immediate subterms must be in S i − 1 , i.e., they must have strictly smaller depths. Therefore: If, for each term s , given P ( r ) for all immediate subterms r of s we can show P ( s ) , Operational Semantics then P ( t ) holds for all t . Slightly more explicit proof : ◮ Assume that for each term s , given P ( r ) for all immediate subterms of s , we can show P ( s ). ◮ Then show, by induction on i , that P ( t ) holds for all terms t with depth i . ◮ Therefore, P ( t ) holds for all t .

  5. Abstract Machines Operational semantics for Booleans An abstract machine consists of: Syntax of terms and values ◮ a set of states t ::= terms ◮ a transition relation on states, written − true constant true → false constant false if t then t else t conditional For the simple languages we are considering at the moment, the term being evaluated is the whole state of the abstract machine. v ::= values true true value false false value Evaluation Relation on Booleans Digression → t ′ is the smallest relation closed The evaluation relation t − Suppose we wanted to change our evaluation strategy so that the under the following rules: then and else branches of an if get evaluated (in that order) before the guard. How would we need to change the rules? if true then t 2 else t 3 − → t 2 ( E-IfTrue ) if false then t 2 else t 3 − → t 3 ( E-IfFalse ) t 1 − → t ′ 1 ( E-If ) if t 1 then t 2 else t 3 − → if t ′ 1 then t 2 else t 3

  6. Digression Digression Suppose we wanted to change our evaluation strategy so that the Suppose we wanted to change our evaluation strategy so that the then and else branches of an if get evaluated (in that order) then and else branches of an if get evaluated (in that order) before the guard. How would we need to change the rules? before the guard. How would we need to change the rules? Suppose, moreover, that if the evaluation of the then and else Suppose, moreover, that if the evaluation of the then and else branches leads to the same value, we want to immediately produce branches leads to the same value, we want to immediately produce that value (“short-circuiting” the evaluation of the guard). How that value (“short-circuiting” the evaluation of the guard). How would we need to change the rules? would we need to change the rules? Of the rules we just invented, which are computation rules and which are congruence rules? Evaluation, more explicitly Even more explicitly... − → is the smallest two-place relation closed under the following What is the generating function corresponding to these rules? rules: (exercise) (( if true then t 2 else t 3 ) , t 2 ) ∈ − → (( if false then t 2 else t 3 ) , t 3 ) ∈ − → ( t 1 , t ′ 1 ) ∈ − → (( if t 1 then t 2 else t 3 ) , ( if t ′ 1 then t 2 else t 3 )) ∈ − →

  7. Derivations We can record the “justification” for a particular pair of terms that are in the evaluation relation in the form of a tree. Reasoning about Evaluation (on the board) Terminology: ◮ These trees are called derivation trees (or just derivations ). ◮ The final statement in a derivation is its conclusion . ◮ We say that the derivation is a witness for its conclusion (or a proof of its conclusion) — it records all the reasoning steps that justify the conclusion. Observation Induction on Derivations Lemma: Suppose we are given a derivation tree D witnessing the We can now write proofs about evaluation “by induction on pair ( t , t ′ ) in the evaluation relation. Then either derivation trees.” 1. the final rule used in D is E-IfTrue and we have Given an arbitrary derivation D with conclusion t − → t ′ , we t = if true then t 2 else t 3 and t ′ = t 2 , for some t 2 assume the desired result for its immediate sub-derivation (if any) and t 3 , or and proceed by a case analysis (using the previous lemma) of the 2. the final rule used in D is E-IfFalse and we have final evaluation rule used in constructing the derivation tree. t = if false then t 2 else t 3 and t ′ = t 3 , for some t 2 and t 3 , or 3. the final rule used in D is E-If and we have t = if t 1 then t 2 else t 3 and t ′ = if t ′ 1 then t 2 else t 3 , for some t 1 , t ′ 1 , t 2 , and t 3 ; moreover, the immediate subderivation of D witnesses E.g.... ( t 1 , t ′ 1 ) ∈− → .

Recommend


More recommend