DOC PREVIEW
UF COP 5555 - Type Equivalence Rules

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Type Equivalence RulesSlide 2Slide 3Type CompatibilityAssignment CompatibilityImplict TypesType ConversionPolymorphic Type CheckingTypical Type Checking ExamplePolymorphic Type Checking Requires us to use More Interesting RulesPolymorphic Type Checking (Same Example)Explicit PolymorphismExampleType 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 typetype Age2 is integer; -- type equivalent alias for integer•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.•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 typesType 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 compatibleAssignment 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.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.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 typesPolymorphic 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.Typical Type Checking Exampleai[] i+int i;int a[10];Requires declarationsint →intintintintint[]:(int→int ) Χ int →int+:int Χ int → intRequires rulesPolymorphic 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.Polymorphic Type Checking(Same Example)ai[] i+Requires declarations or functions[] : (int → σ) Χ int → σAssign type variables {α,β,γ,δ,...} to leaf nodes(int → σ) Χ int → σα ββ (by rule 1)intint+:δ Χ δ → δδ Χ δ → δintint → σint → intintExplicit 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);Example•Problem


View Full Document

UF COP 5555 - Type Equivalence Rules

Download Type Equivalence Rules
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Type Equivalence Rules and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Type Equivalence Rules 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?