Typed Arithmetic Expressions CS 550 Programming Languages Jeremy

  • Slides: 46
Download presentation
Typed Arithmetic Expressions CS 550 Programming Languages Jeremy Johnson TAPL Chapters 3 and 5

Typed Arithmetic Expressions CS 550 Programming Languages Jeremy Johnson TAPL Chapters 3 and 5 1

Types and Safety v Evaluation rules provide operational semantics for programming languages. The rules

Types and Safety v Evaluation rules provide operational semantics for programming languages. The rules provide state transitions for an abstract machine that evaluates terms in the language. v Evaluating a term can result in a value or get stuck in an erroneous state. We would like to be able to tell, without actually evaluating the term, whether or not it will get stuck. v Type rules are introduced to associate types with terms. A term t is well-typed if there is some type T such that t has type T. v The safety property says that well-typed terms do not lead to erroneous states, i. e. “do not go wrong” v Safety is progress plus preservation. A well-typed term is either a value or it transitions to another well-typed term. 2

Outline v. Arithmetic Expressions ØSyntax and evaluation rules ØUntyped arithmetic expressions ØTyped arithmetic expressions

Outline v. Arithmetic Expressions ØSyntax and evaluation rules ØUntyped arithmetic expressions ØTyped arithmetic expressions ØSafety – well typed terms do not get “stuck” 3

Boolean Expressions Syntax Evaluation t : : = true false if t then t

Boolean Expressions Syntax Evaluation t : : = true false if t then t else t if true then t 2 else t 3 t 2 (E-If. True) v : : = true false if false then t 2 else t 3 (E-If. False) t 1 (E-If) if t 1 then t 2 else t 3

Derivation s = if true then false else false t = if s then

Derivation s = if true then false else false t = if s then true else true u = if false then true else true E-If. True s false E-If t u E-If if t then false else false if u then false else false

Determinacy Theorem [Determinacy of one-step evaluation]. If t t and t t , then

Determinacy Theorem [Determinacy of one-step evaluation]. If t t and t t , then t = t . Proof. By induction on a derivation of t t.

Determinacy Proof. By induction on a derivation of t t. Three cases: E-If. True,

Determinacy Proof. By induction on a derivation of t t. Three cases: E-If. True, E-If. False and E-If. The first two are base cases. 1) E-If. True. t = if true then t 2 else t 3 and t = t 2 E-If. False and E-If are not applicable, so t = t = t 2

Determinacy Proof. By induction on a derivation of t t. Three cases: E-If. True,

Determinacy Proof. By induction on a derivation of t t. Three cases: E-If. True, E-If. False and E-If. 2) E-If. False. t = if false then t 2 else t 3 and t = t 3 E-If. True and E-If are not applicable, so t = t = t 3

Determinacy Proof. By induction on a derivation of t t. Three cases: E-If. True,

Determinacy Proof. By induction on a derivation of t t. Three cases: E-If. True, E-If. False and E-If. 3) E-If. t = if t 1 then t 2 else t 3 and t 1 E-If. True and E-If. False are not applicable since t 1 is not a normal form.

Determinacy Proof. By induction on a derivation of t t. 3) E-If. t =

Determinacy Proof. By induction on a derivation of t t. 3) E-If. t = if t 1 then t 2 else t 3 and t = if t 1 then t 2 else t 3 with t 1 E-If. True and E-If. False are not applicable since t 1 is not a normal form, so t = if t 1 then t 2 else t 3 with t 1. By induction t 1 = t 1 and t = t .

Normal Forms Definition. A term t is in normal form if no evaluation rule

Normal Forms Definition. A term t is in normal form if no evaluation rule applies to it. Theorem. Every value is in a normal form and every term t that is in normal form is a value. Proof. Immediate from rules. Show contrapositive by induction on t.

Normal Forms Theorem. Every value is in a normal form and every term t

Normal Forms Theorem. Every value is in a normal form and every term t that is in normal form is a value. Show contrapositive by induction on t. “If t is not a value then it is not a normal form” t = if t 1 then t 2 else t 3 1. t 1 = true E-If. True applicable 2. t 1 = false E-If. False applicable 3. else by induction t 1 and E-If is applicable

Multi-step Evaluation Definition. Multistep evaluation * is the reflexive, transitive closure of one step

Multi-step Evaluation Definition. Multistep evaluation * is the reflexive, transitive closure of one step evaluation.

Uniqueness of Normal Forms Theorem [Uniqueness of normal forms]. If t * u and

Uniqueness of Normal Forms Theorem [Uniqueness of normal forms]. If t * u and t * v, where u and v are normal forms, then u = v. Proof. Corollary of determinacy.

Termination of Evaluation Theorem [Termination of evaluation]. For every term t, there is a

Termination of Evaluation Theorem [Termination of evaluation]. For every term t, there is a normal form t’ such that t * t. Proof. Each evaluation step decreases the size and since size is natural number and the natural numbers are well founded the size must reach 1.

Arithmetic Expressions Syntax Evaluation t : : = 0 succ t pred t iszero

Arithmetic Expressions Syntax Evaluation t : : = 0 succ t pred t iszero t t 1 (E-Succ) succ t 1 nv : : = 0 succ nv pred 0 0 (E-Pred. Zero) pred (succ nv 1) nv 1 (E-Pred. Succ) t 1 (E-Pred) pred t 1 iszero 0 true (E-Is. Zero) iszero (succ nv 1) false (E-Is. Zero. Succ) t 1 (E-Is. Zero) iszero t 1

Derivation pred (succ (pred 0)) * 0 pred (succ (pred 0)) pred (succ 0)

Derivation pred (succ (pred 0)) * 0 pred (succ (pred 0)) pred (succ 0) 0 E-Pred. Zero pred 0 0 E-Succ succ (pred 0) succ 0 E-Pred pred (succ (pred 0)) pred (succ 0)

Stuck Terms Definition. A stuck term is a term that is in normal form

Stuck Terms Definition. A stuck term is a term that is in normal form but is not a value. E. G. pred false if 0 then true else false

Typing Relation Definition. A typing relation, t : T, is defined by a set

Typing Relation Definition. A typing relation, t : T, is defined by a set of inference rules assigning types to terms. A term is well-typed if there is some T such that t : T. For arithmetic expressions there are two types: Bool and Nat Insisting that evaluation rules are only applied to proper types prevents things from going wrong (getting stuck).

Typing Relation Syntax Typing Rules T : : = Bool true : Bool (T-True)

Typing Relation Syntax Typing Rules T : : = Bool true : Bool (T-True) false : Bool (T-False) t 1 : Bool, t 2 : T, t 3 : T (T-If) if t 1 then t 2 else t 3 : T

Typing Relation Syntax Typing Rules T : : = Nat 0 : Nat (T-Zero)

Typing Relation Syntax Typing Rules T : : = Nat 0 : Nat (T-Zero) t 1 : Nat (T-Succ) succ t 1 : Nat *The typing relation is conservative. I. E. some terms that do not get stuck are not well-typed. if (iszero 0) then 0 else false Want type checking to be “easy” t 1 : Nat (T-Pred) pred t 1 : Nat (T-Is. Zero) iszero t 1 : Bool

Inversion Lemma 1. If true : R, then R = Bool. 2. If false

Inversion Lemma 1. If true : 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 and t 2 : R and t 3 : R. 4. If 0 : R, then R = 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 7. If iszero t 1 : R, then R = Bool and t 1 : Nat

Uniqueness of Types Theorem. Each term t has at most one type. Proof. By

Uniqueness of Types Theorem. Each term t has at most one type. Proof. By induction on t using the inversion lemma. The inversion lemma provides a recursive algorithm for computing types.

Safety = Progress + Preservation Progress. A well-typed term is not stuck (either it

Safety = Progress + Preservation Progress. A well-typed term is not stuck (either it is a value or it can take a step according to the evaluation rules). Preservation. If a well-typed term takes a step of evaluation, then the resulting term is well-typed.

Progress Theorem. If t : T, then t is a value or there is

Progress Theorem. If t : T, then t is a value or there is a t’ such that t t’. Proof. By induction on the derivation of t : T.

Progress Theorem. If t : T, then t is a value or there is

Progress Theorem. If t : T, then t is a value or there is a t such that t t. Proof. By induction on the derivation of t : T. Base Cases: T-True, T-False, T-Zero, then t is a value.

Progress Theorem. If t : T, then t is a value or there is

Progress Theorem. If t : T, then t is a value or there is a t such that t t. Proof. By induction on the derivation of t : T. Case: T-If t = if t 1 then t 2 else t 3, t 1 : Bool, t 2, t 3 : T By induction either t 1 has a value E-If. True or E -If. False is applicable. Else t 1 and E-If is applicable

Progress Theorem. If t : T, then t is a value or there is

Progress Theorem. If t : T, then t is a value or there is a t such that t t. Proof. By induction on the derivation of t : T. Case: T-Succ t = succ t 1 and t 1 : Nat By induction either t 1 has a value t is a value else t 1 and E-Succ is applicable

Progress Theorem. If t : T, then t is a value or there is

Progress Theorem. If t : T, then t is a value or there is a t such that t t. Proof. By induction on the derivation of t : T. Case: T-Pred t = pred t 1 and t 1 : Nat By induction either t 1 has a value E-Pred. Zero or E-Pred. Succ is applicable else t 1 and EPred is applicable

Progress Theorem. If t : T, then t is a value or there is

Progress Theorem. If t : T, then t is a value or there is a t such that t t. Proof. By induction on the derivation of t : T. Case: T-Is. Zero t = iszero t 1 and t 1 : Nat By induction either t 1 is a value either EIs. Zero or E-Is. Zero. Succ is applicable else t 1 and E-Is. Zero is applicable

Preservation Theorem. If t : T and t t , then t : T.

Preservation Theorem. If t : T and t t , then t : T. Proof. By induction on t : T.

Preservation Theorem. If t : T and t t , then t : T.

Preservation Theorem. If t : T and t t , then t : T. Proof. By induction on t : T. Case T-True, T-False, and T-Zero. t = true and T = Bool. t = false and T = Bool. t = 0 and T = Nat In all of these cases, t is a value and there is no t t and theorem is vacuously true.

Preservation Theorem. If t : T and t t , then t : T.

Preservation Theorem. If t : T and t t , then t : T. Proof. By induction on t : T. Case T-If t = if t 1 then t 2 else t 3, t 1 : Bool, t 2, t 3 : T E-If. True E-If. False t 1 = true t = t 2 : T t 1 = false t = t 3 : T E-If t 1 and by induction t 1 : Bool t = if t 1 then t 2 else t 3 : T (by T-If)

Preservation Theorem. If t : T and t t , then t : T.

Preservation Theorem. If t : T and t t , then t : T. Proof. By induction on t : T. Case T-Succ t = succ t 1 T = Nat t 1 : Nat E-Succ t 1 and by induction t 1 : Nat t = succ t 1 : Nat (by T-Succ)

Preservation Theorem. If t : T and t t , then t : T.

Preservation Theorem. If t : T and t t , then t : T. Proof. By induction on t : T. Case T-Pred t = pred t 1 T = Nat t 1 : Nat E-Pred. Zero t 1 = 0 and t = 0 : Nat (by T-Zero) E-Pred. Succ t 1 = succ nv 1 and t = nv 1 : Nat (by TZero or T-Succ) E-Pred t 1 and by induction t 1 : Nat t = pred t 1 : Nat (by T-Pred)

Preservation Theorem. If t : T and t t , then t : T.

Preservation Theorem. If t : T and t t , then t : T. Proof. By induction on t : T. Case T-Is. Zero t = iszero t 1 T = Bool t 1 : Nat E-Zero t 1 = 0 and t = true : Bool (by T-True) E-Is. Zero. Succ t 1 = succ nv 1 and t = false : Bool (by T-False) E-Is. Zero t 1 and by induction t 1 : Nat t = iszero t 1 : Bool (by T-Is. Zero)

Untyped Lambda Calculus Syntax t : : = terms: x variable x. t abstraction

Untyped Lambda Calculus Syntax t : : = terms: x variable x. t abstraction t t application v : : = values x. t abstraction value v

Substitution v

Substitution v

Function Types Extend types to include functions § § x: true [returns Bool] x.

Function Types Extend types to include functions § § x: true [returns Bool] x. y. y [returns function] x. if x then 0 else 1 [returns Nat, expects Bool] T T Definition. T : : = T T | Bool § Bool, (Bool Bool) § T 1 T 2 T 3 T 1 (T 2 T 3)

Typing Relation v

Typing Relation v

Simply Typed Lambda Calculus Syntax t : : = terms: x variable x: T.

Simply Typed Lambda Calculus Syntax t : : = terms: x variable x: T. t abstraction t t application v : : = values x: T. t abstraction value v

Lambda Calculus Typing Syntax T : : = types: T T function types :

Lambda Calculus Typing Syntax T : : = types: T T function types : : = contexts empty context , x: T term variable binding v

Substitution Lemma v

Substitution Lemma v

Safety v

Safety v

Erasure Definition. The erasure of a simply typed term t is defined by erase(x)

Erasure Definition. The erasure of a simply typed term t is defined by erase(x) = x erase( x: T 1. t 2 ) = x. erase(t 2) erase(t 1 t 2) = erase(t 1) erase(t 2) Theorem. If t t , then erase(t) erase(t ). If erase(t) m , then there is a simply typed term t such that t t and erase(t ) = m.

Curry-Howard Correspondence Logic 1. Propositions 2. P Q 3. P Q 4. Proof of

Curry-Howard Correspondence Logic 1. Propositions 2. P Q 3. P Q 4. Proof of P 5. Prop P is provable Programming Languages 1. Types 2. Type P Q 3. Type P Q 4. Term t of type P 5. Type P is inhabited by some term A term of - is a proof of a logical proposition corresponding to its type.