Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.

Similar presentations


Presentation on theme: "Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden."— Presentation transcript:

1 Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden

2 Lecture Outline Type Equivalence Type Checking Type Conversion 2

3 Aggregate Data Types Arrays Records Unions Pointers 3

4 Type Equivalence When are two types the same Structural equivalence Declaration equivalence Name equivalence 4

5 Structural Equivalence Two types are the same if they have the same structure i.e. they are constructed in exactly the same way using the same type constructors from the same simple types 5

6 Structural Type Equivalence 6 typedef int anarray[10]; typedef struct { anarray x; int y;} struct1; typedef struct { int x[10]; int y; }struct2; typedef int anarray[10]; typedef struct { anarray a; int b; }struct3; typedef int anarray[10]; typedef struct { int b; anarray a; }struct4;

7 Structural Equivalence Check representing types as trees ◦ Check equivalence recursively on subtrees Consider… Dynamic arrays 7 Type array1 = array[-1..9] of integer; array2 = array[0..10] of integer; Array (INTEGER range <>) of INTEGER

8 Name Equivalence Two name types are equivalent only if they have the same name Name equivalence is available in Ada and C 8 typedef int ar1[10]; typedef ar1 ar2; typedef int age; type ar1 is array (INTEGER range1..10) of INTEGER; type ar2 is new ar1; type age is new INTEGER;

9 Name equivalence… variable1: ar1; variable2: ar1; variable3: ar2; 9 variable4: array (INTEGER range 1..100) of INTEGER; variable5: array (INTEGER range 1..100) of INTEGER; v6,v7: array (INTEGER range 1..100) of INTEGER;

10 Declaration Equivalent Lead back to the same original structure declaration via a series of redeclarations 10 type t1 = array [1..10] of integer; t2 = t1; t3 = t2; type t4 = array [1..10] of integer; t5 = array [1..10] of integer;

11 Type Checking Involves the application of a type equivalence algorithm to expressions and statements to determine if they make sense Any attempt to manipulate a data object with an illegal operation is a type error Program is said to be type safe (or type secure) if guaranteed to have no type errors Static versus dynamic type checking Run time errors 11

12 Type Checking… Strong typing and type checking ◦ Strong guarantees type safety Statically typed versus dynamically typed ◦ Static (type of every program expression be known at compile time)  All variables are declared with an associated type  All operations are specified by stating the types of the required operands and the type of the result A statically typed language is strongly typed 12

13 Type Checking …and type inference ◦ Types of expressions are inferred from types of their subexpressions  E1 + E2 ◦ In a function call  Type checking part (match actual and formal parameters  Type inference part (determine the result type of the call) Close interaction with the type equivalence algorithm 13

14 Type Checking… MODULA2 has declaration equivalence What is wrong with this? PROCEDURE p(ar:ARRAY [1..max] OF INTEGER); 14

15 Type Checking TYPE artype = ARRAY [1..max] of INTEGER; PROCEDURE p(ar: artype); 15

16 Type Conversion r is float and j is integer r = j + 45.6 i is integer and j is integer i = j + 45.6 16

17 Type Conversion… Modula2 i := TRUNC (FLOAT(j) + 45.6) Explicit type conversion ◦ type conversion functions Implicit conversion ◦ coercion ◦ can weaken type checking 17

18 Type conversion… Casts ◦ A value or object of one type is preceded by a type name (int) 3.14 Often does not cause a conversion to take place. Internal representation is reinterpreted as a new type CARDINAL(-1) 65535 18


Download ppt "Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden."

Similar presentations


Ads by Google