Title: COMP313A Programming Languages
1COMP313A Programming Languages
2Overview
- Type Constructors
- Type Equivalence
- Type Checking
- Type Conversion
3Constructors 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
4Constructors Mapping (arrays)
typedef int little_people_num3 little_people_n
um gollum_city 0, 0, 0 gollum_city3
5 typedef int matrix1020
5Constructors Mapping (arrays)
Type intarray array2..5 of integer
little_people (dwarf, elf, goblin)
little_people_num arraylittle_people of
integer intmatrix array1..10,
1..20 of integer
xarray(INTEGER range 2.6) of INTEGER
(0,2,0,5,-33)
6Constructors Union
- Cartesian products conjunction of fields
- Union disjunction of fields
- Discriminated or undiscriminated
7Constructors Unions
typedef union short int offset long
unsigned int absolute address
8Constructors Union
typedef struct address location
descriptor kind
safe_address
enum descriptorrel, abs safe_address
an_address if (an_address rel)
an_address.location.offset 255
9type address_range 0..maxint
address_type (absolute, offset)
safe_address record
case kind address_type of
absolute
(abs_addraddress_range)
offset (off_addr integer)
end
10Constructors 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
11Constructors Pointer and Recursive Types
C/C
Ada
typedef struct int val
int_list next int_list
int-list head
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
12Constructors Pointer and Recursive Types
typedef struct int val
int_list next int_list
int-list head
13Constructors 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)
14Constructors 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
15Type Equivalence
- When are two types the same
- Structural equivalence
- Declaration equivalence
- Name equivalence
16Structural 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
17Structural Equivalence
typedef int A5 typedef A B typedef int
C5 typedef int D10
18Structural Type Equivalence
(Note we are just using the syntax of C as an
example. C does NOT use structural equivalence
for structs
typedef int anarray10 typedef struct
anarray x int y struct1
typedef struct int x10
int y struct2
typedef int anarray10 typedef struct int
b anarray a struct4
typedef int anarray10 typedef struct
anarray a int b struct3
19Structural Equivalence
- Check representing types as trees
- Check equivalence recursively on subtrees
- Consider
- Dynamic arrays
Type array1 array-1..9 of integer
array2 array0..10 of integer
Array (INTEGER range ltgt) of INTEGER