1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type...
-
Upload
darcy-lane -
Category
Documents
-
view
215 -
download
1
Transcript of 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type...
1
COMP313A Programming Languages
Data Types (2)
2
Overview
• Type Constructors
• Type Equivalence• Type Checking• Type Conversion
3
Constructors – Mapping (arrays)
• The array constructor defines mappings as data aggregates
• Mapping from an array index to the value stored in that position in the array
• The domain is the values of the index• The range is the values stored in the array
4
Constructors Mapping (arrays)
• C/C++
typedef int little_people_num[3];
little_people_num gollum_city = {0, 0, 0}
gollum_city[3] = 5
typedef int matrix[10][20];
5
Constructors – Mapping (arrays)…
• Pascal
• ADA
Type intarray = array[2..5] of integer; little_people = (dwarf, elf, goblin); little_people_num = array[little_people] of integer;
intmatrix = array[1..10, 1..20] of integer;
x:array(INTEGER range 2.6) of INTEGER := (0,2,0,5,-33)
6
Constructors Union
• Cartesian products – conjunction of fields• Union – disjunction of fields• Discriminated or undiscriminated
7
Constructors Unions
• Undiscriminated C/C++
typedef union {short int offset;
long unsigned int absolute; } address;
8
Constructors Union…
• Discriminated
typedef struct { address location; descriptor kind; } safe_address;
enum descriptor{rel, abs};
safe_address an_address;
if (an_address == rel) an_address.location.offset = 255;
9
• Pascal Variant Record
type address_range = 0..maxint; address_type = (absolute, offset); safe_address = record case kind: address_type of absolute: (abs_addr:address_range); offset: (off_addr: integer); end
10
Constructors Pointer and Recursive Types
• Recursion used to define aggregates whose size can grow arbitrarily
• Recursive datatype T is defined as a structure that can contain components of type T
• Implemented via pointers
11
Constructors Pointer and Recursive Types…
• Linked list of integers
typedef struct { int val; int_list* next; } int_list; int-list* head
C/C++ Ada
type int_list_node;type int_list_ref is access node int_list_node;type int_list_node is record val: integer; next: int_list_ref; end;
head: int_list_ref
12
Constructors Pointer and Recursive Types
• Lacks …….
typedef struct { int val; int_list* next; } int_list; int-list* head
13
Constructors Pointer and Recursive Types
• Think of the the different possibilities for an int_list as:
{emptylist} U int U (int X int) U (int X int X int)…
14
Constructors Pointer and Recursive Types
• Some languages (Pascal, Ada) require pointers to be typed
• PL/1 treated pointers as untyped data objectsWhat is the significance of this for a type checker?
• C pointers are typed but C allows arithmetic operations on them unlike Pascal and Ada
15
Type Equivalence
• When are two types the same• Structural equivalence• Declaration equivalence• Name equivalence
16
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
17
Structural Equivalence
typedef int A[5]
typedef A B;
typedef int C[5];
typedef int D[10];
18
Structural Type Equivalence
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;
(Note we are just using the syntax of C as an example. C does NOT use structural equivalence for structs
19
Structural Equivalence
• Check representing types as trees – Check equivalence recursively on subtrees
• Consider…
• Dynamic arrays
Type array1 = array[-1..9] of integer; array2 = array[0..10] of integer;
Array (INTEGER range <>) of INTEGER