Type Checking.

Slides:



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

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Names and Bindings.
Chapter 7:: Data Types Programming Language Pragmatics
1 Introduction to Data Types (Section 7.1) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
COEN Expressions and Assignment
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Compiler Construction
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Chapter 6 Type Checking Section 0 Overview
Type Checking  Legality checks  Operator determination  Overload resolution.
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.
Semantics for MinML COS 441 Princeton University Fall 2004.
1 CSC241: Object Oriented Programming Lecture No 07.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Computer Science 101 Introduction to Programming.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
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
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
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.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Names, Scope, and Bindings Programming Languages and Paradigms.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Semantic Analysis Type Checking
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Principles of programming languages 8: Types
CS 326 Programming Languages, Concepts and Implementation
Lecture 16: Introduction to Data Types
Type Systems Terms to learn about types: Related concepts: Type
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
Names and Binding In Text: Chapter 5.
Chapter 7 Expressions and Assignment Statements.
Compiler Construction
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
PRESENTED BY ADNAN M. UZAIR NOMAN
Compiler Construction
Types and Related Issues
Presentation transcript:

Type Checking

Type Checking? Type checking is the process of verifying that each operation executed in a program respects the type system of the language. This generally means that all operands in any expression are of appropriate types and number. Much of what we do in the semantic analysis phase is type checking.

Designing a Type Checker When designing a type checker for a compiler, here’s the process: 1.Identify the types that are available in the language 2.Identify the language constructs that have types associated with them 3. Identify the semantic rules for the language

Cond… If a problem is found, e.g., one tries to add a char to a double in C, we encounter a type error. A language is considered strongly- typed if each and every type error is detected during compilation. Type checking can be done compilation, during execution, or divided across both.

Static type checking Static type checking is done at compile-time. The information the type checker needs is obtained via declarations and stored in a master symbol table. After this information is collected, the types involved in each operation are checked.

Cont… For example, if a and b are of type int and we assign very large values to them, a * b may not be in the acceptable range of ints, or an attempt to compute the ratio between two integers may raise a division by zero. These kinds of type errors usually cannot be detected at compiler time.

Dynamic type checking Dynamic type checking is implemented by including type information for each data location at runtime. For example, a variable of type double would contain both the actual double value and some kind of tag indicating "double type". The execution of any operation begins by first checking these type tags. The operation is performed only if everything checks out. Otherwise, a type error occurs and usually halts execution.

Cont… For example, when an add operation is invoked, it first examines the type tags of the two operands to ensure they are compatible. LISP is an example of a language that relies on dynamic type checking.

Implicit type conversion Many compilers have built-in functionality for correcting the simplest of type errors. Implicit type conversion, or coercion, is when a compiler finds a type error and then changes the type of the variable to an appropriate type. Ada and Pascal, for example, provide almost no automatic coercions, requiring the programmer to take explicit actions to convert between various numeric types.

Where type checking is needed? Assignments: When a variable is given a value by an assignment, it must be verified that the type of the value is the same as the declared type of the variable. Overloading: The same name is used for several different operations over several different types.

Cont… Polymorphism / Generic types: Some languages allow a function to be poly-morphic or generic, that is, to be defined over a large class of similar types, e.g. over all arrays no matter what the types of the elements are.

Cont… Data structures: A data structure may define a value with several components(e.g., a struct, tuple or record), or a value that may be of different types at different times (e.g., a union, variant or sum). To type-check such structures, the type checker must be able to represent their types. The type checker may need a data structure that describes complex types.

Cont… Type conversion: A language may have operators for converting a value of one type to a value of another type, e.g. an integer to a floating point number. Some-times these operators are explicit in the program and hence easy to check. How-ever, many languages allow implicit conversion of integers to floats, such that, for example, 3+3.12 is well-typed with the implicit assumption that the integer 3 is converted to a float before the addition.

Cont… Implicit types: Some languages (like Standard ML and Haskell) require pro-grams to be well-typed, but do not require explicit type declarations for variables or functions. For such to work, a type inference algorithm is used. A type in-reference algorithm gathers information about uses of functions and variables and uses this information to infer the types of these. If there are inconsistent uses of a variable, a type error is reported.

Case Study: ML Data Type ML, or Meta-Language, is an important functional language developed in Edinburgh in the 1970’s. It was developed to implement a theorem prover, but recently, it has gained popularity as a general purpose language. ML deals with data types in a novel way. example: fun mult x = x * 10; Requires no type information because ML infers that x is an integer since it is being multiplied by an integer.

Cont.. The only time a programmer must supply a declaration is if ML cannot infer the types. For example, fun sqr x = x * x; Would result in a type error because the multiplication operator is overloaded, i.e., there exist separate multiplication operations for reals and for integers. so the programmer would have to clarify: fun sqr x:int = x * x;

Type Checking Declarations Type Checking Expressions Type Checking Declarations