Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Symbol Table.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Introducing Formal Methods, Module 1, Version 1.1, Oct., Formal Specification and Analytical Verification L 5.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
Compiler Construction
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Prof. Hilfinger CS164 Lecture 191 Type Checking Lecture 19 (from notes by G. Necula)
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Semantic Analysis Professor Yihjia Tsai Tamkang University.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
3/3/2008Prof. Hilfinger CS164 Lecture 171 Type Checking Lecture 17 (P. N. Hilfinger and G. Necula)
Prof. Fateman CS 164 Lecture 131 Types Lecture 13.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Prof. Necula CS 164 Lectures Semantic Analysis Typechecking in COOL Lecture 7.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Type Systems CS Definitions Program analysis Discovering facts about programs. Dynamic analysis Program analysis by using program executions.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS 363 Comparative Programming Languages Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Chapter 12 Support for Object oriented Programming.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
CMSC 330: Organization of Programming Languages Operational Semantics.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Operational Semantics of Scheme
Functional Programming
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Context-Sensitive Analysis
Semantic Analysis with Emphasis on Name Analysis
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
Corky Cartwright January 18, 2017
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Mini Language Interpreter Programming Languages (CS 550)
Names and Binding In Text: Chapter 5.
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
Presentation transcript:

Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)

Outline What is type checking Simple type rules Self_Type Implementation

Types What is a type –Varies from language to language Consensus –A set of values –A set of operations Classes –One instantiation of the modern notion of types

Why do we need type systems? Consider assembly code –add $r1, $r2, $r3 What are the types of $r1, $r2, $r3?

Types and Operations Certain operations are legal for values of each type –It does not make sense to add a function pointer and an integer in C –It does make sense to add two integers –But both have the same assembly language implementation!

Type Systems A language’s type system specifies which operations are valid for which types The goal of type checking is to ensure that operations are used with the correct types –Enforces intended interpretation of values because nothing else will!

Type Checking Overview Three kinds of languages –Statically typed: (Almost) all checking of types is done as part of compilation Semantic Analysis C, Java, Cool, ML –Dynamically typed: Almost all checking of types is done as part of program execution Code generation Scheme –Untyped No type checking (Machine Code)

Type Wars Competing views on static vs. dynamic typing Static typing proponents say: –Static checking catches many programming errors –Prove properties of your code –Avoids the overhead of runtime type checks Dynamic typing proponents say –Static type systems are restrictive –Rapid prototyping difficult with types systems –Complicates the programming language and the compiler –Compiler optimizations can hide costs

Type Wars (cont.) In practice, most code is written in statically typed languages with escape mechanisms –Unsafe casts in C Java –union in C It is debatable whether this compromise represents the best or worst of both worlds

Types Outline Types concepts in Cool Notation for type rules –Logical rules of inference Cool type rules General properties of type systems

Cool Types The types are –Class Names –SELF_TYPE The user declares types for identifiers The semantic analysis infers types for expressions –Every expression has a unique type

Type Checking and Type Inference Type checking –The process of verifying fully typed programs Type inference –The process of filling in missing type information Different terms used interchangeably

Rules of Inference We have seen two examples of formal notions specifying parts of the compiler –Regular expressions –Context-free grammars Appropriate formalisms for static type checking –Syntax directed translations –Attribute grammars –Logical rules of inference

Why Rules of Inference? Inference rules have the form: –“If Hypothesis is true, then conclusion is true” Type checking computes via reasoning –“If E1 and E2 have certain types, then E1+E2 have certain type” Rules of inference are compact notation of “If-Then” statements

From English to an inference rule [Easy to read with practice] Start with a simplified system and gradually add features Building blocks –Symbol  is ‘and’ –Symbol  is ‘if then’ –Symbol x:T is ‘x has type T’

From English to an inference rule(2 ) If e1 has type Int and e2 has type Int, then e1 + e2 has type Int (e1 has type Int  e2 has type Int)  e1 +e2 has type Int (e1: Int  e2: Int)  e1+e2: Int

From English to an inference rule(3 ) The statement (e1: Int  e2: Int)  e1+e2: Int is a special case of Hypothesis 1 ...  Hypothesis n  Conclusion This is an inference rule

Notation for Inference Rules By tradition inference rules are written  Hypothesis 1 ...  Hypothesis n Conclusion Cool type rules have hypothesis and conclusion  e: T  means “it is provable that...”

Two Rules i is an integer  i : Int [Int] de1 : Int de2 : Int  e1+e2 : Int [Add]

Type Rules (cont.) These rules give templates describing how to type integers and + expressions By filling the templates, we can produce complete typings for expressions

Example is an integer  1 : Int [Int] 2 is an integer  2: Int [Int]  1+2: Int [Add]

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: 1 is an integer  1 : Object [Strange]

Type Checking Proofs Type checking proves facts e: T –Proof is on structure of the AST –Proof has the shape of the AST –One type rule is used for each AST node If the type rule used for a node e: –Hypotheses are the proofs of e’s subexpressions –Conclusion is the type of e Bottom up pass over the AST

Rules for Constants  false : Bool [Bool] s is a string constant  s: String [String]

Rules for New new T produces an object of type T –Ignore SELF_TYPE for now...  new T: T [New]

Two More Rules  e: Bool   e: Bool [Not]  e1: Bool  e2: T  while e1 loop e2 pool: Object [Loop]

A Problem What is the type of a variable reference The local structure rules does not carry enough information to give x a type x is an identifier  x: ? [Var]

A Solution Put more information in the rules A type environment gives types for free variables –A type environment is a function from ObjectIdentifiers to Types –Symbol table –A variable is free in an expression if it is not defined within the expression

Type Environments Let O be a function from ObjectIdentifiers to Types The sentence O  e: T is read: under the assumption that variables have the types given by O, it is provable that e has the type T

Modified Rules i is an integer O  i : Int [Int] O  e1 : Int O  e2 : Int O  e1+e2 : Int [Add]

New Rules O(x)=T O  x: T [Var]

Let Rule O(T 0 /x)  e 1 : T 1 O  let x: T 0 in e 1 : T 1 [Let-No-Init] O[T/y] means O modified to return T on argument y Enforces a variable scope

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

Let Rule with Initialization O  e 0 : T 0 O(T 0 /x)  e 1 : T 1 O  let x: T 0  e 0 in e 1 : T 1 [Let-Init] Weak rule

Subtyping Define a relation  on classes –X  X –X  Y if X inherits from Y –X  Z if X  Y and Y  Z O  e 0 : T T  T 0 O(T 0 /x)  e 1 : T 1 O  let x: T 0  e 0 in e 1 : T 1 [Let-Init]

Assignment Both rules are sound but more programs typecheck with the second one More uses of subtyping O (Id) = T 0 O  e 1 : T 1 T 1  T 0 O  Id  e 1 : T 1 [Assign]

Initialized Attributes Let O c (x) = T for all attributes x:T in class C Attribute initialization is similar to let, except 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 : T 1 ; [Attr-Init]

If-Then-Else Consider: if e0 then e1 else e2 The result can be either e1 or e2 The type is either e1’s type or e2’ type The best we can do is the smallest supertype larger then the type of e1 and e2

Least Upper Bounds lub(X, Y) is the least upper bound of X and Y (denoted by Z) –X  Z and Y  Z Z is upper bound –X  Z’ and Y  Z’  Z  Z’ Z is the least upper bound In Cool, the least upper bound of two types is their least common ancestor in the inheritance tree

If-Then-Else-Revisited O c  e 0 :Bool O  e 1 : T 1 O  e 2 : T 2 O  if e 0 then e 1 else e 2 : lub(T 1, T 2 ) [If-Then-Else]

Case The rule for case expressions takes 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 O  case e 0 of x 1 : T 1 ;... ; x n :T n esac : lub(T’ 1,.., T’ n ) [Case]

Method Dispatch There is a problem with type checking method calls We need information about the formal parameters of and return type O  e 0 :T 0 O  e 1 : T 1... O  e n : T n O  e 0.f(e 1,,,,,, e n ): ? [Dispatch]

Notes on dispatch In Cool, the method and object identifiers live in different name spaces –A method foo and an object foo can coexist in the same scope –In the type rules this is reflected by a separate mapping M for method signatures M(C, f) = (T 1,..., T n, T n+1 ) means that in class C there is a method f f(X 1 :T 1,..., X n :T n ): T n+1

The Dispatch Rule Revisited O, M  e 0 :T 0 O, M  e 1 : T 1... O, M  e n : T n M(T 0, f) = (T’ 1,..., T’ n, T’ n+1 ) T i  T’ i for 1  i  n O  e 0.f(e 1,,,,,, e n ):T’ n+1 [Dispatch]

Static Dispatch A variation of normal dispatch The method is found in the class explicitly named by the programmer The inferred type of the dispatch expression must conform to the specified type

Static Dispatch O, M  e 0 :T 0 O, M  e 1 : T 1... O, M  e n : T n T 0  T M(T 0, f) = (T’ 1,..., T’ m, T’ n+1 ) T i  T’ i for 1  i  n O  e 1,,,,,, e n ):T’ n+1 [StaticDispatch]

The Method Environment The method environment must be added to all rules In most cases, M is passed but not actually used O, M  e1 : Int O, M  e2 : Int O, M  e1+e2 : Int [Add]

More Environments For some cases involving SELF_TYPE, we need to know the class in which an expression appears The full environment for COOL –A mapping O gives types to Object Id’s –A mapping M giving types to methods –The current C

Sentences The form of a sentence in the logic is O, M, C  e : C O, M, C  e1 : Int O, M, C  e2 : Int O, M, C  e1+e2 : Int [Add]

Effectiveness of Static Type Systems Static type systems detect common errors But some correct programs are disallowed –Some argue for dynamic checking instead –Other argue for more expressive type systems But more expressive type systems are more complex

Dynamic and Static Types The dynamic type of an object is the class that is used in the new expression –a runtime notion –Even languages that are statically typed have dynamic types The static type of an expression captures all the dynamic types that the expression could have –A compile-time notion

Dynamic and static types In early type systems the set of static types correspond directly to the dynamic types Soundness theorem: for all expressions E dynamic_type(E) = static_type(E) Gets more complicated in advanced type systems

Dynamic and Static Types in Cool A variable of static type A can hold the value of static type B if B  A class A {... } class B inherits A {... } class Main ( x: A  new A ;... x  new B ; }

Dynamic and Static Types Soundness of the Cool type system: –  E. dynamic_type(E)  static_type(E) Why is this ok –All operations that can be used on an object of type C can be also used on an object of type C’  C –Subclasses only add behavior (attributes or methods) –Methods can be redefined but with the same type!

An Example class Count { i : Int  0 ; Inc() Count { { i  i + 1 ; self ; } } ; class Stock inherits Count { name: String ; }; class Main { Stock a  (new Stock). inc();... a.name.. };

SELF_TYPE to the Rescue We will extend the type system Insight –Inc returns “self” –The return value has type as “self” –Any subtype of Count Introduce the Keyword SELF_TYPE to use for return value of such functions –Need to modify the typing rules

An Example (revisited) class count { i : Int  0 ; Inc(): Self_Type { { i  i + 1 ; self ; } } ; class Stock inherits Count { name: String ; }; class Main { Stock a  (new Stock). inc();... a.name.. };

Type Systems The rules in this lecture are Cool-specific A lot of theory about type systems General themes –Type rules are defined on the structure of expressions –Types of variables are modeled by an environment

One Pass Type Checking COOL type checking can be implemented in a single traversal over the AST Type environment is passed down the tree –From parent to child Types are passed up the tree –From child to parent

Implementing Type Systems O, M, C  e1 : Int O, M, C  e2 : Int O, M, C  e1+e2 : Int [Add] TypeCheck(Environment, e1, e2) { T1 = TypeCheck(Environment, e1); T2 = TypeCheck(Environment, e2); check T1 == T2 == Int; return INT’ }