Type Checking- Contd 66.648 Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

Intermediate Code Generation
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
COEN Expressions and Assignment
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Type Checking.
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
Compiler Construction
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Fall 2003CS416 Compiler Design1 Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks –Static – done during.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter 6 Type Checking Section 0 Overview
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Type Checking  Legality checks  Operator determination  Overload resolution.
Chapter 15: Operator Overloading
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are.
Imperative Programming
Types and Inheritances Compiler Design Lecture (03/04//98) Computer Science Rensselaer Polytechnic.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Arithmetic Expressions
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
ISBN Chapter 7 Expressions and Assignment Statements.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Classes, Interfaces and Packages
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Expressions and Assignment Statements
Compiler Design – CSE 504 Type Checking
Type Checking and Type Inference
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
College of Computer Science and Engineering
ML’s Type Inference and Polymorphism
Chapter 7 Expressions and Assignment Statements.
Compiler Construction
Polymorphism Programming Language Design and Implementation
Polymorphism Programming Language Design and Implementation
ML’s Type Inference and Polymorphism
Polymorphism Programming Language Design and Implementation
Compiler Construction
Polymorphism Programming Language Design and Implementation
Presentation transcript:

Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic

Lecture Outline Types and type expressions Types and type expressions Unification Unification Administration Administration

Type Expressions We are in chapter 6 of the text book. Please read that chapter. Equivalence of Type Expressions: The checking rules have the form: if two type expressions are equal then return a certain type else return type-error. There is an interaction between the notion of equivalence of types and representation of types and we have to take both of them together.

Structural Equivalence As long as type expressions are built from basic types and constructors, a notion of equivalence between two type expressions is structural equivalence. Two type expressions are structurally equivalent iff they are identical. Ex: pointer(Integer) is equivalent to pointer(Integer).

Modifications of the notion of structural equivalence are needed to reflect the actual type checking of the source language. (array bounds may not be part of the types -But in Java, array bound checking is done) Type ConstructorEncoding pointer01 array10 freturns11 Type Checking

The basic types are encoded using 4 bits as follows: Basic TypesEncoding Boolean0000 char0001 Integer0010 Real0011 char pointer(freturns(char)) Encoding-Contd

Type Equivalence boolean sequival (s, t) { if s and t are the same basic type return true; else if s=array(s1,s2) and t=array(t1,t2) then return sequival(s1,t1) and sequival(s2,t2) else if s=s1xs2 and t=t1xt2 then return sequival(s1,t1) and sequival(s2,t2) else if s= pointer(s1) and t=pointer(t1) then return sequival(s1,t1) else if s=s1->s2 and t=t1->t2 then return sequival(s1,t1) and sequival(s2,t2) else return false }

Type Equivalence with Encoding With the encoding testing of structural equivalence becomes simpler. Languages such as Java use type signatures to state about the return types. As against structural type equivalence, there is also the notion of name type equivalence. (This happens in cases where types are given names). C and Java requires type names to be declared before they are used.

Type Coersions X + I x is real and I is integer. X I into real real + (The language definition specifies what conversions are necessary.) Type conversions occur with the overloading of operators. Coersions: Conversion is implicit when is done by the compiler. Conversion is said to be explicit

Function Overloading If the programmer must write something to cause the conversion. An overloaded symbol is one that has different meanings on its context. Set of possible types for a subexpression: *: int x int -> int *: realxreal -> real *:complex x complex -> complex

Narrowing of Types A complete expression has a unique type. Given a unique type from the context, we can narrow down the type choices for each subexpression. function add(I,j) = I+j end. These will cause a type error. Because, we cannot narrow the type. However, if we state function add(I:int,j) = I+j end.

Polymorphic Functions An ordinary procedure allows the statements in its body to be executed with arguments of fixed type. When a polymorphic procedure is invoked, the statements in its body can be executed with arguments of different types. Ex: Built in operators for indexing operators, applying functions and manipulating pointers.

Type Variables Variables representing type expressions allow one to talk about unknown types. An important application of type variables is checking consistent usage of identifiers in a language that does not require identifiers to be declared before they are used. (In Java, c identifiers have to be declared before they are used.) Ex: sml, lisp..

Type Inference Type inference is the problem of determining the type of a language construct from the way it is used. Ex: Fun add(I,j) = I+j+2 end. Fun deref(p) = return *p.

Type Inference Techniques of type inference and type checking have a lot in common. The methodology of type inference is similar to the inferences used in Artificial Intelligence. Type expressions are similar to arithmetic expressions with constants being basic types. There are notions of substitutions, instances and unification. By substitution, we mean whenever an expression s appears in t replace s in t with something else.

Substitutions Type_Expr subst(Type_Expr t) { if t is a basic type return t else if t is a variable return s(t) else if t is t1 -> t2 return subst(t1)->subst(t2) }Ex: poniter(a) becomes pointer(real) if we substitute a= real.

Instances Pointer(integer) is an instance of pointer(a) Pointer(real) is an instance of pointer(a) Pointer (real) is an instance of a integer->integer is an instance of a->a integer is not an instance of real. Integer-> a is not instance of a->a Integer->real is not an instance of a->a Instance impose an ordering

Unification Two type expressions t1 and t2 unify if there exists some substitutions such that s(t1)=s(t2). We are interested in the most general unifier (I.e., a substitution with the fewest constraints on the variables in the expressions.) 1. s(t1) = s(t2) and 2. For any other substitution s’ such that s’(t1)=s’(t2), the substitution s’ is an instance of s.

Comments and Feedback Project 2 is out. Please start working. PLEASE do not wait for the due date to come. We are in chapter 6. We will finish the rest of chapter 6 in the next class. Please keep studying this material. It may look difficult.