Type Analysis. Type Checking vs. Type Inference

Similar documents
CSE 431S Type Checking. Washington University Spring 2013

Type Checking. Error Checking

Inheritance and object compatibility

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

Type systems. Static typing

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

CSE 307: Principles of Programming Languages

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

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

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

CS-202 Introduction to Object Oriented Programming

Object typing and subtypes

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

G Programming Languages - Fall 2012

CSE 307: Principles of Programming Languages

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

Objects, Subclassing, Subtyping, and Inheritance

ITI Introduction to Computing II

Object-oriented Programming. Object-oriented Programming

Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

ITI Introduction to Computing II

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

Java Object Oriented Design. CSC207 Fall 2014

What are the characteristics of Object Oriented programming language?

Intermediate Code Generation Part II

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

C++ Yanyan SHEN. slide 1

Fundamentals of Programming Languages

Tokens, Expressions and Control Structures

Types and Classes. I II From predefined (simple) and user-defined (composite) types via Abstract data types

Type Checking and Type Inference

index.pdf January 21,

Instantiation of Template class

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS250 Final Review Questions

CS250 Final Review Questions

OBJECT ORIENTED PROGRAMMING

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

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

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

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

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Dynamic Binding C++ Douglas C. Schmidt

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CSc 520. Principles of Programming Languages 45: OO Languages Introduction

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

User-Defined Algebraic Data Types

Programming Languages

ITI Introduction to Computing II

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Quiz. Programming Languages. CSE 130 : Fall Lecture 16: Static Types for Objects. Ranjit Jhala UC San Diego

Concepts Introduced in Chapter 6

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Semantic Analysis with Attribute Grammars Part 5

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Programming Languages

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

The compilation process is driven by the syntactic structure of the program as discovered by the parser

ITI Introduction to Computing II

Practice for Chapter 11

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

CSE 307: Principles of Programming Languages

CS558 Programming Languages

Inheritance. Transitivity

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

JAVASCRIPT BASICS. Type-Conversion in JavaScript. Type conversion or typecasting is one of the very important concept in

Lecture 10: building large projects, beginning C++, C++ and structs

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Inheritance. Inheritance

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Rules and syntax for inheritance. The boring stuff

Chapter 10 Object-Oriented Programming

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Static Checking and Type Systems

VALLIAMMAI ENGINEERING COLLEGE

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

Information System Design (IT60105)

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

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

Transcription:

Type Analysis Is an operator applied to an incompatible operand? Type checking: Static: Check for type compatibility at compile time Dynamic: Check for type compatibility at run time Type analysis phase also used to resolve fields in a structure: Example: list.element Compilers Types CSE 304/504 1 / 25 Type Checking vs. Type Inference A Type Checker only verifies that the given declarations are consistent with their use. Examples: type checkers for Pascal, C. A Type Inference system generates consistent type declarations from information implicit in the program. Examples: Type inference in SML, Scheme. Given y = 3.1415 * x * x, we can infer that y is a float. Compilers Types CSE 304/504 2 / 25

Why Static Type Checking? Catch errors at compile time instead of run time. Determine which operators to apply. Example: In x + y, + is integer addition if x and y are both integers. Recognize when to convert from one representation to another (Type Coercion). Example: In x + y, if x is a float while y is an integer, convert y to a float value before adding. Compilers Types CSE 304/504 3 / 25 Type Checking: An Example E int const { E.type = int; E float const { E.type = float; E E 1 + E 2 { if E 1.type == E 2.type == int E.type = int; else E.type = float; Compilers Types CSE 304/504 4 / 25

Type Checking: Another Example E int const { E.type = int; E float const { E.type = float; E id { E.type = sym lookup(id.entry, type); E E 1 + E 2 { if (E 1.type {int, float) OR (E 2.type {int, float) E.type = error; else if E 1.type == E 2.type == int E.type = int; else E.type = float; Compilers Types CSE 304/504 5 / 25 Types Base types: atomic types with no internal structure. Examples: int, char. Structured types: Types that combine (collect together) elements of other types. Arrays: Characterized by dimensions, index range in each dimension, and type of elements. Records: (structs and unions) Characterized by fields in the record and their types. Compilers Types CSE 304/504 6 / 25

Type Expressions Language to define types. Type int float char... void error name array(type) record( (name, Type) ) pointer(type) tuple( (Type) ) arrow( Type, Type) Compilers Types CSE 304/504 7 / 25 Examples of Type Expressions float xform[3][3]; xform array(array(float)) char *string; string pointer(char) struct list { int element; struct list *next; l; list record( (element, int), (next, pointer(list) )) l list int max(int, int); max arrow( tuple( int, int), int) Compilers Types CSE 304/504 8 / 25

Type Checking with Type Expressions E E 1 [ E 2 ] { if E 1.type == array(t) AND E 2.type == int E.type = T else E.type = error E * E 1 { if E 1.type == pointer(t) E.type = T else E.type = error E & E 1 { E.type = pointer(e 1.type) Compilers Types CSE 304/504 9 / 25 Functions and Operators Functions and Operators have Arrow types. max: int int int sort: numlist numlist Functions and operators are applied to operands. max(x,y): max : int int int x : int y : int (x, y) : int int max(x, y) : int Compilers Types CSE 304/504 10 / 25

Function Application E E 1 E 2 { if E 1.type arrow(s, T) AND E 2.type S E.type = T else E.type = error E ( E 1, E 2 ) { E.type = tuple(e 1.type, E 2.type) Compilers Types CSE 304/504 11 / 25 Type Equivalence When are two types equal? type Vector = array [1..10] of real; type Weights = array [1..10] of real; var x, y: Vector; z: Weight; Name Equivalence: When they have the same name. x and y have same type, but z has different type. Structural Equivalence: When they have the same structure. x, y and z have same type. Compilers Types CSE 304/504 12 / 25

Structural Equivalence S T iff: S and T are the same basic type; S = array(s 1 ), T = array(t 1 ), and S 1 T 1. S = pointer(s 1 ), T = pointer(t 1 ), and S 1 T 1. S = tuple(s 1, S 2 ), T = tuple(t 1, T 2 ), and S 1 T 1 and S 2 T 2. S = arrow(s 1, S 2 ), T = arrow(t 1, T 2 ), and S 1 T 1 and S 2 T 2. Compilers Types CSE 304/504 13 / 25 Subtyping Object-oriented languages permit subtyping. class Rectangle { private int x,y; int area() {... class Square extends Rectangle {... Square is a subclass of Rectangle. Since all methods on Rectangle are inherited by Square (unless explicitly overridden) Square is a subtype of Rectangle. Compilers Types CSE 304/504 14 / 25

Inheritance class Circle { float x, y; // center float r; // radius float area() { return 3.1415 * r * r; class ColoredCircle extends Circle { Color c; class Test{ static main() { ColoredCircle t;... t.area()... Compilers Types CSE 304/504 15 / 25 Resolving Names What entity is represented by t.area()? (assume no overloading) Determine the type of t. t has to be of type user(c). If c has a method of name area, we are done. Otherwise, if the superclass of c has a method of name area, we are done. Otherwise, if the superclass of superclass of c... = Determine the nearest superclass of class c that has a method with name area. Compilers Types CSE 304/504 16 / 25

Overloading class Rectangle { int x,y; // top lh corner int l, w; // length and width Rectangle move() { x = x + 5; y = y + 5; return this; Rectangle move(int dx, int dy) { x = x + dx; y = y + dy; return this; Compilers Types CSE 304/504 17 / 25 Resolving Overloaded Names What entity is represented by move in r.move(3, 10)? Determine the type of r. r has to be of type user(c). Determine the nearest superclass of class c that has a method with name move such that move is a method that takes two int parameters. Compilers Types CSE 304/504 18 / 25

Structural Subtyping S T iff: S and T are the same basic type. S = user(type 1 ), T = user(type 2 ) and type 1 type 2. S = array(s 1 ), T = array(t 1 ), and S 1 T 1 ; S = pointer(s 1 ), T = pointer(t 1 ), and S 1 T 1 ; S = tuple(s 1, S 2 ), T = tuple(t 1, T 2 ), and S 1 T 1 and S 2 T 2 ; S = arrow(s 1, S 2 ), T = arrow(t 1, T 2 ), and S 1 T 1 and T 2 S 2. Compilers Types CSE 304/504 19 / 25 Inheritance and Overloading What entity is represented by f in E.f(a 1, a 2,..., a n )? Let the type of E be user(c). f is the method in the nearest superclass of class c such that type of f is a supertype of type(a 1 ) type(a 1 ). Compilers Types CSE 304/504 20 / 25

Inheritance: Another Example graphical object translate scale polyline length closed graphical area polygon area ellipse rectangle area triangle Compilers Types CSE 304/504 21 / 25 Abstract objects and Concrete Representations Abstract classes declare methods, but do not define them. Example: closed graphical declares area method, but cannot define the method. The different area methods are defined when the object s representations are concrete: in rectangle, ellipse, etc. When area method is applied to an object of class closed graphical, we method to be called is the one defined in rectangle, triangle, ellipse, etc.... which can be resolved only at run-time! Compilers Types CSE 304/504 22 / 25

Types in OO Languages: The Whole Story Decaf implements a small part of the type system for an OO language. Subtype rule: Wherever an object of type t is required (as a parameter of a method, return value, or rhs of assignments), object of any subtype s of t can be used. Compilers Types CSE 304/504 23 / 25 Types in OO Languages: The Whole Story (contd.) Method Selection rule: If class B inherits from class tt A and overwrites method m, then for any B object b, method m of B must be used, even if b us used as an A object. class A { int m() {... class C{ int f(b b) { A a; class B extends A { int m() {... a = b;... a.m()... Compilers Types CSE 304/504 24 / 25

Types in OO Languages: The Whole Story (contd.) Dynamic Binding rule: A method of object obj, which can be potentially overwritten in a subclass has to be bound dynamically if the compiler cannot determine the runtime type of obj. Compilers Types CSE 304/504 25 / 25