Today 1. Organizational Matters 2. What is this course about? Type Systems 3. Where do “types” come from? 4. Def. of the small language Expr. Its syntax and semantics. Lecture 1 Oct. 20th, 2004 5. Structural Induction on Expr’s Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004 1. Organizational Matters 1. Organizational Matters Course Book: Benjamin Pierce, “Types and Programming Languages” Lectures: Exercises (lab): We 15:15-17:00, INR 331 MIT Press, 2002. We 13:15-15:00, INM203 Sebastian Maneth Burak Emir BC360, 021-69 31226 INR320, 021-69 36867 We will strictly follow this book! (last 3 lectures by Martin Odersky) So: Good to buy it! To get credits you have to: � 1-2 written assignments 1/3 � one programming assignment 2/3 � oral examination Type Systems for Programming Languages Type Systems for Programming Languages exe What for ?? Definition A compiler for P of prog.lang. P � to prevent execution errors . Definition of A typechecker C A PL in which all well-typed programs are free of execution errors type system T for T is called type sound . program � is ( P , T ) type sound? � is T decidable? � does C correctly implement T ? 1
Type Systems in Programming Languages What you will learn in this course: What for ?? • how to define a type system T (to allow for unambiguous implementations) � to prevent execution errors . • how to formally prove that ( P , T ) is type sound • how to implement a typechecker for T Execution Errors Execution Errors examples: examples: trapped trapped • division by zero • division by zero computation stops immediately computation stops immediately • accessing an illegal addr. • accessing an illegal addr. untrapped • jump to a wrong addr. untrapped • jump to a wrong addr. later causes arbitrary behavior • accessing past the end later causes arbitrary behavior • accessing past the end of an array of an array A program is SAFE if it does not have untrapped errors. A program is SAFE if it does not have untrapped errors. A PL is SAFE if all its programs are safe. A PL is SAFE if all its programs are. trapped + some “forbidden” untrapped errors := well-behaved safe/unsafe and typed/untyped What is a TYPE, in our context? typed untyped A type is an upper bound of the range of values that safe ML, Java LISP a program variable can assume during execution. unsafe C Assembler e.g. if x has type Boolean, then in all runs it should only take one of the values true / false. safety ⇒ integrity of run-time structures � not(x) has a meaning in every run ⇒ enables garbage collection ⇒ PLs in which variables can be given nontrivial types saves code size / develop. time are called TYPED languages . (price: performance) 2
safe/unsafe and typed/untyped var x : Boolean typechecker should typed untyped … complain! safe ML, Java LISP x := 10; unsafe C Assembler caveat: of course no one knows if this line will ever be executed! … but … it just not SAFE to have it. safety ⇒ integrity of run-time structures should not be allowed to write such a program: it has no meaning ! ⇒ enables garbage collection ⇒ SECURITY saves code size / develop. time TYPE SYSTEMS are there to PROTECT YOU from making vs. PERFORMANCE stupid (obvious) mistakes. (price: performance) Type Theory is much older than PLs! Course Outline Bertrand Russell (1872-1970) • today: Intro, Arithm. Expressions, Induction, Evaluation � LAB1 • next: (untyped) Lambda-Calculus � LAB2 untyped λ -evaluator Let P = { Q ∈ sets | Q ∉ Q} 1901 Russell’s Paradox • 3 rd : Simply-Typed Lambda-Calculus � LAB3 simply typed w. let/fix P ∉ P then: P ∈ P ⇔ • 4 rd : Simple Extensions, Subtyping � LAB4 subtyping on records ⇒ Naive set theory is inconsistent! • 5 th : Subtyping, Featherweight Java � LAB5 ⇒ MUST eliminate self-referential defs. to make set theory consistent • 6 th : Recursive Types I HOW? • 7 th : Recursive Types II • 8 th : Polymorphism I 1903 define a hierarchy of types: individuals, sets, sets of set, etc. • 9 th : Polymorphism II Any well defined set can only have elements from lower levels. • 10 th : Bounded Quantification • 11-13 th : Scala’s Type System (by Martin Odersky) Syntax and Semantics of PLs Syntax and Semantics of PLs 1960 Irons, Syntax-Directed Compiler for ALGOL 60 1960 Irons, Syntax-Directed Compiler for ALGOL 60 Compiler Compiler Defining | Translating 3
Syntax and Semantics of PLs Syntax and Semantics of PLs Until today, EBNF (ext. cf. grammar) is used to describe 1960 Irons, Syntax-Directed Compiler for ALGOL 60 the syntax of a programming language . Example: Arithmetic Expressions Compiler Expr ::= true | false | zero Expr ::= if Expr then Expr else Expr Defining | Translating Expr ::= succ Expr Expr ::= pred Expr 1966 Younger, O(n^3) Parsing of Context-Free Grammars Expr ::= isZero Expr Derivable Expressions: Syntax Translator pred succ zero � Parse Tree Check � if isZero pred succ zero then zero else true � if zero then true else false Syntax and Semantics of PLs Syntax and Semantics of PLs Until today, EBNF (ext. cf. grammar) is used to describe Until today, EBNF (ext. cf. grammar) is used to describe the syntax of a programming language . the syntax of a programming language . Example: Arithmetic Expressions Example: Arithmetic Expressions Expr ::= true | false | zero Expr ::= true | false | zero Expr ::= if Expr then Expr else Expr Expr ::= if Expr then Expr else Expr Expr ::= succ (Expr) Expr ::= succ (Expr) Expr ::= pred (Expr) Expr ::= pred (Expr) Expr ::= isZero (Expr) Expr ::= isZero (Expr) Derivable Expressions: Derivable Expressions: � pred (succ (zero)) � pred (succ (zero)) � if isZero (pred (succ (zero))) then zero else true � if isZero (pred (succ (zero))) then zero else true � if zero then true else false � if zero then true else false semantics?? Syntax and Semantics of PLs Syntax and Semantics of PLs Alternative Formalism: Inference Rules 1. Operational Semantics : behavior defined in terms of abstract The set of expressions is the smallest set E such that: machines true ∈ E false ∈ E zero ∈ E 2. Denotational Semantics : maps programs by an interpretation function into a collection of semantic domains (such as, e.g., t 1 ∈ E t 1 ∈ E t 1 ∈ E numbers, functions, etc.) succ t 1 ∈ E pred t 1 ∈ E isZero t 1 ∈ E 3. Axiomatic Semantics : proves properties of a program by t 1 ∈ E t 2 ∈ E t 3 ∈ E applying laws about program behavior (e.g., given that properties if t 1 then t 2 else t 3 ∈ E P hold before a statement, what properties Q hold after executing it?) 4
Semantics of Expr Syntax and Semantics of PLs Evaluation Relation → on Expr’s Expr ::= true | false | zero Expr ::= if Expr then Expr else Expr 1. Operational Semantics : behavior defined in terms of abstract if true then t 2 else t 3 → t 2 Expr ::= succ (Expr) machines Expr ::= pred (Expr) if false then t 2 else t 3 → t 3 Expr ::= isZero (Expr) 2. Denotational Semantics : maps programs by an interpretation t 1 → t 1 ’ function into a collection of semantic domains (such as, e.b., Val ::= true | false | NVal if t 1 then t 2 else t 3 → numbers, functions, etc) NVal ::= zero | succ NVal if t 1 ’ then t 2 else t 3 3. Axiomatic Semantics : proves properties of a program by applying laws about program behavior (e.g., given that properties P hold before a statement, what properties Q hold after executing it?) Semantics of Expr Semantics of Expr if isZero pred succ pred zero then zero else succ zero Evaluation Relation → on Expr’s Example: Expr ::= true | false | zero Expr ::= if Expr then Expr else Expr if true then t 2 else t 3 → t 2 Expr ::= succ (Expr) Expr ::= pred (Expr) if false then t 2 else t 3 → t 3 Expr ::= isZero (Expr) t 1 → t 1 ’ t 1 → t 1 ’ Val ::= true | false | NVal if t 1 then t 2 else t 3 → NVal ::= zero | succ NVal if t 1 then t 2 else t 3 → if t 1 ’ then t 2 else t 3 if t 1 ’ then t 2 else t 3 E t 1 → t 1 ’ t 1 → t 1 ’ t 1 → t 1 ’ t 1 → t 1 ’ t 1 → t 1 ’ t 1 → t 1 ’ succ t 1 → succ t 1 ’ pred t 1 → pred t 1 ’ isZero t 1 → isZero t 1 ’ succ t 1 → succ t 1 ’ pred t 1 → pred t 1 ’ isZero t 1 → isZero t 1 ’ pred zero → zero isZero zero → true pred zero → zero isZero zero → true pred succ nv 1 → nv 1 isZero succ nv 1 → false pred succ nv 1 → nv 1 isZero succ nv 1 → false Semantics of Expr Semantics of Expr redex redex if isZero pred succ pred zero then zero else succ zero if isZero pred succ pred zero then zero else succ zero Example: Example: → if isZero pred succ zero then zero else succ zero t 1 → t 1 ’ t 1 → t 1 ’ if t 1 then t 2 else t 3 → if t 1 then t 2 else t 3 → if t 1 ’ then t 2 else t 3 E if t 1 ’ then t 2 else t 3 E t 1 → t 1 ’ t 1 → t 1 ’ t 1 → t 1 ’ t 1 → t 1 ’ t 1 → t 1 ’ t 1 → t 1 ’ succ t 1 → succ t 1 ’ pred t 1 → pred t 1 ’ isZero t 1 → isZero t 1 ’ succ t 1 → succ t 1 ’ pred t 1 → pred t 1 ’ isZero t 1 → isZero t 1 ’ pred zero → zero isZero zero → true pred zero → zero isZero zero → true pred succ nv 1 → nv 1 isZero succ nv 1 → false pred succ nv 1 → nv 1 isZero succ nv 1 → false 5
Recommend
More recommend