+ All Categories
Home > Documents > Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.

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

Date post: 18-Jan-2018
Category:
Upload: elwin-abraham-mason
View: 234 times
Download: 0 times
Share this document with a friend
Description:
Aggregate Data Types Arrays Records Unions Pointers 3
18
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden
Transcript
Page 1: Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.

Data Types (3)

1

Programming Languages – Principles and Practice by Kenneth C Louden

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

Lecture OutlineLecture Outline

Type EquivalenceType CheckingType Conversion

2

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

Aggregate Data TypesAggregate Data TypesArraysRecordsUnionsPointers

3

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

Type EquivalenceType Equivalence

When are two types the same

Structural equivalenceDeclaration equivalenceName equivalence

4

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

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

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

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;

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

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

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

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;

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

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;

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

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;

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

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

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

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

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

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

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

Type Checking…Type Checking…

MODULA2 has declaration equivalence

What is wrong with this?

PROCEDURE p(ar:ARRAY [1..max] OF INTEGER);

14

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

Type CheckingType CheckingTYPE artype = ARRAY [1..max] of

INTEGER;

PROCEDURE p(ar: artype);

15

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

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

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

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

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

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


Recommended