✬ ✩ ✬ ✩ CIS 500 Software Foundations Midterm Exam Fall 2005 19 October, 2005 ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 1 CIS 500, 19 October, 2005 2 ✬ ✩ ✬ ✩ Midterm Exam � Exam solutions on web page. � Look at your exam in Cheryl Hickey’s office. Types � Submit regrade request (in writing) before October 26. � You can pick up your exam from Cheryl after October 26. ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 3 CIS 500, 19 October, 2005 4
✬ ✩ ✬ ✩ Type Systems Approaches to Typing � currently, active and successful topic in PL research � A strongly typed language prevents programs from accessing private data, corrupting memory, crashing the machine, etc. � “light-weight” formal methods � A weakly typed language does not. � “enabling technology” for all sorts of other things, e.g. language-based security � A statically typed language performs type-consistency checks at when programs are first entered. � the “skeleton” around which modern programming languages are often designed � A dynamically typed language delays these checks until programs are executed. Weak Strong Dynamic Lisp, Scheme, Perl, Python, Smalltalk Static C, C++ ML, ADA, Java ⋆ ⋆ Strictly speaking, Java should be called “mostly static” ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 5 CIS 500, 19 October, 2005 6 ✬ ✩ ✬ ✩ Plan Outline � For today, we’ll go back to the simple language of arithmetic and boolean 1. begin with a set of terms, a set of values, and an evaluation relation expressions and show how to give it a (very simple) type system 2. define a set of types classifying values according to their “shapes” � Next week, we’ll develop a simple type system for the lambda-calculus, 3. define a typing relation t : T that classifies terms according to the shape following TAPL Ch.9. of the values that result from evaluating them � We’ll spend a good part of the rest of the semester adding features to this 4. check that the typing relation is sound in the sense that, if t : T , then type system evaluation of t will not get stuck ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 7 CIS 500, 19 October, 2005 8
✬ ✩ ✬ ✩ Arithmetic Expressions – Syntax Evaluation Rules t ::= terms true constant true ( E-IfTrue ) if true then t 2 else t 3 − → t 2 false constant false if t then t else t conditional ( E-IfFalse ) if false then t 2 else t 3 − → t 3 0 constant zero succ t successor → t ′ t 1 − pred t predecessor 1 ( E-If ) iszero t zero test if t 1 then t 2 else t 3 − → if t ′ 1 then t 2 else t 3 ::= v values true true value false false value nv numeric value nv ::= numeric values 0 zero value succ nv successor value ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 9 CIS 500, 19 October, 2005 10 ✬ ✩ ✬ ✩ Types t 1 − → t ′ 1 ( E-Succ ) → succ t ′ succ t 1 − 1 In this language, values have two possible “shapes”: they are either booleans or numbers. ( E-PredZero ) pred 0 − → 0 ::= T types ( E-PredSucc ) pred (succ nv 1 ) − → nv 1 type of booleans Bool t 1 − → t ′ Nat type of numbers 1 ( E-Pred ) → pred t ′ pred t 1 − 1 ( E-IszeroZero ) iszero 0 − → true ( E-IszeroSucc ) iszero (succ nv 1 ) − → false t 1 − → t ′ 1 ( E-IsZero ) → iszero t ′ iszero t 1 − 1 ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 11 CIS 500, 19 October, 2005 12
✬ ✩ ✬ ✩ Typing Rules Typing Rules ( T-True ) ( T-True ) true : Bool true : Bool ( T-False ) ( T-False ) false : Bool false : Bool t 1 : Bool t 2 : T t 3 : T ( T-If ) if t 1 then t 2 else t 3 : T ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 13 CIS 500, 19 October, 2005 13-a ✬ ✩ ✬ ✩ Typing Rules Typing Rules ( T-Zero ) ( T-Zero ) 0 : Nat 0 : Nat t 1 : Nat ( T-Succ ) succ t 1 : Nat ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 14 CIS 500, 19 October, 2005 14-a
✬ ✩ ✬ ✩ Typing Rules Typing Rules ( T-Zero ) ( T-Zero ) 0 : Nat 0 : Nat t 1 : Nat t 1 : Nat ( T-Succ ) ( T-Succ ) succ t 1 : Nat succ t 1 : Nat t 1 : Nat t 1 : Nat ( T-Pred ) ( T-Pred ) pred t 1 : Nat pred t 1 : Nat t 1 : Nat ( T-IsZero ) iszero t 1 : Bool ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 14-b CIS 500, 19 October, 2005 14-c ✬ ✩ ✬ ✩ Typing Derivations Imprecision of Typing Like other static program analyses, type systems are generally imprecise: they Every pair ( t , T ) in the typing relation can be justified by a derivation tree built from instances of the inference rules. do not predict exactly what kind of value will be returned by every program, but just a conservative (safe) approximation. T-Zero T-Zero t 1 : Bool t 2 : T t 3 : T 0 : Nat 0 : Nat ( T-If ) T-IsZero T-Zero T-Pred if t 1 then t 2 else t 3 : T iszero 0 : Bool 0 : Nat pred 0 : Nat T-If if iszero 0 then 0 else pred 0 : Nat Using this rule, we cannot assign a type to if true then 0 else false Proofs of properties about the typing relation often proceed by induction on even though this term will certainly evaluate to a number. typing derivations. ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 15 CIS 500, 19 October, 2005 16
✬ ✩ ✬ ✩ Type Safety ∗ t ′ and t ′ � − → then t ′ is a value. Type Safety Theorem: If t:T and t − → We usually prove type safety by showing the following two properties: Properties of the Typing Relation 1. Progress: A well-typed term is not stuck → t ′ for some t ′ . If t : T , then either t is a value or else t − 2. Preservation: Types are preserved by one-step evaluation → t ′ , then t ′ : T . If t : T and t − ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 17 CIS 500, 19 October, 2005 18 ✬ ✩ ✬ ✩ Inversion Inversion Lemma: Lemma: 1. If true : R , then R = Bool . 1. If true : R , then R = Bool . 2. If false : R , then R = Bool . 2. If false : R , then R = Bool . 3. If if t 1 then t 2 else t 3 : R , then t 1 : Bool , t 2 : R , and t 3 : R . 3. If if t 1 then t 2 else t 3 : R , then t 1 : Bool , t 2 : R , and t 3 : R . 4. If 0 : R , then R = Nat . 4. If 0 : R , then R = Nat . 5. If succ t 1 : R , then R = Nat and t 1 : Nat . 5. If succ t 1 : R , then R = Nat and t 1 : Nat . 6. If pred t 1 : R , then R = Nat and t 1 : Nat . 6. If pred t 1 : R , then R = Nat and t 1 : Nat . 7. If iszero t 1 : R , then R = Bool and t 1 : Nat . 7. If iszero t 1 : R , then R = Bool and t 1 : Nat . Proof: ... ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 19 CIS 500, 19 October, 2005 19-a
✬ ✩ ✬ ✩ Inversion Typechecking Algorithm Lemma: typeof(t) = if t = true then Bool else if t = false then Bool 1. If true : R , then R = Bool . else if t = if t1 then t2 else t3 then 2. If false : R , then R = Bool . let T1 = typeof(t1) in let T2 = typeof(t2) in 3. If if t 1 then t 2 else t 3 : R , then t 1 : Bool , t 2 : R , and t 3 : R . let T3 = typeof(t3) in if T1 = Bool and T2=T3 then T2 4. If 0 : R , then R = Nat . else "not typable" 5. If succ t 1 : R , then R = Nat and t 1 : Nat . else if t = 0 then Nat else if t = succ t1 then 6. If pred t 1 : R , then R = Nat and t 1 : Nat . let T1 = typeof(t1) in if T1 = Nat then Nat else "not typable" 7. If iszero t 1 : R , then R = Bool and t 1 : Nat . else if t = pred t1 then Proof: ... let T1 = typeof(t1) in if T1 = Nat then Nat else "not typable" else if t = iszero t1 then let T1 = typeof(t1) in ✫ ✪ ✫ ✪ This leads directly to a recursive algorithm for calculating the type of a term... if T1 = Nat then Bool else "not typable" CIS 500, 19 October, 2005 19-b CIS 500, 19 October, 2005 20 ✬ ✩ ✬ ✩ Canonical Forms Canonical Forms Lemma: Lemma: 1. If v is a value of type Bool , then v is either true or false . 1. If v is a value of type Bool , then v is either true or false . 2. If v is a value of type Nat , then v is a numeric value 2. If v is a value of type Nat , then v is a numeric value Proof: ... ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 21 CIS 500, 19 October, 2005 21-a
✬ ✩ ✬ ✩ Progress Progress Theorem: Suppose t is a well-typed term (that is, t : T for some T ). Then Theorem: Suppose t is a well-typed term (that is, t : T for some T ). Then either t is a value or else there is some t ′ with t − either t is a value or else there is some t ′ with t − → t ′ . → t ′ . Proof: ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 22 CIS 500, 19 October, 2005 22-a ✬ ✩ ✬ ✩ Progress Progress Theorem: Suppose t is a well-typed term (that is, t : T for some T ). Then Theorem: Suppose t is a well-typed term (that is, t : T for some T ). Then either t is a value or else there is some t ′ with t − either t is a value or else there is some t ′ with t − → t ′ . → t ′ . Proof: By induction on a derivation of t : T . Proof: By induction on a derivation of t : T . The T-True , T-False , and T-Zero cases are immediate, since t in these cases is a value. ✫ ✪ ✫ ✪ CIS 500, 19 October, 2005 22-b CIS 500, 19 October, 2005 22-c
Recommend
More recommend