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

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Type checking Scott 7.2, including CD81 on the ML type system 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 equivalencewhen are the types of two values the same? type compatibilitywhen can a value of a given type can be used in a given context? type inferencehow can we determine the type of an expression based on the types of its parts and the context Type Equivalence Structural equivalence Name equivalence with alias types strict loose 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 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 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 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 TYPE person = STRING; TYPE automobile = STRING; person and automobile should probably not be considered equivalentstrict 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 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. 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 . types have different sets of values, but intersection is represented in the same way....
View Full Document

This note was uploaded on 12/09/2009 for the course COP 5555 taught by Professor Staff during the Fall '08 term at University of Florida.

Page1 / 59

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

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online