Chapter 4 of Programming Languages by Ravi Sethi TYPES:DATA REPRESENTATION • The Role of Types • Basic Types • Arrays: Sequences of Elements • Records: Named Fields • Unions and Variant Records • Sets • Pointers: Efficiency and Dynamic Allocation • Two String Tables • Types and Error Checking THE ROLE OF TYPES • Data objects and Data representation Objects in an application have corresponding representations in a program. Example: correspondence between application and program application program data January 31 31 data May 6 126 variable d n(integer #) operation tomorrow (d) n+1 • Values and Their Types 1. Basic type 2. Constructed type THE ROLE OF TYPES(cont’d) • Type Expression a) Describes how a data representation is built up B) Can be used to 1. Represent data objects 2. Lay out values in the underlying machine 3. Check that operators are applied properly within expression • Static Layout Decision (next slide) • A Preview of Type Names, Arrays, and Records 1. Type names: boolean, char, integer, real 2. Arrays: a sequence of elements 3. Records: a set of components, each with its own type THE ROLE OF TYPES(cont’d) BASIC TYPES • Enumeration : a finite sequence of names written between parentheses type day = (Mon, Tue, Wed, Thu, Fir, Sat, Sun); • Integers and Reals • Short-Circuit Evaluation of Boolean Expressions Example : While (i >= 0 && x != A[ i ]) i = i-1 ; • Subranges : a special case of basic types that restrict the range of values of an existing type - weekends, weekdays BASIC TYPES (cont’d) • Programming Style: Characters and Type Conversion Example : C : characters are implicitly coerced to integers int c; c = getchar(); -> coerced to an int Pascal : Conversion will be done explicitly c = chr(ord(c)) i = ord(chr(i)) ARRAYS: SEQUENCES OF ELEMENTS • Efficient access and storage allocation • Indexed by integers or enumerations • Array layout 1. Elements appear in consecutive location in the underlying machine 2. Elements can be computed in two parts a) Pre-computation b) Run time 3. Arrays of Arrays : row-major layout, column-major layout ARRAYS: SEQUENCES OF ELEMENTS (cont’d) • Array Bounds a) Computed at compile time (Static evaluation) b) Computed at run time (Dynamic evaluation) • Array Values and Initialization : a sequence of values for array elements Example: int coin[] = { 1, 5, 10, 25, 50, 100 }; RECORDS: NAMED FIELDS • A record type is a template • A Variable declaration allocates storage • Operations on Records Example : z. re := x. re + y. re • A comparison of Arrays and Records arrays records --------------------------------------------------------------------------------------------component types component selectors homogeneous expressions evaluated at run time heterogeneous names known at compile time UNIONS AND VARIANT RECORDS • Union : a special case of a variant record with an empty common part • Variant record : a part common to all records of that type and a variant part • figure 4.7 (next slide) UNIONS AND VARIANT RECORDS(cont’d) UNIONS AND VARIANT RECORDS(cont’d) • Layout of Variant Records 1. Fixed Part 2. Tag Field 3. Variant Part • Variant Records compromise type safety SETS • Set Values:in Pascal, All elements must be of the same simple type • Set Types: Type set of S represents subsets of S Example: var A : set of [1..3] A can denote one of the following sets: [ ],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3] • A set of n elements: implemented as a bit vector of length n • The basic operation on set is a membership test POINTERS: EFFICIENCY AND DYNAMIC ALLOCATION • More efficient to move or copy a pointer to the data structure • Dynamically Implemented to use in data structure • First-class citizens • Pointer type must be declared before used • Dereferencing operation : indirect access Example Pascal: dynamic allocation on the heap, dereferencing, assignment, equality testing, deallocation • Serve as links between cells (linked lists) • Type and layout of storage are known statically before a program runs POINTERS: EFFICIENCY AND DYNAMIC ALLOCATION(cont’d) • The pointer operations affect access to storage • Dangling pointer is a pointer to storage that is being used for another purpose (deallocate the storage, but the reference to the pointer is still there) • Pascal restricts pointer operations (only new and dispose); C stresses flexibility • Rearranged inexpensively TWO STRING TABLES • From a distance, types in Pascal and C are very similar, both have arrays, records and pointers. Differences in the treatment of pointers, lead to differences in style. The variable-length string application is famous and is used to show the differences (section 4.8) • Pascal • TeX • troff • Word • all the words are in a pool (buffer), the words are index through an array called start which contains the beginning position of the word in the pool starting at 0 so start[0] = 0 and pool[start[0]] is the ’T’ Indirect access through an array Integers as pointers - disadvantage compiler cannot check that it is a character TWO STRING TABLES • From Yacc - Yet another Compiler Compiler • start is the actual pointer address in pool not just an index • C 1. Indirect access through pointers 2. Array name : pointer to a zeroth element - operations on pointers in C allow successive characters in a string to be accessed 3. Dereferencing and yielding operators Example: p = &x; *p = *p + 1; is same as x = x + 1; TYPES AND ERROR CHECKING • Types extend from values to expressions, the type of an expression x + y can be inferred from the types x and y • Types of variable bindings 1. Static or early bindings 2. Dynamic or late bindings Pascal has static bindings of types and dynamic bindings of values to variables. Lisp has dynamic binding of both values and types • Type systems : a set of rules • Basic rule of type checking 1. Overloading : Multiple Meanings 2. Coercion : conversion from one type to another 3. Polymorphism : parameterized type TYPES AND ERROR CHECKING(cont’d) • Type names and type equivalence Example : x, y : array [0..9] of integer z : array [0..9] of integer In Pascal, x and y have the same type, but z does not * Structural Equivalence 1. A type name is structurally equivalent to itself 2. Two types are formed by applying the same type constructor 3. After a type declaration, type n = T TYPES AND ERROR CHECKING(cont’d) * Forms of Name Equivalence 1. Pure name equivalence 2. Transitive name equivalence, type S = integer; T = S; U = integer; 3. Type expression equivalence * Type Equivalence - sometimes left ambiguous in Pascal, sometimes given rules like Modula-2 * Circular Types : linked data structures give rise to recursive or circular types struct x { int p; struct x *next; }; TYPES AND ERROR CHECKING(cont’d) • Static and Dynamic Checking 1. Type error occurs if an operation is improperly applied 2. Programs are checked statically 3. Dynamic checking is done during program execution 4. Strong type ensures freedom from type errors