10/31/2015IT 3271 A Type is a Set 1. A set of values 2. A low-level representation 3. A collection of operations on those values int n; Chapter 6: Types Bertrand Russell (1872 – 1970) (1910) Alfred N. Whitehead (1861 – 1947) Required for programming languages, so it is more restricted.
10/31/2015IT 3272 n Issues of types – Type annotations (how to declare?) – Type inference (how to determine?) – Type checking (when to check?) – Type equivalence issues (what is it?)
10/31/2015IT 3273 Primitive vs. Constructed Types n Primitive type: can be used but cannot be defined. ML: int, real, char n Constructed type: can be defined in a program ML: type intpair = int * int
10/31/2015IT 3274 Primitive Types n Every programming language defines its primitive types – Some define the primitive types precisely for implementation (e.g., Java) – Some leave it loose for implementation (e.g., C, ML)
10/31/2015IT 3275 Integral Types in different PL’s C: char unsigned char short int unsigned short int int unsigned int long int unsigned long int No standard implementation, but longer sizes must provide at least as much range as shorter sizes. Java: byte (1-byte signed) char (2-byte unsigned) short (2-byte signed) int (4-byte signed) long (8-byte signed) Scheme: integer Integers of unbounded range
10/31/2015IT 3276 Issues of primitive types n What sets of values? – Language specification; how much left up to the implementation? – If necessary, how can a program find out? ( INT_MAX in C, Int.maxInt in ML, etc.) n What operations are supported? – Detailed definitions: rounding, exceptions, etc. The choice of representation is a critical part in these decisions
10/31/2015IT 3277 Constructed Types 1.enumerations 2.tuples 3.arrays 4.strings 5.lists 6.unions 7.subtypes 8.function types Example: Each has a connection to a set operation in mathematics
10/31/2015IT 3278 Making Sets by Enumeration n We can construct sets by just listing all the elements: C: enum coin {penny, nickel, dime, quarter}; Ada: type GENDER is (MALE, FEMALE); Pascal: type primaryColors = (red, green, blue); ML: datatype day = M | Tu | W | Th | F | Sa | Su; n These define a new type (= set) n They also define a collection of named constants of that type (= elements)
10/31/2015IT 3279 Representing Enumeration Values n A common representation is to treat the values of an enumeration as small integers enum coin { penny = 1, nickel = 5, dime = 10, quarter = 25 }; enum escapes { BELL = '\a', BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', VTAB = '\v', RETURN = '\r' }; They are countable!!
10/31/2015IT Operations on Enumeration Values n Equality test: n If using integer to represent elements then elements are legitimate for all integer operations: fun isWeekend x = (x = Sa orelse x = Su); Pascal: for C := red to blue do P(C) C: int x = penny + nickel + dime;
10/31/2015IT Making Sets by Tupling n The Cartesian product of two or more sets defines tuples: n Some language like ML supports pure tuples: (i.e., fields without names) fun get1 (x : real * real) = #1 x;
10/31/2015IT Making Types by Tupling n Many PLs support record types: tuples with named fields C: struct complex { double rp; double ip; } x; ML: type complex = { rp:real, ip:real }; Operations on tuples: (selection) C: x.ip ML: #ip x fun getip (x : complex) = #ip x;
10/31/2015IT Representing Tuple Values n A common representation is to just place the elements side-by-side in memory n But there are lots of details: – In what order? – Are there “holes” between elements (e.g. on word boundaries) in memory? – Are these details visible to the programmer?
10/31/2015IT What does C say about tuples (structs)? Preface C is a general-purpose programming language which features economy of expression, modern control flow and data structures, and a rich set of operators. C is not a “very high level” language, nor a “big” one, and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages. ….. The C Programming Language Brian W. Kernighan and Dennis M. Ritchie 1978 edition Let’s see what does C say about itself first.
10/31/2015IT ANSI C The members of a structure have addresses increasing in the order of their declarations. A non-field member of a structure is aligned at an addressing boundary depending on its type; therefore, there may be unnamed holes in a structure. struct { unsigned keyword : 1; unsigned external : 1; unsigned statics : 1; int value; unsigned ascii : 7; char eascii; } flag1, flag2; flag1 flag2 unnamed holes
10/31/2015IT Sets of Vectors n Fixed-size vectors: n Arbitrary-size vectors: n In PLs: arrays, strings, lists n Issues: – What are the index values? – Is the array size fixed at compile time? What is the difference between this and tuples?
10/31/2015IT Index values for array n Java, C, C++: – First element of an array a is a[0] – Indexes are always integers starting from 0 n Pascal is more flexible: – integers, characters, enumerations, subranges – Starting/Ending index chosen by the programmer – size is fixed at compile time type LetterCount = array['a'..'z'] of Integer; var Counts: LetterCount; begin Counts['a'] = 1 ………
10/31/2015IT n Array designers need to decide: 1.What are the index values? 2.Is array size fixed at compile time (static type)? 3.What operations are supported? 4.Are multiple dimensions allowed? 5.Is a higher-dimensional array the same as an array of arrays? 6.Is re-dimensioning possible at runtime? 7.What is the order of elements in memory? 8.Is there a separate type for strings (not just array of characters)? 9.Is there a separate type for lists?
10/31/2015IT Making Types by Union C: union element { int i; float f; }; ML: datatype element = I of int | F of real; Making Sets by Union:
10/31/2015IT Representing Union Values n Two (or more) representations overlap each other in memory. n This representation may or may not be exposed to the programmer union element { int i; char *p; } u; /* sizeof(u) is */ /* max(sizeof(u.i),sizeof(u.p)) */
10/31/2015IT Loosely Typed Unions n Some languages expose the details of union implementation (i.e., the programmer knows the implementation) union element { int i; float f; } e; No type casting is involved e f i float x; int n; e.i = 100; e.f = 1.0; x = e.f; n = e.i;
10/31/2015IT Strictly Typed Unions n In ML, an element type x is either I or F, thus, you have to say what to do with each type of value in the union: datatype element = I of int | F of real; Type casting is involved fun getReal (F x) = x | getReal (I x) = real x;
10/31/2015IT Variant Records n Union where specific type is dynamically determined by the value of a field (“discriminated union”) n Ada and Modula-2 C: I don’t care what is it. ML: I have to know what is it in advance Ada: Let’s see what is it.
10/31/2015IT Ada Variant Record Example type DEVICE is (PRINTER, DISK); type PERIPHERAL(Unit: DEVICE) is record HoursWorking: INTEGER; case Unit is when PRINTER => Line_count: INTEGER; when DISK => Cylinder: INTEGER; Track: INTEGER; end case; end record;
10/31/2015IT Subsets n We can define the subset selected by any predicate P: S X
10/31/2015IT Making Subtypes – Less general: Pascal subranges type digit = 0..9; – More general: Ada subtypes subtype DIGIT is INTEGER range 0..9; subtype WEEKDAY is DAY range MON..FRI; – Most general: Lisp types with predicates
10/31/2015IT Ada Subtypes type DEVICE is (PRINTER, DISK); type PERIPHERAL(Unit: DEVICE) is record HoursWorking: INTEGER; case Unit is when PRINTER => Line_count: INTEGER; when DISK => Cylinder: INTEGER; Track: INTEGER; end case; end record; subtype DISK_UNIT is PERIPHERAL(DISK);
10/31/2015IT Types with Predicates in Lisp (declare (type integer x)) (declare (type (or null cons) x)) (declare (type (and number (not integer)) x)) (declare (type (and integer (satisfies evenp)) x))
10/31/2015IT Representing Subtype Values n Usually, we just use the same representation for the subtype as for the supertype n Questions: – Do we want to shorten it if we can? I.e., does X: 1..9 take the same space as X: Integer ? – Do we enforce the subtyping? Is X := 10 legal? What about X := X + 1 ?
10/31/2015IT n Type is a key idea of OOP n A class is a type: data and operations on that data, bundled together n A subclass is a subtype: it includes a subset of the objects, but supports a superset of the operations
10/31/2015IT Making Sets of Functions n define a set of functions with given domain and range: A set can be a collection of functions
10/31/2015IT Making Types of Functions n Most languages have some notion of the type of a function: int f(char a, char b) { return a==b; } fun f(a:char, b:char) = (a = b); ML: C:
10/31/2015IT Operations on Function Values n call the function n bound to variables n passed as parameters n Treated as value Many languages support nothing beyond function call (What can C do?) ML supports many operations (all above and others)
10/31/2015IT Type Annotations The syntax for describing types n Some languages use naming conventions to declare the types of variables – BASIC: S$ is a string – Fortran: I is an integer n Like explicit annotations, these conventions provide static type information
10/31/2015IT Type Inference n Infers a static type for every expression and for every function, usually requires no annotations n Constants usually have static types – Java: 10 has type int, 10L has type long n Expressions may have static types inferred from operators and types of operands – Java: if a is double, a*0 is double ( 0.0 ) Simple Complicate Determining the types
10/31/2015IT Static Type Checking n Static type checking determines a type for everything before running the program: variables, functions, expressions, everything n Compile-time error messages when static types are not consistent – Operators: 1+"abc" – Functions: round("abc") – Statements: if "abc" then … n Most modern languages are statically typed
10/31/2015IT Dynamic Typing n In some languages, programs are not statically type-checked before being run. they are dynamically type-checked during the runtime. n At runtime, the language system checks that operands are of suitable types for operations
10/31/2015IT Example: Lisp It won’t work if a or b is not a number An improper call, like (f nil nil), is not caught at compile time n It is caught at runtime – that is dynamic typing (defun f (a b) (+ a b))
10/31/2015IT Explicit Runtime Type Tests n Some languages allow explicit runtime type tests: – Java: test object type with instanceof operator n Type information is known at runtime, even when the language is mostly statically typed
10/31/2015IT Strong Typing Weak Typing The purpose of type-checking is to prevent the application of operations to incorrect types of operands n ML and Java, the type-checking is thorough enough to guarantee this—that’s strong typing n C has holes in the type system that add flexibility but weaken the guarantee
10/31/2015IT Type Equivalence What happens if apply f to an irpair2 ? – Name equivalence does not permit this: irpair2 and irpair1 are different names – Structural equivalence does permit this, since the types are constructed identically n ML uses Structural equivalence type irpair1 = int * real; type irpair2 = int * real; fun f(x:irpair1) = #1 x;