Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.

Slides:



Advertisements
Similar presentations
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Advertisements

Names and Bindings.
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.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
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.
Type Checking.
CS 355 – Programming Languages
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Names, Bindings, Type Checking, and Scopes
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ISBN Chapter 6 Data Types: Structured types.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
ISBN Lecture 05 Variable Attributes.
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
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.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 © 2002 by Addison Wesley Longman, Inc Names - We discuss all user-defined names here - Design issues for names: - Maximum length? - Are.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Names, Bindings, Type Checking, and Scopes
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
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.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
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.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding In Text: Chapter 4.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
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.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Semantic Analysis Type Checking
CSE 3302 Programming Languages
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
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
Names, Bindings, Type Checking, and Scopes
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Names and Binding In Text: Chapter 5.
Types and Related Issues
Presentation transcript:

Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems

Dr. M. Al-Mulhem Introduction 2 References Textbook Chapter 6 “On Understanding Types, Data Abstraction, and Polymorphism” by Luca Cardelli and Peter Wegner, ACM Computing Surveys, Vol. 17, No. 4, December 1985

Dr. M. Al-Mulhem Introduction 3 A type (commonly known as data type) is a well-defined set of values and operations defined on those values.  Examples Integers Real numbers Boolean

Dr. M. Al-Mulhem Introduction 4 A type system is a well-defined system of associating types with variables and other objects defined in a program. A type error is a run-time error that occurs when an operation is attempted on a value for which it is not well-defined.

Dr. M. Al-Mulhem Introduction 5 A programming language is strongly typed if its type system allows all type errors in programs to be detected either at compile time or at run time but before the actual execution of the statement in which they may occur.

Dr. M. Al-Mulhem Introduction 6 A program language is type safe if no program is allowed to violate its type distinctions. For example, a function has a different type from an integer. Therefore, any language that allows integers to be used as functions is not type safe. The type safety for some languages is: SafetyExample languageExplanation Not SafeC and C++Type casts, pointer arithmetic Almost safePascalExplicit deallocation, dangling pointers SafeLisp, ML, Smalltalk, javaComplete type checking

Dr. M. Al-Mulhem Introduction 7 Two approaches to associate a type with data (type binding) Type declaration (static types) Type inference (dynamic types) Static type systems require that all variables are bound to a type at compile time Java uses static type system A type inference system can be used to infer the types of variables when little or no type information is given explicitly. LISP uses type inference system

Dr. M. Al-Mulhem Introduction 8 Static type systems  Allow type inconsistencies to be discovered at compile time  Facilitates early detection of type errors  Allows greater execution-time efficiency  It enforces a programming discipline on the programmer that makes programs more structured and easier to read But static typing may  Lead to loss of flexibility and expressive power by prematurely constraining the behavior of variables to that associated with a particular type.  exclude generic procedures that capture the structure of an algorithm uniformly applicable to a range of types.

Dr. M. Al-Mulhem Introduction 9 Type inference system  A type inference system can be used to infer the types of variables when little or no type information is given explicitly  A variable in the same program can have multiple types.  Example: Javascript uses assignment statement to infer the type of variables. List=[3.2,4.3]--List is an array … List = 45--List is an integer

Dr. M. Al-Mulhem Introduction 10 Introduction ML, Miranda, and Haskell use the context of the reference to determine the types. For example, the function declaration fun circumf ( r ) = * r * r;  The type of both the parameter and the return value are inferred from the type of the constant in the expression (real) For example, the function declaration fun square ( x ) = x * x;  The type of both the parameter and the return value are inferred from * operator (arithmetic and the default is int).

Dr. M. Al-Mulhem Introduction 11 Type Systems How can we define a type system for a language so that type errors can be detected? Write a set of function specifications that define what it means for a program to be type safe.  Attribute grammar Rules can express ideas like “Type of LHS and RHS of an assignment statement must be the same”

Dr. M. Al-Mulhem Introduction 12 Formalizing the Type System Type map  A set of pairs representing the declared variables and their types tm = {,, … } Formal specification TypeSystem: Declarations  TypeMap TS (Declarations d) =  where i  {1, 2,…,n}

Dr. M. Al-Mulhem Introduction 13 Elementary and User-defined Types Elementary types  Types defined in the language  Examples: int, char User-defined types  Types defined by the user using language constructors.  Examples: Classes in Java, struct in C, type in Pascal and Ada.

Dr. M. Al-Mulhem Introduction 14 Type Compatibility A type is compatible if 1. It is legal for the operator 2. It is allowed to be implicitly converted by compiler generated code to a legal type Two approaches to type compatibility 1. Name type compatibility 2. Structure type compatibility

Dr. M. Al-Mulhem Introduction 15 Name Type Compatibility Name type compatibility means the two variables have compatible types if they are in either the same declaration or in declarations that use the same type name. Example: int a, b; int c Under name type compatibility, a, b, and c are compatibile.

Dr. M. Al-Mulhem Introduction 16 Name Type Compatibility Easy to implement but highly restrictive:  subranges of integer types are not compatible with integer types. Example: Consider the following Ada declaration type Indextype is ; count : Integer; index : Indextype; Under name compatibility, the variables count and index are not compatible.

Dr. M. Al-Mulhem Introduction 17 Structure Type Compatibility Structure type compatibility means that two variables have compatible types if their types have identical structures. Example: Consider the following C declarations: struct employeestruct date{ long number; int month, day, year; char name [MAXNAME];}; char phone [MAXDIGIT]; int age; }; struct employee emp; struct date d;

Dr. M. Al-Mulhem Introduction 18 Structure Type Compatibility More flexible, but harder to implement:  Under name type compatibility, only two type names must be compared to determine compatibility  Under structure type compatibility, the entire structure of the two types must be compared.

Dr. M. Al-Mulhem Introduction 19 Problems with Type Compatibility Consider the following problems of two structured types: Are two record types compatible if they are structurally the same but use different field names? Example: Consider the C declarations: struct employee1 struct employee2{ long number; long ID; char name [MAXNAME]; char phone [MAXDIGIT]; int age; };

Dr. M. Al-Mulhem Introduction 20 Problems with Type Compatibility Are two array types compatible if they are the same except that the subscripts are different? Example: Consider the following Pascal declarations typeArray1 = array[ ]; Array2 = array[0.. 99];

Dr. M. Al-Mulhem Introduction 21 Problems with Type Compatibility Are two enumeration types compatible if their components are spelled differently? Example: Consider the follow C# declarations: enum day1 {Mon, Tue, Wed} enum day2 {Sat, Sun, Mon}