Download presentation
Presentation is loading. Please wait.
Published byChristian Miles Modified over 9 years ago
1
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
2
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.
3
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
4
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
5
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 t 1 is assignment compatible with any other numeric type t 2 if conversion of t 1 to t 2 does not cause loss of information.
6
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.
7
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) functions character’pos(‘a’) character’val(41) Union types
8
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.
9
Typical Type Checking Example a i []i + int i; int a[10]; Requires declarations int →int int []:(int→int ) Χ int →int +:int Χ int → int Requires rules
10
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.
11
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
12
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 struct StackNode { T data; StackNode *next; }; template struct Stack { StackNode *theStack; }; ML declares this in a different way datatype ‘a Stack = EmptyStack | Stack of 'a * ('a Stack);
13
Example Problem 6.28
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.