type environment updated subtype sound

Similar documents
Scoping rules match identifier uses with identifier definitions. A type is a set of values coupled with a set of operations on those values.

Passing Out Review Forms

type environment updated subtype sound

More Static Semantics. Static. One-Slide Summary. Lecture Outline. Typing Rules. Dispatch Rules SELF_TYPE

More Static Semantics

More Static Semantics

Lecture Outline. Type systems and their expressiveness. Type Checking in COOL (II) Type checking with SELF_TYPE in COOL

Overview of Semantic Analysis. Lecture 9

Type Checking in COOL (II) Lecture 10

Lecture Outline. Type systems and their expressiveness. Type Checking in COOL (II) Type checking with SELF_TYPE in COOL

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Operational Semantics. One-Slide Summary. Lecture Outline

Operational Semantics of Cool

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Semantic Analysis. Lecture 9. February 7, 2018

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Operational Semantics

Subtyping: An introduction

CSci 4223 Principles of Programming Languages

Contextual Analysis (2) Limitations of CFGs (3)

Supplementary Notes on Concurrent ML

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

Functional Programming. Introduction To Cool

Announcements. Written Assignment 2 Due Monday at 5:00PM. Midterm next Wednesday in class, 11:00 1:00. Midterm review session next Monday in class.

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich

Midterm II CS164, Spring 2006

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

(How Not To Do) Global Optimizations

CSCE 314 Programming Languages. Type System

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CS558 Programming Languages

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

CMSC 330: Organization of Programming Languages

CoolAid: The Cool Reference Manual

Implementing a VSOP Compiler. March 20, 2018

CS558 Programming Languages

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Fall Lecture 3 September 4. Stephen Brookes

The Cool Reference Manual

About coding style Spring February 9, 2004

Computer Science 312 Fall Prelim 2 SOLUTIONS April 18, 2006

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Lecture #23: Conversion and Type Inference

Semantic Analysis Type Checking

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

CIS 341 Final Examination 4 May 2017

Version 4, 12 October 2004 Project handed out on 12 October. Complete Java implementation due on 2 November.

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

BNDN F2013: Software Development in Large Teams with International Collaboration C# Quiz

Programming Languages

Simply-Typed Lambda Calculus

Local Optimizations #1

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Recursive Types and Subtyping

Scoping and Type Checking

Operational Semantics of Cool

Compilers. Cool Semantics II. Alex Aiken

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Separate Compilation. Comparison. Single Source File Programs Programs where all the code is contained within 1 file.

Implementing objects as in Javascript, Lua, 164 is expensive because object attributes are implemented as hashtable accesses:

Formal Semantics of Programming Languages

6. Asymptotics: The Big-O and Other Notations

Subsumption. Principle of safe substitution

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Introduction to Axiomatic Semantics (1/2)

Formal Semantics of Programming Languages

An Introduction to Subtyping

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Operational Semantics of Cool

CSEP505 Programming Languages, Autumn 2016, Final Exam December, Programming Languages for a World of Change

Functions. Lecture 6 COP 3014 Spring February 11, 2018

7.3.3 A Language With Nested Procedure Declarations

Second-Order Type Systems

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Introduction to Axiomatic Semantics (1/2)

Concurrency Problem. Thread Synchronization: Too Much Milk. The Fundamental Issue. Critical Sections. Order of thread execution is non-deterministic

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Recursive Types and Subtyping

COMP520 - GoLite Type Checking Specification

Programs as data first-order functional language type checking

Written Assignment 5 Due??

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

Pierce Ch. 3, 8, 11, 15. Type Systems

Featherweight Java (FJ)

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Metaprogramming assignment 3

A Crash Course on Standard ML

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Lecture 15 CIS 341: COMPILERS

The Phases of a Compiler. Course Overview. In Chapter 4. Syntax Analysis. Syntax Analysis. Multi Pass Compiler. PART I: overview material

Announcements. Written Assignment 2 due today at 5:00PM. Programming Project 2 due Friday at 11:59PM. Please contact us with questions!

Transcription:

#1 Type Checkg

#2 One-Slide Summary A type environment gives types for free variables. You typecheck a let-body with an environment that has been updated to conta the new let-variable. If an object of type X could be used when one of type Y is acceptable then we say X is a subtype of Y, also written X Y. A type system is sound if E. dynamic_type(e) static_type(e)

#3 Lecture Outle Typg Rules Typg Environments Let Rules Subtypg Wrong Rules

#4 Example: 1 + 2 ` 1 : Int ` 1 + 2 : Int ` 2 : Int

If we can prove it, then it's true! Soundness A type system is sound if Whenever ` e : T Then e evaluates to a value of type T We only want sound rules But some sound rules are better than others: (i is an teger) ` i : Object #5

#6 Type Checkg Proofs Type checkg proves facts e : T One type rule is used for each kd of expression In the type rule used for a node e The hypotheses are the proofs of types of e s subexpressions The conclusion is the proof of type of e itself

#7 Rules for Constants ` false : Bool [Bool] ` s : Strg [Strg] (s is a strg constant)

#8 Rule for New new T produces an object of type T Ignore SELF_TYPE for now... ` new T : T [New]

#9 Two More Rules ` e : Bool ` not e : Bool [Not] ` e 1 : Bool ` e 2 : T ` while e 1 loop e 2 pool : Object [Loop]

#10 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool

#11 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not false

#12 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not false : Bool

#13 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not : Bool + false : Bool

#14 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not : Bool + false : Bool 1

#15 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not : Bool + false : Bool 1 : Int

#16 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not : Bool + false : Bool 1 : Int * 2

#17 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not : Bool + false : Bool 1 : Int * 2 : Int

#18 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not : Bool + false : Bool 1 : Int * 2 : Int 3

#19 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not : Bool + false : Bool 1 : Int * 2 : Int 3 : Int

#20 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not : Bool + false : Bool 1 : Int * : Int 2 : Int 3 : Int

#21 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool not : Bool + : Int false : Bool 1 : Int * : Int 2 : Int 3 : Int

#22 Typg: Example Typg for while not false loop 1 + 2 * 3 pool while loop pool : Object not : Bool + : Int false : Bool 1 : Int * : Int 2 : Int 3 : Int

Typg Derivations The typg reasong can be expressed as a tree: ` 2 : Int ` 3 : Int ` false : Bool ` 1 : Int ` 2 * 3 : Int ` not false : Bool ` 1 + 2 * 3: Int ` while not false loop 1 + 2 * 3 : Object The root of the tree is the whole expression Each node is an stance of a typg rule Leaves are the rules with no hypotheses #23

#24 A Problem What is the type of a variable reference? ` x :? [Var] (x is an identifier) The local structural rule does not carry enough formation to give x a type. Fail.

#25 A Solution: Put more formation the rules! A type environment gives types for free variables A type environment is a mappg from Object_Identifiers to Types A variable is free an expression if: The expression contas an occurrence of the variable that refers to a declaration outside the expression the expression x, the variable x is free let x : Int x + y only y is free x + let x : Int x + y both x, y are free

#26 Type Environments Let O be a function from Object_Identifiers to Types The sentence O ` e : T is read: Under the assumption that variables have the types given by O, it is provable that the expression e has the type T

#27 Modified Rules The type environment is added to the earlier rules: O ` i : Int [Int] (i is an teger) O ` e 1 : Int O ` e 2 : Int O ` e 1 + e 2 : Int [Add]

#28 New Rules And we can write new rules: O ` x : T [Var] (O(x) = T) Equivalently: O(x) = T O ` x : T [Var]

Let e 1 : T 1 O ` let x : T 0 e 1 : T 1 [Let-No-Init] /x] means O modified to map x to T 0 and behavg as O on all other arguments : /x] (x) = T 0 /x] (y) = O(y) (You can write O[x/T 0 ] on tests/assignments.) #29

#30 Let Example Consider the Cool expression let x : T 0 (let y : T 1 E x, y ) + (let x : T 2 F x, y ) (where E x, y and F x, y are some Cool expression that conta occurrences of x and y ) Scope of y is E x, y of outer x is E x, y of ner x is F x, y This is captured precisely the typg rule.

#31 Example of Typg let AST let x : T 0 + let y : T 1 let x : T 2 E x, y Fx, y x

#32 Example of Typg let AST Type env. O ` let x : T 0 + let y : T 1 let x : T 2 E x, y Fx, y x

#33 Example of Typg let AST Type env. O ` let x : T 0 + let y : T 1 let x : T 2 E x, y Fx, y x

#34 Example of Typg let AST Type env. O ` let x : T 0 + let y : T 1 let x : T 2 E x, y x Fx, y

#35 Example of Typg let AST Type env. O ` let x : T 0 + let y : T 1 let x : T 2 ( /x])[t 1 /y] ` E x, y Fx, y x

#36 Example of Typg let AST Type env. O ` let x : T 0 + let y : T 1 let x : T 2 ( /x])[t 1 /y] ` E x, y Fx, y ( /x])[t 1 /y] ` x

#37 Example of Typg let AST Type env. O ` let x : T 0 + let y : T 1 let x : T 2 ( /x])[t 1 /y] ` E x, y ( /x])[t 2 Fx, y ( /x])[t 1 /y] ` x

#38 Example of Typg let AST Type env. Types O ` let x : T 0 + let y : T 1 let x : T 2 ( /x])[t 1 /y] ` E x, y ( /x])[t 2 Fx, y ( /x])[t 1 /y] ` x : T 0

#39 Example of Typg let AST Type env. Types O ` let x : T 0 + let y : T 1 let x : T 2 ( /x])[t 1 /y] ` E x, y : t ( /x])[t 2 Fx, y ( /x])[t 1 /y] ` x : T 0

#40 Example of Typg let AST Type env. Types O ` let x : T 0 + let y : T 1 : t let x : T 2 ( /x])[t 1 /y] ` E x, y : t ( /x])[t 2 Fx, y ( /x])[t 1 /y] ` x : T 0

#41 Example of Typg let AST Type env. Types O ` let x : T 0 + let y : T 1 : t let x : T 2 ( /x])[t 1 /y] ` E x, y : t ( /x])[t 2 Fx, y : t ( /x])[t 1 /y] ` x : T 0

#42 Example of Typg let AST Type env. Types O ` let x : T 0 + let y : T 1 : t let x : T 2 : t ( /x])[t 1 /y] ` E x, y : t ( /x])[t 2 Fx, y : t ( /x])[t 1 /y] ` x : T 0

#43 Example of Typg let AST Type env. O ` let x : T 0 Types + : t let y : T 1 : t let x : T 2 : t ( /x])[t 1 /y] ` E x, y : t ( /x])[t 2 Fx, y : t ( /x])[t 1 /y] ` x : T 0

#44 Example of Typg let AST Type env. O ` let x : T 0 : t Types + : t let y : T 1 : t let x : T 2 : t ( /x])[t 1 /y] ` E x, y : t ( /x])[t 2 Fx, y : t ( /x])[t 1 /y] ` x : T 0

#45 Notes The type environment gives types to the free identifiers the current scope The type environment is passed down the AST from the root towards the leaves Types are computed up the AST from the leaves towards the root

Q: Movies (362 / 842) In this 1992 comedy Dana Carvey and Mike Myers reprise a Saturday Night Live skit, sg Bohemian Rhapsody and say of a guitar: "Oh yes, it will be me."

Q: General (455 / 842) This numerical technique for fdg solutions to boundary-value problems was itially developed for use structural analysis the 1940's. The subject is represented by a model consistg of a number of lked simplified representations of discrete regions. It is often used to determe stress and displacement mechanical systems.

Q: Movies (377 / 842) Identify the subject or the speaker 2 of the followg 3 Star Wars quotes. "Aren't you a little short to be a stormtrooper?" "I felt a great disturbance the Force... as if millions of voices suddenly cried out terror and were suddenly silenced." "I recognized your foul stench when I was brought on board."

#49 Let with Initialization Now consider let with itialization: O ` e 0 : T 0 e 1 : T 1 [Let-Init] O ` let x : T 0 Ã e 0 e 1 : T 1 This rule is weak. Why?

#50 Let with Initialization Consider the example: class C herits P { } let x : P Ã new C The previous let rule does not allow this code We say that the rule is too weak or complete

Subtypg Defe a relation X Y on classes to say that: An object of type X could be used when one of type Y is acceptable, or equivalently X conforms with Y In Cool this means that X is a subclass of Y Defe a relation on classes X X X Y if X herits from Y X Z if X Y and Y Z #51

#52 Let With Initialization (Better) O ` e 0 : T T T 0 e 1 : T 1 [Let-Init] O ` let x : T Ã e e : T 0 0 1 1 Both rules for let are sound But more programs type check with this new rule (it is more complete)

#53 Type System Tug-of-War There is a tension between Flexible rules that do not constra programmg Restrictive rules that ensure safety of execution

#54 Expressiveness of Static Type Systems A static type system enables a compiler to detect many common programmg errors The cost is that some correct programs are disallowed Some argue for dynamic type checkg stead Others argue for more expressive static type checkg But more expressive type systems are also more complex

#55 Dynamic And Static Types The dynamic type of an object is the class C that is used the new C expression that creates the object A run-time notion Even languages that are not statically typed have the notion of dynamic type The static type of an expression is a notation that captures all possible dynamic types the expression could take A compile-time notion

#56 Dynamic and Static Types. (Cont.) In early type systems the set of static types correspond directly with the dynamic types Soundness theorem: for all expressions E dynamic_type(e) = static_type(e) ( all executions, E evaluates to values of the type ferred by the compiler) This gets more complicated advanced type systems (e.g., Java, Cool)

Dynamic and Static Types COOL x has static type A class A { } class B herits A { } class Ma { A x new A; x new B; } Here, x s value has dynamic type A Here, x s value has dynamic type B A variable of static type A can hold values of static type B, if B A #57

#58 Dynamic and Static Types Soundness theorem for the Cool type system: E. dynamic_type(e) static_type(e) Why is this Ok? For E, compiler uses static_type(e) All operations that can be used on an object of type C can also be used on an object of type C C Such as fetchg the value of an attribute Or vokg a method on the object Subclasses can only add attributes or methods Methods can be redefed but with the same types!

#59 Subtypg Example Consider the followg Cool class defitions Class A { a() : t { 0 }; } Class B herits A { b() : t { 1 }; } An stance of B has methods a and b An stance of A has method a A type error occurs if we try to voke method b on an stance of A

#60 Example of Wrong Let Rule (1) Now consider a hypothetical wrong let rule: O ` e : T T T O ` e : T 0 0 1 1 O ` let x : T Ã e e : T 0 0 1 1 How is it different from the correct rule?

#61 Example of Wrong Let Rule (1) Now consider a hypothetical wrong let rule: O ` e : T T T O ` e : T 0 0 1 1 O ` let x : T à e e : T 0 0 1 1 How is it different from the correct rule? The followg good program does not typecheck let x : Int à 0 x + 1 Why?

#62 Example of Wrong Let Rule (2) Now consider a hypothetical wrong let rule: O ` e : T T T O[T e : T 0 0 0 1 1 O ` let x : T Ã e e : T 0 0 1 1 How is it different from the correct rule?

#63 Example of Wrong Let Rule (2) Now consider a hypothetical wrong let rule: O ` e : T T T O[T e : T 0 0 0 1 1 O ` let x : T Ã e e : T 0 0 1 1 How is it different from the correct rule? The followg bad program is well typed let x : B Ã new A x.b() Why is this program bad?

#64 Example of Wrong Let Rule (3) Now consider a hypothetical wrong let rule: O ` e : T T T O[T e : T 0 0 1 1 O ` let x : T Ã e e : T 0 0 1 1 How is it different from the correct rule?

#65 Example of Wrong Let Rule (3) Now consider a hypothetical wrong let rule: O ` e : T T T O[T e : T 0 0 1 1 O ` let x : T à e e : T 0 0 1 1 How is it different from the correct rule? The followg good program is not well typed let x : A à new B { x à new A; x.a(); } Why is this program not well typed?

#66 Typg Rule Notation The typg rules use very concise notation They are very carefully constructed Virtually any change a rule either: Makes the type system unsound (bad programs are accepted as well typed) Or, makes the type system less usable (complete) (good programs are rejected) But some good programs will be rejected anyway The notion of a good program is undecidable

#67 Assignment More uses of subtypg: O(id) = T 0 O ` e 1 : T 1 T 1 T 0 [Assign] O ` id à e 1 : T 1

Initialized Attributes Let O C (x) = T for all attributes x:t class C O C represents the class-wide scope Attribute itialization is similar to let, except for the scope of names O C (id) = T 0 O C ` e 1 : T 1 T 1 T 0 O C ` id : T 0 Ã e 1 ; [Attr-Init] #68

#69 If-Then-Else Consider: if e 0 then e 1 else e 2 fi The result can be either e 1 or e 2 The dynamic type is either e 1 s or e 2 s type The best we can do statically is the smallest supertype larger than the type of e 1 and e 2

#70 If-Then-Else example Consider the class hierarchy P A B and the expression if then new A else new B fi Its type should allow for the dynamic type to be both A or B Smallest supertype is P

#71 Least Upper Bounds Defe: lub(x,y) to be the least upper bound of X and Y. lub(x,y) is Z if X Z Æ Y Z Z is an upper bound X Z Æ Y Z Z Z Z is least among upper bounds In Cool, the least upper bound of two types is their least common ancestor the heritance tree

#72 If-Then-Else Revisited O ` e : Bool 0 O ` e : T 1 1 O ` e : T 2 2 O ` if e then e else e fi : lub(t, T ) 0 1 2 1 2 [If-Then-Else]

#73 Case The rule for case expressions takes a lub over all branches O ` e 0 : T 0 O[T 1 /x 1 ] ` e 1 : T 1 O[T n /x n ] ` e n : T n [Case] O ` case e 0 of x 1 :T 1 ) e 1 ; ; x n : T n ) e n ; esac : lub(t 1,,T n )

#74 Next Time Type checkg method dispatch Type checkg with SELF_TYPE COOL

#75 Homework Today: WA3 due Get started on PA4 Checkpot due Oct 14 Before Next Class: Read Chapter 7.2