Passing Out Review Forms

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.

type environment updated subtype sound

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.

Operational Semantics. One-Slide Summary. Lecture Outline

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 of Cool

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

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

Operational Semantics

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

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

Semantic Analysis. Lecture 9. February 7, 2018

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

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

Operational Semantics of Cool

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.

Compilers. Cool Semantics II. Alex Aiken

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

Operational Semantics of Cool

Semantic Analysis Type Checking

Scoping and Type Checking

CSCE 314 Programming Languages. Type System

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

Implementing a VSOP Compiler. March 20, 2018

(How Not To Do) Global Optimizations

CoolAid: The Cool Reference Manual

COMP520 - GoLite Type Checking Specification

Lecture #23: Conversion and Type Inference

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

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

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

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

Principles of Programming Languages

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

The Cool Reference Manual

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS558 Programming Languages

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

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

Midterm II CS164, Spring 2006

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

CIS 341 Final Examination 4 May 2017

CS558 Programming Languages

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

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

Lecture 15 CIS 341: COMPILERS

CMSC 330: Organization of Programming Languages

Local Optimizations #1

Run-Time Organization

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Written Assignment 5 Due??

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

Lecture 17 of 41. Clausal (Conjunctive Normal) Form and Resolution Techniques

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

Fall Lecture 3 September 4. Stephen Brookes

Formal Semantics of Programming Languages

The Substitution Model. Nate Foster Spring 2018

Lecture 13: Subtyping

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

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

COMP520 - GoLite Type Checking Specification

Formal Semantics of Programming Languages

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

The role of semantic analysis in a compiler

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Second-Order Type Systems

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

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

CSE332 Summer 2010: Midterm Exam Sample Solutions

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

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

The Substitution Model

Reasoning About Imperative Programs. COS 441 Slides 10

Harvard School of Engineering and Applied Sciences Computer Science 152

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

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

CIS 500 Software Foundations Fall December 6

Administrivia. Existential Types. CIS 500 Software Foundations Fall December 6. Administrivia. Motivation. Motivation

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

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

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

6. Asymptotics: The Big-O and Other Notations

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Late Binding; OOP as a Racket Pattern

Transcription:

Type Checking #1

Passing Out Review Forms #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 contain 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 Outline Typing Rules Typing Environments Let Rules Subtyping Wrong Rules #4

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

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 integer) ` i : Object #6

Type Checking Proofs Type checking proves facts e : T One type rule is used for each kind 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 ` s : String [Bool] [String] (s is a string 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] ` e1 : Bool ` e2 : T [Loop] ` while e1 loop e2 pool : Object #10

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

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

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

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

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

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

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

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

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

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

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

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

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

Typing Derivations The typing reasoning 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 instance of a typing rule Leaves are the rules with no hypotheses #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 information to give x a type. Fail. #25

A Solution: Put more information in the rules! A type environment gives types for free variables A type environment is a mapping from Object_Identifiers to Types A variable is free in an expression if: The expression contains an occurrence of the variable that refers to a declaration outside the expression in the expression x, the variable x is free in let x : Int in x + y only y is free in x + let x : Int in 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 integer) O ` e1 : Int O ` e2 : Int O ` e1 + e2 : 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] #29

Let O[T0/x] ` e1 : T1 O ` let x : T0 in e1 : T1 [Let-No-Init] O[T0/x] means O modified to map x to T0 and behaving as O on all other arguments : O[T0/x] (x) = T0 O[T0/x] (y) = O(y) (You can write O[x/T0] on tests/assignments.) #30

Let Example Consider the Cool expression let x : T0 in (let y : T1 in Ex, y) + (let x : T2 in Fx, y) (where Ex, y and Fx, y are some Cool expression that contain occurrences of x and y ) Scope of y is Ex, y of outer x is Ex, y of inner x is Fx, y This is captured precisely in the typing rule. #31

Example of Typing let AST let x : T0 in + let y : T1 in let x : T2 in Ex, y Fx, y x #32

Example of Typing let AST Type env. O ` let x : T0 in + let y : T1 in let x : T2 in Ex, y Fx, y x #33

Example of Typing let AST Type env. O ` let x : T0 in O[T0/x] ` let y : T1 in + let x : T2 in Ex, y Fx, y x #34

Example of Typing let AST Type env. O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in + O[T0/x] ` let x : T2 in Ex, y Fx, y x #35

Example of Typing let AST Type env. O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in (O[T0/x])[T1/y] ` + O[T0/x] ` let x : T2 in Ex, y Fx, y x #36

Example of Typing let AST Type env. O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in (O[T0/x])[T1/y] ` + O[T0/x] ` let x : T2 in Ex, y Fx, y (O[T0/x])[T1/y] ` x #37

Example of Typing let AST Type env. O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in (O[T0/x])[T1/y] ` + O[T0/x] ` let x : T2 in Ex, y (O[T0/x])[T2/x] ` Fx, y (O[T0/x])[T1/y] ` x #38

Example of Typing let AST Type env. Types O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in (O[T0/x])[T1/y] ` + O[T0/x] ` let x : T2 in Ex, y (O[T0/x])[T2/x] ` Fx, y (O[T0/x])[T1/y] ` x : T0 #39

Example of Typing let AST Type env. Types O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in (O[T0/x])[T1/y] ` + O[T0/x] ` let x : T2 in Ex, y : int (O[T0/x])[T2/x] ` Fx, y (O[T0/x])[T1/y] ` x : T0 #40

Example of Typing let AST Type env. Types O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in (O[T0/x])[T1/y] ` : int + O[T0/x] ` let x : T2 in Ex, y : int (O[T0/x])[T2/x] ` Fx, y (O[T0/x])[T1/y] ` x : T0 #41

Example of Typing let AST Type env. Types O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in (O[T0/x])[T1/y] ` : int + O[T0/x] ` let x : T2 in Ex, y : int (O[T0/x])[T2/x] ` Fx, y : int (O[T0/x])[T1/y] ` x : T0 #42

Example of Typing let AST Type env. Types O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in (O[T0/x])[T1/y] ` : int + O[T0/x] ` let x : T2 in : int Ex, y : int (O[T0/x])[T2/x] ` Fx, y : int (O[T0/x])[T1/y] ` x : T0 #43

Example of Typing let AST Type env. Types O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in (O[T0/x])[T1/y] ` : int + : int O[T0/x] ` let x : T2 in : int Ex, y : int (O[T0/x])[T2/x] ` Fx, y : int (O[T0/x])[T1/y] ` x : T0 #44

Example of Typing let AST Type env. Types O ` let x : T0 in O[T0/x] ` O[T0/x] ` let y : T1 in (O[T0/x])[T1/y] ` : int : int + : int O[T0/x] ` let x : T2 in : int Ex, y : int (O[T0/x])[T2/x] ` Fx, y : int (O[T0/x])[T1/y] ` x : T0 #45

Notes The type environment gives types to the free identifiers in 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 #46

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

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

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

Let with Initialization Now consider let with initialization: O ` e 0 : T0 O[T0/x] ` e1 : T1 O ` let x : T0 Ã e0 in e1 : T1 [Let-Init] This rule is weak. Why? #50

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

Subtyping Define 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 Define a relation on classes X X X Y if X inherits from Y X Z if X Y and Y Z #52

Let With Initialization (Better) O ` e0 : T T T0 O[T0/x] ` e1 : T1 O ` let x : T0 Ã e0 in e1 : T1 [Let-Init] 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 constrain programming Restrictive rules that ensure safety of execution #54

Expressiveness of Static Type Systems A static type system enables a compiler to detect many common programming errors The cost is that some correct programs are disallowed Some argue for dynamic type checking instead Others argue for more expressive static type checking 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 in 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) (in all executions, E evaluates to values of the type inferred by the compiler) This gets more complicated in advanced type systems (e.g., Java, Cool) #57

Dynamic and Static Types in COOL x has static type A class A { } class B inherits A { } class Main { Here, x s value has A x new A; dynamic type A Here, x s value has x new B; dynamic type B } A variable of static type A can hold values of static type B, if B A #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 fetching the value of an attribute Or invoking a method on the object Subclasses can only add attributes or methods Methods can be redefined but with the same types! #59

Subtyping Example Consider the following Cool class definitions Class A { a() : int { 0 }; } Class B inherits A { b() : int { 1 }; } An instance of B has methods a and b An instance of A has method a A type error occurs if we try to invoke method b on an instance of A #60

Example of Wrong Let Rule (1) Now consider a hypothetical wrong let rule: O ` e0 : T T T 0 O ` e1 : T 1 O ` let x : T0 Ã e0 in e1 : T1 How is it different from the correct rule? #61

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

Example of Wrong Let Rule (2) Now consider a hypothetical wrong let rule: O ` e0 : T T0 T O[T0/x] ` e1 : T1 O ` let x : T0 Ã e0 in e1 : T1 How is it different from the correct rule? #63

Example of Wrong Let Rule (2) Now consider a hypothetical wrong let rule: O ` e0 : T T0 T O[T0/x] ` e1 : T1 O ` let x : T0 Ã e0 in e1 : T1 How is it different from the correct rule? The following bad program is well typed let x : B Ã new A in x.b() Why is this program bad? #64

Example of Wrong Let Rule (3) Now consider a hypothetical wrong let rule: O ` e0 : T T T0 O[T/x] ` e1 : T1 O ` let x : T0 Ã e0 in e1 : T1 How is it different from the correct rule? #65

Example of Wrong Let Rule (3) Now consider a hypothetical wrong let rule: O ` e0 : T T T0 O[T/x] ` e1 : T1 O ` let x : T0 à e0 in e1 : T1 How is it different from the correct rule? The following good program is not well typed let x : A à new B in { x à new A; x.a(); } Why is this program not well typed? #66

Typing Rule Notation The typing rules use very concise notation They are very carefully constructed Virtually any change in a rule either: Makes the type system unsound (bad programs are accepted as well typed) Or, makes the type system less usable (incomplete) (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 subtyping: O(id) = T0 O ` e 1 : T1 T1 T0 [Assign] O ` id à e1 : T1 #68

Initialized Attributes Let OC(x) = T for all attributes x:t in class C OC represents the class-wide scope Attribute initialization is similar to let, except for the scope of names OC(id) = T0 OC ` e 1 : T 1 T1 T0 OC ` id : T0 Ã e1 ; [Attr-Init] #69

If-Then-Else Consider: if e0 then e1 else e2 fi The result can be either e1 or e2 The dynamic type is either e1 s or e2 s type The best we can do statically is the smallest supertype larger than the type of e1 and e2 #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 Define: 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 in the inheritance tree #72

If-Then-Else Revisited O ` e0 : Bool O ` e 1 : T1 O ` e 2 : T2 O ` if e0 then e1 else e2 fi : lub(t1, T2) [If-Then-Else] #73

Case The rule for case expressions takes a lub over all branches O ` e0 : T0 O[T1/x1] ` e1 : T1 [Case] O[Tn/xn] ` en : Tn O ` case e0 of x1:t1 ) e1; ; xn : Tn ) en; esac : lub(t1,,tn ) #74

Next Time (Post-Midterm) Type checking method dispatch Type checking with SELF_TYPE in COOL #75

Homework Today: WA3 due Wednesday: PA3 due Parsing! Thursday Feb 28 Midterm 1 in Class 2:05-3:15 One page of notes (front and back) hand-written by you Before Next Tuesday: Read Chapter 7.2 #76