1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type...

19
1 COMP313A Programming Languages Data Types (2)

Transcript of 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type...

Page 1: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

1

COMP313A Programming Languages

Data Types (2)

Page 2: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

2

Overview

• Type Constructors

• Type Equivalence• Type Checking• Type Conversion

Page 3: 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

Page 4: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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];

Page 5: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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)

Page 6: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

6

Constructors Union

• Cartesian products – conjunction of fields• Union – disjunction of fields• Discriminated or undiscriminated

Page 7: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

7

Constructors Unions

• Undiscriminated C/C++

typedef union {short int offset;

long unsigned int absolute; } address;

Page 8: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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;

Page 9: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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

Page 10: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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

Page 11: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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

Page 12: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

12

Constructors Pointer and Recursive Types

• Lacks …….

typedef struct { int val; int_list* next; } int_list; int-list* head

Page 13: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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)…

Page 14: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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

Page 15: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

15

Type Equivalence

• When are two types the same• Structural equivalence• Declaration equivalence• Name equivalence

Page 16: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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

Page 17: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

17

Structural Equivalence

typedef int A[5]

typedef A B;

typedef int C[5];

typedef int D[10];

Page 18: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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

Page 19: 1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.

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