1 / 13

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) alias type Age1 is new integer; -- new and different type

Download Presentation

Type Equivalence Rules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 t1 is assignment compatible with any other numeric type t2 if conversion of t1 to t2 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. + [] i int i a int int →int Typical Type Checking Example Requires declarations int i; int a[10]; int Requires rules []:(int→int ) Χ int →int +:int Χ int → int int

  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: • All occurrences of the same identifier in a given scope must have the same type. • 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. • 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.) • 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. + [] i i a Polymorphic Type Checking(Same Example) Assign type variables {α,β,γ,δ,...} to leaf nodes Requires declarations or functions δΧδ → δ [] : (int → σ) Χ int → σ int +:δΧδ → δ (int → σ) Χ int → σ int int β (by rule 1) 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 <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);

  13. Example • Problem 6.28

More Related