Date post: | 18-Jan-2018 |
Category: |
Documents |
Upload: | elwin-abraham-mason |
View: | 234 times |
Download: | 0 times |
Data Types (3)
1
Programming Languages – Principles and Practice by Kenneth C Louden
Lecture OutlineLecture Outline
Type EquivalenceType CheckingType Conversion
2
Aggregate Data TypesAggregate Data TypesArraysRecordsUnionsPointers
3
Type EquivalenceType Equivalence
When are two types the same
Structural equivalenceDeclaration equivalenceName equivalence
4
Structural EquivalenceStructural 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
Structural Type Structural Type EquivalenceEquivalence
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;
Structural EquivalenceStructural EquivalenceCheck 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
Name EquivalenceName EquivalenceTwo 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;
Name equivalence…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;
Declaration EquivalentDeclaration 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;
Type CheckingType 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
Type Checking…Type Checking…Strong typing and type checking
◦Strong guarantees type safetyStatically 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 resultA statically typed language is strongly
typed
12
Type CheckingType 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 algorithm13
Type Checking…Type Checking…
MODULA2 has declaration equivalence
What is wrong with this?
PROCEDURE p(ar:ARRAY [1..max] OF INTEGER);
14
Type CheckingType CheckingTYPE artype = ARRAY [1..max] of
INTEGER;
PROCEDURE p(ar: artype);
15
Type ConversionType Conversionr is float and j is integer
r = j + 45.6
i is integer and j is integer
i = j + 45.6
16
Type Conversion…Type Conversion…Modula2
i := TRUNC (FLOAT(j) + 45.6)
Explicit type conversion◦type conversion functions
Implicit conversion ◦coercion◦can weaken type checking
17
Type conversion…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