34
1 Type Checking and Data Type Type Checking and Data Type Implementation Implementation (Sections 7.2- 7.4) (Sections 7.2- 7.4) CSCI 431 Programming Languages CSCI 431 Programming Languages Fall 2003 Fall 2003 A compilation of material A compilation of material developed by Felix Hernandez- developed by Felix Hernandez- Campos and Michael Scott Campos and Michael Scott

1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

Embed Size (px)

Citation preview

Page 1: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

11

Type Checking and Data Type Type Checking and Data Type ImplementationImplementation

(Sections 7.2- 7.4)(Sections 7.2- 7.4)

CSCI 431 Programming LanguagesCSCI 431 Programming Languages

Fall 2003Fall 2003

A compilation of material developed by Felix A compilation of material developed by Felix Hernandez-Campos and Michael ScottHernandez-Campos and Michael Scott

Page 2: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

22

Type CheckingType Checking

• Type equivalenceType equivalence– When are the types of two values the same?When are the types of two values the same?

• Type compatibilityType compatibility– When can a value of type A be used in a context that When can a value of type A be used in a context that

expects type B?expects type B?

• Type inferenceType inference– What is the type of an expression, given the types of the What is the type of an expression, given the types of the

operands?operands?

Page 3: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

33

Type CheckingType Checking

• Type equivalenceType equivalence– When are the types of two values the same?When are the types of two values the same?

• Type compatibilityType compatibility– When can a value of type A be used in a context that When can a value of type A be used in a context that

expects type B?expects type B?

• Type inferenceType inference– What is the type of an expression, given the types of the What is the type of an expression, given the types of the

operands?operands?

Page 4: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

44

Type EquivalenceType Equivalence

• Type equivalence is defined in two principal waysType equivalence is defined in two principal ways– Structural equivalenceStructural equivalence– Name equivalenceName equivalence

• Two types are Two types are structurally equivalentstructurally equivalent if they have if they have identical type structuresidentical type structures

– They must have the same componentsThey must have the same components– E.g.E.g. Fortran Fortran

• Two type are Two type are nominally equivalentnominally equivalent if they have the if they have the same namesame name

– It may or may not include aliasIt may or may not include alias– E.g.E.g. Java Java– Name equivalence is more fashionable these daysName equivalence is more fashionable these days

Page 5: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

55

• Structural equivalence depends on simple Structural equivalence depends on simple comparison of type descriptions substitute out all comparison of type descriptions substitute out all names; expand all the way to built-in types. Original names; expand all the way to built-in types. Original types are equivalent if the expanded type types are equivalent if the expanded type descriptions are the samedescriptions are the same

• Pointers complicate matters, but the Algol folks Pointers complicate matters, but the Algol folks figured out how to handle it in the late 1960's. The figured out how to handle it in the late 1960's. The simple (not quite correct) approach is to pretend all simple (not quite correct) approach is to pretend all pointers are equivalent.pointers are equivalent.

Type EquivalenceType EquivalenceStructural EquivalenceStructural Equivalence

Page 6: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

66

Type EquivalenceType EquivalenceStructural EquivalenceStructural Equivalence

• Does the format of the declaration depend?Does the format of the declaration depend?

typedef struct {typedef struct {

int a, b;int a, b;

} foo2;} foo2;

typedef struct { int a, b; } foo1;typedef struct { int a, b; } foo1;

• All languages consider these two types structurally All languages consider these two types structurally equivalentequivalent

Page 7: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

77

Type EquivalenceType EquivalenceStructural EquivalenceStructural Equivalence

• Is the order in the declaration relevant?Is the order in the declaration relevant?

typedef struct {typedef struct {

int a;int a;

int b;int b;

} foo1;} foo1;

typedef struct {typedef struct {

int b;int b;

int a;int a;

} foo2;} foo2;

• Most languages consider these two types structurally Most languages consider these two types structurally equivalentequivalent

Page 8: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

88

Type EquivalenceType EquivalenceStructural Equivalence PitfallStructural Equivalence Pitfall

• Are these two types structurally equivalent?Are these two types structurally equivalent?

typedef struct {typedef struct {

char *name; char *name; char *address;char *address;int age;int age;

} student;} student;

typedef struct {typedef struct {

char *name; char *name; char *address;char *address;int age;int age;

} school;} school;

• They are, and it is unlikely that the programmer They are, and it is unlikely that the programmer intended to make these two types equivalentintended to make these two types equivalent

Page 9: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

99

Type EquivalenceType EquivalenceName EquivalenceName Equivalence

• Assumes type definitions with different names are Assumes type definitions with different names are not equivalentnot equivalent

– Otherwise, why did the programmer create two Otherwise, why did the programmer create two definitions?definitions?

• It solves the previous problemIt solves the previous problem– studentstudent and and schoolschool are not nominally equivalent are not nominally equivalent

• Aliases:Aliases:– Under Under strict name equivalencestrict name equivalence, aliases are not equivalent, aliases are not equivalent

» type A = Btype A = B is a definition ( is a definition (i.e. i.e. new object)new object)

– Under Under loose name equivalenceloose name equivalence, aliases are equivalent, aliases are equivalent» type A = Btype A = B is a declaration ( is a declaration (i.e.i.e. binding) binding)

Page 10: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

1010

Type EquivalenceType EquivalenceName Equivalence and AliasesName Equivalence and Aliases

• Loose name equivalence may introduce undesired Loose name equivalence may introduce undesired type equivalencestype equivalences

Page 11: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

1111

An ExampleAn Example

Strict name equivalence: Strict name equivalence: types are equivalent if they refer to the same declarationtypes are equivalent if they refer to the same declaration

Loose name equivalenceLoose name equivalencetypes are equivalent if they refer to the same outermost constructor,types are equivalent if they refer to the same outermost constructor,i.e. if they refer to the same declaration after factoring out any type i.e. if they refer to the same declaration after factoring out any type

aliases. aliases.

Example:Example:type alink = pointer to cell;type alink = pointer to cell;subtype blink = alink;subtype blink = alink;p, q : pointer to cell;p, q : pointer to cell;r : alink;r : alink;s : blink;s : blink;u : alink;u : alink;

Page 12: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

1212

Structural equivalence says all five variables have Structural equivalence says all five variables have same type.same type.

Strict name equivalence equates types of p & q and r & Strict name equivalence equates types of p & q and r & u, because they refer back to the same type u, because they refer back to the same type declaration (a constructor on the RHS of a var declaration (a constructor on the RHS of a var declaration is an implicit declaration of an declaration is an implicit declaration of an ANONYMOUS type)ANONYMOUS type)

Loose name equivalence equates types of p & q and r, Loose name equivalence equates types of p & q and r, s, & u because in both cases we can trace back to the s, & u because in both cases we can trace back to the same "^" constructor.same "^" constructor.

An ExampleAn Example

Page 13: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

1313

Type ConversionType Conversion

• A values of one type can be used in a context that A values of one type can be used in a context that expects another type using expects another type using type conversiontype conversion or or type type castcast

• Two flavors:Two flavors:– Converting type castConverting type cast: underlying bits are changed: underlying bits are changed

» E.g.E.g. In C, In C,int i; float f = 3.4;int i; float f = 3.4;i = int(f);i = int(f);

– Nonconverting type castNonconverting type cast: underlying bits are not altered: underlying bits are not altered» E.g.E.g. In C, In C,int i; float f; int j = 4;int i; float f; int j = 4;i = j;i = j;i = *((int *) &f);i = *((int *) &f);

Page 14: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

1414

Type CheckingType Checking

• Type equivalenceType equivalence– When are the types of two values the same?When are the types of two values the same?

• Type compatibilityType compatibility– When can a value of type A be used in a context that When can a value of type A be used in a context that

expects type B?expects type B?

• Type inferenceType inference– What is the type of an expression, given the types of the What is the type of an expression, given the types of the

operands?operands?

Page 15: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

1515

Type CompatibilityType Compatibility

• Most languages do not require type equivalence in Most languages do not require type equivalence in every contextevery context

• Two types T and S are compatible in Ada if any of Two types T and S are compatible in Ada if any of the following conditions is true:the following conditions is true:

– T and S are equivalentT and S are equivalent– T is a subtype of ST is a subtype of S– S is a subtype of TS is a subtype of T– T and S are arrays with the same number of elements and T and S are arrays with the same number of elements and

the same type of elementsthe same type of elements

Page 16: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

1616

Type CompatibilityType Compatibility

• Implicit type conversion due to type compatibility Implicit type conversion due to type compatibility are known as are known as type coercionstype coercions

– They may involved low-level conversionsThey may involved low-level conversions

• Example:Example: type weekday is (sun,mon,tue,wed,thur,fri,sat);type weekday is (sun,mon,tue,wed,thur,fri,sat);

subtype workday is weekday range mon..fri;subtype workday is weekday range mon..fri;

Page 17: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

1717

Type CompatibilityType Compatibility

• Type coercions make type systems weakerType coercions make type systems weaker

• Example:Example:

Page 18: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

1818

Type CompatibilityType Compatibility

• Generic container objects require a Generic container objects require a generic reference generic reference typetype

• ExampleExample

Page 19: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

1919

Type CheckingType Checking

• Type equivalenceType equivalence– When are the types of two values the same?When are the types of two values the same?

• Type compatibilityType compatibility– When can a value of type A be used in a context that When can a value of type A be used in a context that

expects type B?expects type B?

• Type inferenceType inference– What is the type of an expression, given the types of the What is the type of an expression, given the types of the

operands?operands?– Generally easy, but subranges and composites may Generally easy, but subranges and composites may

complicate this issuecomplicate this issue

Page 20: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

2020

RecordsRecords

Page 21: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

2121

RecordsRecordsMemory LayoutMemory Layout

• Basic layout in 32-bit machinesBasic layout in 32-bit machines– There may be There may be holesholes in the allocation of memory in the allocation of memory

Page 22: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

2222

RecordsRecordsMemory LayoutMemory Layout

• Packed layoutPacked layout

• Rearranging record fieldRearranging record field

Page 23: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

2323

RecordsRecordsImplications of Memory LayoutImplications of Memory Layout

• More memory efficient layouts have several More memory efficient layouts have several drawbacksdrawbacks

– Assignments require multiple instructionsAssignments require multiple instructions» Masking and shifting operationsMasking and shifting operations

– Access to record elements requires multiple instructionsAccess to record elements requires multiple instructions» Masking and shifting operationsMasking and shifting operations

• Holes complicate record equality Holes complicate record equality – Requires field by field comparison or default values in Requires field by field comparison or default values in

holesholes– Some languages forbid record comparisonSome languages forbid record comparison

» E.g.E.g. Pascal, C Pascal, C

Page 24: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

2424

Variant RecordsVariant Records

• A variant record A variant record provides two or provides two or more alternative more alternative fields or collections fields or collections of fields, of fields, butbut only only one of the one of the alternatives is valid alternatives is valid at any given timeat any given time

– They are called They are called unions in C/C++unions in C/C++

Page 25: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

2525

Safe Unions (Ada)Safe Unions (Ada)

Page 26: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

2626

Variant RecordsVariant RecordsMemory LayoutMemory Layout

• Some languages, like C, do not check whether Some languages, like C, do not check whether variant records are properly accessvariant records are properly access

• Other languages keep track of the value in an Other languages keep track of the value in an additional field, increasing safetyadditional field, increasing safety

Page 27: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

2727

ArraysArraysMemory LayoutMemory Layout

• Arrays are usually stored in contiguous locationsArrays are usually stored in contiguous locations

• Two common ordersTwo common orders

Page 28: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

2828

ArraysArraysMemory LayoutMemory Layout

• Contiguous allocation vs. row pointersContiguous allocation vs. row pointers

Page 29: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

2929

ArraysArraysAddress CalculationsAddress Calculations

• Code generation for a 3D arrayCode generation for a 3D array– A: array [LA: array [L11..U..U11] of array [L] of array [L22..U..U22] of array [L] of array [L33..U..U33] of ] of

elem_typeelem_type

Page 30: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

3030

ArraysArraysAddress CalculationsAddress Calculations

Row-MajorRow-MajorOrderOrder

Compile-Time Compile-Time ConstantConstant

OptimizationOptimization

Page 31: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

3131

ArraysArrays Address CalculationsAddress Calculations

Instruction sequence to load A[I,j,k] into a register:Instruction sequence to load A[I,j,k] into a register:

Page 32: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

3232

Dope VectorsDope Vectors

• Descriptors (Descriptors (dope vectorsdope vectors) are required when array ) are required when array bounds are not known at compile time.bounds are not known at compile time.

• A dope vector is usually stored next to the pointer to A dope vector is usually stored next to the pointer to data.data.

• Typically, a dope vector for an array of dynamic Typically, a dope vector for an array of dynamic scope will contain the lower bound for each scope will contain the lower bound for each dimension and the size of every dimension.dimension and the size of every dimension.

• Upper bounds are also need to support dynamics out-Upper bounds are also need to support dynamics out-of-bounds checkingof-bounds checking

• When bounds are know the arithmetic can be done at When bounds are know the arithmetic can be done at compile timecompile time

Page 33: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

3333

Arrays: Lifetime and ShapeArrays: Lifetime and Shape

• LifetimeLifetime (how long object exists) and (how long object exists) and shapeshape (dimensions and bounds), common options:(dimensions and bounds), common options:

– global lifetime, static shapeglobal lifetime, static shape» Pascal, C globalsPascal, C globals

– local lifetime, static shapelocal lifetime, static shape» Pascal, C localsPascal, C locals

– local lifetime, shape bound at elaborationlocal lifetime, shape bound at elaboration» Ada localsAda locals

– arbitrary lifetime, shape bound at elaborationarbitrary lifetime, shape bound at elaboration» Java arraysJava arrays

– arbitrary lifetime, dynamic shapearbitrary lifetime, dynamic shapeIcon strings, APL, Perl arraysIcon strings, APL, Perl arrays

Page 34: 1 Type Checking and Data Type Implementation (Sections 7.2- 7.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix

3434

Dope VectorsDope Vectors

• The first two classes are just familiar global and local The first two classes are just familiar global and local variables. variables.

• The third class can still be put in a procedure's The third class can still be put in a procedure's activation record;activation record;

– you put the dope vector and a pointer at a fixed offset from you put the dope vector and a pointer at a fixed offset from the FP and the data itself higher up in the frame.the FP and the data itself higher up in the frame.

• The fourth and fifth have to be allocated off a heap.The fourth and fifth have to be allocated off a heap.