{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08 Types-2 - Type checking Scott 7.2 including CD81 on the...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Type checking Scott 7.2, including CD81 on the ML type system
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Type checking Recall the def of a type system 1. Mechanism for defining types and associating them with certain language constructs 2. A set of rules for type equivalence—when are the types of two values the same? type compatibility—when can a value of a given type can be used in a given context? type inference—how can we determine the type of an expression based on the types of its parts and the context
Background image of page 2
Type Equivalence Structural equivalence Name equivalence with alias types strict loose
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Structural equivalence: two types are equivalent if they consist of the same components put together in the same way. Example TYPE t1 = RECORD x,y: INTEGER END TYPE t2 = RECORD x,y: INTEGER END TYPE t3 = RECORD p,q: INTEGER END
Background image of page 4
Name equivalence Based on lexical occurrence. Each definition introduces a new type. The same example TYPE t1 = RECORD x,y: INTEGER END TYPE t2 = RECORD x,y: INTEGER END TYPE t3 = RECORD p,q: INTEGER END these types are not equivalent under name equivalence
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Alias types TYPE new_type = old_type; Loose name equivalence : aliases are equivalent in an otherwise name equivalent language Strict name equivalence : aliased types are not equivalent
Background image of page 6
MODULE pair; IMPORT elemType; EXPORT first, second; ... PROCEDURE first(Pair p):elemType; //returns first item in pair .... PROCEDURE second(Pair p):elemType //returns second item in pair To use precede with a type definition to define elemType. for example TYPE elemType = INTEGER; INTEGER is then considered to be the same type as elemType provided the language has loose name equivalence
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
TYPE person = STRING; TYPE automobile = STRING; person and automobile should probably not be considered equivalent—strict name equivalence would be more appropriate. Ada allows the programmer to specify which subtype elemType is integer; //loose name equiv type person is new string; //strict name equiv type automobile is new string; //gives desired behavior
Background image of page 8
Type conversions (casts) Allow programmer to use object of one type in a context where another type is expected. 3 cases: types are structurally equivalent but language is name equivalent. types have different sets of values, but intersection is represented in the same way. types have different low-level representations, but there is some sensible correspondence between them.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
types are structurally equivalent but language is name equivalent. Conversion is conceptual since types have same low-level representation and set of values. TYPE t2 = RECORD x,y: INTEGER END TYPE t3 = RECORD p,q: INTEGER END t3 x; t2 y := (t2)x; The cast just tells the compiler to allow this assignment .
Background image of page 10
types have different sets of values, but intersection is represented in the same way.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}