Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs 2
Most programming languages provide: A set of simple data entities such as Integer Real Boolean Char Mechanisms for constructing new data entities from these. 3
Finiteness: Mathematically, integer data is thought of as:, -3, -2, -1, 0, 1, 2, 3, In a computer s hardware, there is always an upper and lower bound for values of integers. This is due to a given representation size in bits of an integer on a specific machine. 4
Different language designs deal with data type information in different ways. Some languages mandate that type information be explicit in the programming language. This is used to verify program correctness prior to execution. Example: int x; float y; char z; 5
Scheme is a language with no explicit types or translation-time typing. Ada is a very strictly-typed language. It performs strict type checking. 6
Some form of static (translation-time) type checking is very useful to: Allocate memory efficiently. Reduce the amount of code to be compiled. Improves both writability and readability. Improves security and reliability. Removes ambiguities. Can be used to prove program correctness. 7
A Data Type is a Set of Values Insufficient Definition! 8
A Data Type is a Set of Values + A Set of Operations on Those Values having Certain Properties 9
Example: The values of an int range from a lower bound to an upper bound (e.g. MININT to MAXINT) Operations on int include: + - * / Etc. 10
Type Checking: Data Types Is the process a translator goes through to determine whether the type information in a program is consistent. 11
Type Checking: Data Types Example: x = y + z Can the type of the result of y + z be stored in x? The type of y + z depends on the type of y and z Determining the type of y + z is called type inference. 12
We can construct more complex user defined types from simple data types: Example: An array of ten integers in C/C++: int a[10] The same array in Ada: a: array (1..10) of integer 13
Type definitions: In some languages, we can give new types names. Example in C: typedef int Array_of_ten_integers[10]; Array_of_ten_integers a; A name given to an int array of ten elements Just like we declare int a, We can declare Array_of_ten_integers a 14
Type definitions: In some languages, we can give new types names. Example in C: typedef int Array_of_ten_integers[10]; Array_of_ten_integers a; A name given to an int array of ten elements Just like we declare int a, We can declare Array_of_ten_integers a 15
Type definitions: In some languages, we can give new types names. Example in Ada: type Array_of_ten_integers is array(1..10) of integer; a: Array_of_ten_integers; 16
Type definitions: With new type definitions, we have a problem! During type checking, a translator must often compare two types to determine if they are the same. Example: The following should be equivalent in type! int a[10] Array_of_ten_integers 17
Type definitions: Data Types Each language with type declarations has rules for checking equivalence. These are called rule equivalence algorithms. 18
Strongly Typed Languages: If a programming language definition specifies a complete type system that can be applied statically. Guarantees that all (unintentional) data-corrupting errors in a program will be detected at the earliest possible point. The language is said to be strongly typed. 19
Strongly Typed Languages: All type errors are detected at translation time. With the exception of a few errors that can only be checked during execution. E.g. array subscript bounds 20
Strongly Typed Languages: Strong typing ensures that: Most unsafe programs (programs with data corrupting errors) will be rejected at translation time. The unsafe programs that are not rejected at translation time will cause an execution error prior to any data corrupting actions. Strong typing can also be a burden on the programmer, having to explicitly specify types all the time. 21
Strongly Typed Languages (Examples): Ada is a strongly typed language. Strong ML and Haskell Pascal C (Considered weakly typed) Weak 22
Untyped Languages: Languages without static type checking. Also called dynamically typed languages. Examples: Scheme, Smalltalk, most scripting languages such as Perl. An untyped language does not necessarily allow programs to corrupt data, but rather all safety checking is performed at execution time: They will generate runtime errors. 23
Type Construction: Data Types How are new types constructed? We can think of construction as mathematical operations. 24
Type Construction: Data Types Cartesian product. Unions. Subsets. Arrays/Functions. Sequences/Lists. Recursive types. No Intersection: types should NOT overlap. 25
Type Construction: Cartesian product: Data Types Finite combinations of previously defined types. Example: C structs: struct IntCharReal { int i; char c; double r; }; A finite combination of those types 26
Type Construction: Unions: Values belong to ONE of a finite set of types. Example: C unions: union IntOrReal { }; int i; double r; Only one of those can be used. 27
Type Construction: Data Types Subsets: A new type may be a subrange of another type. Example in Ada: subtype IntDigit_Type is integer range 0..9 28
Type Construction: Data Types Subsets: A new type may be a subrange of another type. Example in Ada: subtype IntDigit_Type is integer range 0..9 29
Type Construction: Data Types Arrays and Functions: You can define arrays in most programming languages as a homogeneous collection of a given type. You can define functions also. 30
Type Construction: Data Types Sequences and Lists: Sequences are like arrays, except that they are potentially infinite. Example: Streams Lists are like arrays, but can only be accessed by counting down from the first element. Almost all functional languages have lists. 31
Type Construction: Data Types Recursive Types: Example: class node { int value; node next; } Recursive Definition 32
Example: Java Type Structure: Java Types Primitive Reference boolean Numeric Array class interface Integral char byte short int long Floating point float double 33
Example: C Type Structure: C Types Basic Derived void Numeric Pointer Array Function struct union Integral (signed) (unsigned) char int short int long int enum Floating float double long double 34
Type Checking: Dynamic type checking: If type information is maintained and checked at runtime. Interpreters by definition perform dynamic type checking. Compilers also generate code that maintain type attributes during runtime in table. Dynamic type checking is required when the types can only be determined at runtime. Static type checking: The types are determined from the text of the program. Checking is performed before execution. 35
Type Checking: A strongly typed language must report all type errors as compilation errors that prevent execution. A language definition may not specify whether static or dynamic type checking is to be used. 36
Type Compatibility: Data Types Two different types may be combined correctly in certain ways. They are called compatible. Example: int, short. What do we mean by combined correctly in a certain way? Ranges! 37
Implicit Types: Sometimes types are implicitly inferred. Example: 2 + 3, or x / y In C, the following is an implicit int variable X; Also, in C, if functions have no return type indicated, they implicitly return an int. 38
Overlapping Types: Data Types It is sometimes difficult to avoid overlapping of various types. Example: In Java, the following types overlap: int short long byte All overlap 39
Shared Operations: Data Types The + operator for example may be applied to any combination of int, short, byte, double, float, long. The + operator even has a different meaning in: System.out.println( My + Name + Is ); The plus operator is said to be overloaded. 40
Type Conversion: There is always a need to convert from one type to another. Conversion can either be implicit (automatic) or explicit (manual). 41
Type Conversion: Example of implicit type conversion in C/C++: int x = 3.3; Although x is an int, 3.3 is truncated to 3, and stored in x. Example of implicit type conversion in Java: double x = 2 + 3; The expression s type is automatically promoted to double to be stored in x. Implicit conversions are also called coercions. 42
Type Conversion: When we implicitly convert from int to double for example, the destination is large enough to accommodate any int. This is called a widening conversion. The opposite is called a narrowing conversion. 43
Polymorphic Type Checking Most statically typed languages require that explicit type information be given for all names in each declaration. It is possible to use an extension of type inference to determine the types of names in a declaration without explicitly giving those types. The translator can collect information on uses of a name, and infer from the set of all uses a probable type (the most general type) for which all uses are correct. The Hindley-Milner type checking provides this kind of inference. It is a major feature of strongly typed functional languages such as ML and Haskell. 44
Type Conversion: Explicit conversions involve the writing of directives to explicitly convert from one type to another in the code. Example: int x = 2; double y = 3.3; y = (double) x; (type casting) Type casting will temporarily change the type of the RHS to that of the cast. 45