Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems."— Presentation transcript:

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

2 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

3 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

4 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.

5 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.

6 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

7 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

8 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.

9 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

10 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 ) = 3.14159 * 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).

11 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”

12 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}

13 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.

14 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

15 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.

16 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 1..100; count : Integer; index : Indextype; Under name compatibility, the variables count and index are not compatible.

17 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;

18 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.

19 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; };

20 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[1.. 100]; Array2 = array[0.. 99];

21 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}


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

Similar presentations


Ads by Google