1 / 18

Data Types

Data Types. Definition of Data Type. Definition 1 a set of values example) the data type int in Java denotes the following set V int = { x | x ∈ Z , -2,147,483,648 ≤ x ≤ 2,147,483,647 } Definition 2 a set of values with a set of operations

royce
Download Presentation

Data Types

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. Data Types

  2. Definition of Data Type • Definition 1 • a set of values example) the data type int in Java denotes the following set Vint = { x | x ∈ Z, -2,147,483,648 ≤ x ≤ 2,147,483,647 } • Definition 2 • a set of values with a set of operations example) the data type int in Java denotes the following set Vint = { x | x ∈ Z, -2,147,483,648 ≤ x ≤ 2,147,483,647 } with the following set of operations Fint = { +, -, *, … } • The operators should be applied to the “appropriate” operands. Programming Languages

  3. Type Checking and Type Inference • Type Checking • The process to determine the type consistency between the operators (or subprograms) and the operands (or arguments) • Type Inference • The process of attaching types to expressions • The type checking and the type inference are interdependent. Programming Languages

  4. Type System • Type Equivalence Algorithm • comparing two types and determining the equality of them • Type System • Type Construction Methods • Type Equivalence Algorithm • Type Inference and Type Checking Rules Programming Languages

  5. Typing • Strongly-Typed Language • the languages which detect all type errors during translation time • Weakly-Typed Language • the languages which detect type errors during translation time but allow a few loopholes • Untyped Language • languages without static type systems Programming Languages

  6. Safe Programs • Every well-typed programs are safe but not vice versa. contain data-corrupting errors Executable Programs Legal Programs (Safe Programs) Unsafe Programs Well-Typed Programs Programming Languages

  7. Type Equivalence • Structural Equivalence • Two types are equivalent when they have the same structure. • The same structure means the same type constructor and the same component types. • Name Equivalence • Two types are equivalent only if they have the same name. • Declaration Equivalence • Two types are equivalent if they are derived from the same name. Programming Languages

  8. Complicating Factors • Array Size • The size of array types may be considered as a part of the array type • If it is the case, type checking of array parameters is problematic. • Field Names of Record Types • The following RecA and RecB should be structurally equivalent (char ⅹ int) but not in practice because of the field names. struct RecA { char x; int y; }; struct RecB { char a; int b; }; Programming Languages

  9. Language Example: Ada • Ada • pure name equivalence • the variables a and b in a, b: array (1..10) of integer; are not type equivalent because the above declaration is considered as a shorthand for a: array (1..10) of integer; b: array (1..10) of integer; • the new types and the subtypes are different each other • new type type Age is new integer; • subtype: compatible to the original type subtype Age is integer; Programming Languages

  10. name equivalence for structures and unions structural equivalence for others ‘typedef’ does not introduce a new type name, just makes an alias array size is not the part of the array type struct A { char x;   int y; }; struct B { char x;   int y; }; typedef struct A C; typedef C* P; typedef struct B * Q; typedef struct A * R; typedef int S[10]; typedef int T[5]; typedef int Age; typedef int (*F)(int); typedef Age (*G)(Age); Language Example: C struct A = C ≠ struct B P = R ≠ R S = T Age = int F = G Programming Languages

  11. Language Examples: Others • Pascal • every type constructors introduce a new type • new type names for existing type names are equivalent • Java • no ‘typedef’ constructs • class and interface declarations introduce a new type name • inheritance hierarchy (subtype) • structural equivalence for arrays • ML • ‘datatype’ constructs a new type • ‘type’ constructs an alias of an existing type Programming Languages

  12. Type Checking • Type Checking Categories • Dynamic Type Checking • Static Type Checking • Weakly-Typed Languages • Strongly-Typed Languages: All type errors are caught before run-time in a strongly typed language. Note) Some languages may leave unspecified whether dynamic or static typing is to be used Programming Languages

  13. Type Checking Examples • C/C++ • static type checking • no a strongly typed language: type conversion • Scheme • dynamic type checking but rigorous • predefined type test functions: number?, symbol?, … • Ada • a strongly typed language • array bound checks are performed during run-time: exceptions Programming Languages

  14. Type Checking and Type Inference • Type Inference • the process to infer the type of an expression from the types of its subexpressions • Type Checking and Type Inference • Type checking rules and type inference rules are intermingled e1 + e2 • Type checking rules are type inference rules are closely related with the type equivalence algorithm • Explicit declarations are helpful for type checking but not mandatory To check the type error of the whole expression, we have to infer the types of the subexpressions. Programming Languages

  15. Type Compatibility • Relaxing the type correctness For assignment compatibility, information preservation is an important requirement. • Language Examples • Ada: subrange types of a base type are compatible • C, Java: numeric types are compatible e1 + e2 Type Equivalence Type Compatibility e1 and e2 should be of equivalent types. e1 and e2 should be of compatible types. Programming Languages

  16. Classifying Type Conversions According to the notation Implicit Conversions (coercion) Explicit Conversions (casting) According to the sizes of the types involved Widening Conversions Narrowing Conversions: may involve a loss of data C Example int x = 3; x = 2.3 + x / 2; int promotion (widening) double double assignment conversion (narrowing) int Type Conversion Programming Languages

  17. Notes on Type Conversions • Advantages of Explicit Conversions • Type conversions are documented precisely. • Make it easier for the translator to resolve overloading. double max(int, double); // max #1 double max(double, int); // max #2 ... max(2, 3); // calls what? max(2, (double)3); // calls max #1 • Structure Casting • The sizes of the structure types should be identical. • The translation merely reinterprets the memory Programming Languages

  18. A Loophole in the Strong Typing • Undiscriminated union makes a loophole in the strong typing • Example) inspecting the internal value of the true in C++ union { char c; bool b; } x; x.b = true; cout << static_cast<int>(x.c) << endl; Programming Languages

More Related