type equivalence rules

13
Type Equivalence Rules • Ada – Strict name equivalence except for almost everything • Unique array constructors give rise to unique types • Subtypes can create a new type or just an (equivalent) alias type Age1 is new integer; -- new and different type type Age2 is integer; -- type equivalent alias for integer

Upload: lamar-mathis

Post on 31-Dec-2015

18 views

Category:

Documents


3 download

DESCRIPTION

Type Equivalence Rules. Ada Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create a new type or just an (equivalent) alias type Age1 is new integer; -- new and different type - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Type Equivalence Rules

Type Equivalence Rules

• Ada– Strict name equivalence except for almost

everything• Unique array constructors give rise to unique types• Subtypes can create a new type or just an

(equivalent) aliastype Age1 is new integer; -- new and different type

type Age2 is integer; -- type equivalent alias for integer

Page 2: Type Equivalence Rules

• C– Name equivalence for structs and unions

• Struct and union constructors create new nonequivalent types.

– Structural equivalence for everything else• Any other type constructor (array, pointer) creates

a type with the same name as any other same structure type.

Page 3: Type Equivalence Rules

• Java– Class and interface declarations create new

type names. Name equivalence is used for these types with the exception that a subclass type object may be assigned to a superclass variable.

– Structural equivalence for scalars– Arrays must be structurally equivalent

(ignoring size) with name equivalent component types

Page 4: Type Equivalence Rules

Type Compatibility

• General term type compatibility applies both to assignment and comparison with relational operators.

• Equivalent types are always compatible.

• C and Java:– all numeric types are compatible

• Ada– subranges of the same type are compatible

Page 5: Type Equivalence Rules

Assignment Compatibility

• Assignment compatibility is used to determine when a value of one type may be assigned or passed as a parameter to a variable of another type. We say that the assigned value type is compatible with the variable or parameter to which it will be assigned.

• C– All numeric types are assignment compatible

• Java– Any numeric type t1 is assignment compatible with

any other numeric type t2 if conversion of t1 to t2 does not cause loss of information.

Page 6: Type Equivalence Rules

Implict Types

• If the type of a value or name is determined without a declaration, it is said to be implicit.– FORTRAN variables: starting letter

determines type a-h,o-z: Real, i-n: integer– Old C: all undeclared variables and return

types are int– 4652: type is implicit. 4652L: type is explicit.

Page 7: Type Equivalence Rules

Type Conversion• Coercion (implicit type conversion) is used to convert values to

compatible types for use as arguments to operations.int x = 5;x = 4.6 + x / 3;x = (4.6 + x) / 3;

• Widening vs. Narrowing• Explicit conversion (casts in C and Java)

– static_cast (old-style cast, but checked for consistency and can’t cast away const)

– reinterpret_cast (old-style cast, but can’t cast away const)– const_cast (casts away const)– dynamic_cast (casts a ptr/ref to a subclass or superclass ptr/ref)

• Ada attribute (tick) functionscharacter’pos(‘a’)character’val(41)

• Union types

Page 8: Type Equivalence Rules

Polymorphic Type Checking• Program code is said to be polymorphic (meaning many-formed

or many-shaped) if it can be evaluated correctly when the variables it references are assigned different types.

• Usually the term is only applied to functions or subprograms that can be defined without giving explicit parameter type declarations.

• Conventional type checking starts with an AST for an expression attributed with the types of the leaf nodes.

• Rules are applied to determine what assignment of specific operators and functions to the interior nodes can be employed to evaluate the expression without violating type compatibility.

• If more than one such assignment is found, then the type rules of the language will either

– assign a scalar quality value to each match and choose the best matching collection of assignments, or

– declare that the expression has ambiguous overloadings and fail.

Page 9: Type Equivalence Rules

Typical Type Checking Example

a i

[] i

+

int i;int a[10];

Requires declarations

int →int int

intint

int

[]:(int→int ) Χ int →int

+:int Χ int → int

Requires rules

Page 10: Type Equivalence Rules

Polymorphic Type Checking Requires us to use More Interesting Rules

• These are rules that ML uses to infer the type correctness of polymorphic code:

1. All occurrences of the same identifier in a given scope must have the same type.

2. In an if/then/else expression, the condition must be of type bool, and the then and else clauses must be of the same type.

3. A programmer-defined function has type 'a -> 'b where 'a is the type of the function’s parameter and 'b is the type of its result. (Functions have tuple arguments.)

4. When a function is applied, the type of the argument passed to the function must be the same as the parameter type in the function’s definition and the type of the application is the same as the type of the result in the function’s definition.

Page 11: Type Equivalence Rules

Polymorphic Type Checking(Same Example)

a i

[] i

+Requires declarations or functions

[] : (int → σ) Χ int → σ

Assign type variables {α,β,γ,δ,...} to leaf nodes

(int → σ) Χ int → σ

α β

β (by rule 1)

int

int

+:δ Χ δ → δ

δ Χ δ → δ

int

int → σint → int

int

Page 12: Type Equivalence Rules

Explicit Polymorphism

• Explicit parametric polymorphism allows one to constrain elements of a data structure or piece of code to have a type given by a parameter.

• C++ template types give this capability:template <typename T>struct StackNode{ T data; StackNode<T> *next;};template <typename T>struct Stack{ StackNode<T> *theStack;};

• ML declares this in a different waydatatype ‘a Stack = EmptyStack | Stack of 'a * ('a Stack);

Page 13: Type Equivalence Rules

Example

• Problem 6.28