Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;

Similar presentations


Presentation on theme: "Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;"— Presentation transcript:

1 Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming; they make it digestible. Robin Milner

2 Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic Types (5.4.3-5.4.6) 5.5Recursive Data Types 5.6Functions as Types 5.7Type Equivalence 5.8Subtypes 5.9Polymorphism and Generics 5.10Programmer-Defined Types

3 Copyright © 2006 The McGraw-Hill Companies, Inc. 5.4.3 Arrays and Lists Arrays: Sequence of values, accessible by index All array values are from the same type Design issues: –Number of dimensions/declaration syntax –Index type –Are array dimensions static or dynamic? –Are the boundaries of the index set determined by the language or the programmer? –Should the language enforce bounds checking?

4 Copyright © 2006 The McGraw-Hill Companies, Inc. Design Issues for Arrays Declaration syntax/number of dimensions –Technically, C only allows one dimension int C[4][3]; //array of 4 3-element rows –Compare to Pascal: TYPE Num = ARRAY[1..4, 1..3] OF Integer; VAR Num:C; and Fortran: integer C(4,3) early Fortran version : DIMENSION i(4,3) –Early languages set an upper limit on the number of dimensions

5 Copyright © 2006 The McGraw-Hill Companies, Inc. Array Design Issues Index type –Usually restricted to integers –Pascal allows any integral type: integers, char, enum

6 Copyright © 2006 The McGraw-Hill Companies, Inc. Array Design Issues Are the bounds of the index set fixed? C-like languages: (0, …, n-1) Fortran: defaults to (1, 2, …, n) but modifiable: – INTEGER Counts (-20:20) Pascal & Ada: programmer determined –Ada: a : array(-4..5) of INTEGER; –Pascal: TYPE nums = ARRAY[10…19] OF Integer nums: Numbers

7 Copyright © 2006 The McGraw-Hill Companies, Inc. Array Design Issues Are array dimensions static or dynamic? –Pascal: always static; array dimensions are part of the array type. –Java arrays are sized at runtime, don’t change thereafter –C/C++ permits runtime declaration of array bounds for dynamically declared arrays: int numbers[100]; // static array int* intPtr; cin >> n; intPtr = new int[n];//dynamic array

8 Copyright © 2006 The McGraw-Hill Companies, Inc. Design Issues Should the language enforce bounds checking? –Java and Ada do; Pascal provides it as an option Safety versus efficiency Overwriting array boundaries is one of the most common (and hardest to find) errors in a program –C & C++ generate a memory protection error if the reference is outside the boundaries of the program address space, otherwise no error message is given

9 Copyright © 2006 The McGraw-Hill Companies, Inc. Implementation Issues Dope Vector: array data needed at run time; e.g., –Element size & type –Index type/range for each dimension –Start address of array in memory Used to calculate memory addresses, & do index range checking if included in language Built during the semantic analysis phase of translation

10 Copyright © 2006 The McGraw-Hill Companies, Inc. Example/Java Consider the Java declaration int[] A = new int(n); At runtime, every reference A[i] must be checked: is 0 < i < n ? What memory address is referenced? Portions of the dope vector must be present at run- time for checking.

11 Copyright © 2006 The McGraw-Hill Companies, Inc. Dope Vectors For a one-dimensional array of integers, where integers take 4 bytes of storage For a two-dimensional array of characters, where characters take 2 bytes 4 = e, element size int = element type 10 = size (n) 2 = e, element size char = element type 4 = # of rows (m) 3 = # of col’s (n)

12 Copyright © 2006 The McGraw-Hill Companies, Inc. Address Computation To compute the address of a given element : addr(a[i]) = addr(a[0]) + e∙i (e = element size) addr(a[i][j]) = addr(a[0][0]) + e∙(ni + j) (n = number of columns) Two-dimensional array formula assumes row-major storage order; Fortran uses column-major. This formula assumes indexes start at 0; for languages that permit other bounds, modify the formula.

13 Copyright © 2006 The McGraw-Hill Companies, Inc. Strings Early languages – little or no direct support –Fortran, Algol60: none –COBOL: static strings, few operations –C: string is a 1D array terminated by a NUL character error prone In Java, Perl, Python, strings are built-in types; length is not restricted. C++ provides a string library – not a built in type.

14 Copyright © 2006 The McGraw-Hill Companies, Inc. Structures (Records) Collection of elements of different types –Elements commonly referred to as fields –Stored sequentially in memory, with some qualifications Used first in Cobol, PL/I Absent from Fortran, Algol 60 Common to Pascal-like, C-like languages How does a C++ struct differ from a class? Omitted from Java as redundant

15 Copyright © 2006 The McGraw-Hill Companies, Inc. struct employeeType { int id; char name[25]; int age; float salary; char dept; }; struct employeeType employee;... employee.age = 45;

16 Copyright © 2006 The McGraw-Hill Companies, Inc. Unions C/C++: union Pascal/Ada: variant record Logically: multiple views of same storage Infrequently used today, but might be useful in some systems applications Complicates issues of type safety since actual data type isn’t known at compile time.

17 Copyright © 2006 The McGraw-Hill Companies, Inc. union myUnion {// c/c++ example int i; float r; }; union myUnion u; // can only use one member at a time u.i = 10; // u is an integer u.r = 3.14; // u is a now a float Compiler reserves enough space to hold the largest member

18 Copyright © 2006 The McGraw-Hill Companies, Inc. 5.5 Recursive Data Type C/C++ linked lists are an example of a recursive data structure since the struct Node contained a field that was a pointer to a structure of the same type. In other words, recursion is allowed in the type definition. Other languages; e.g., Haskell, provide more general ways to define recursive data structures.

19 Copyright © 2006 The McGraw-Hill Companies, Inc. 5.6 Functions as Types Languages differ as to what kinds of entities can be assigned to a variable, or passed as an argument, or in other ways treated as a value of some type. Functions are an example. Pascal example: function newton(a, b: real; function f: real): real; Know that f returns a real value, but the arguments to f are unspecified.

20 Copyright © 2006 The McGraw-Hill Companies, Inc. Functions as Types Java interfaces can be used to achieve a similar effect. Not possible to construct functions and return them as function values. Should languages make functions types? Increases the generality of languages; see discussion of Scheme programming language.

21 Copyright © 2006 The McGraw-Hill Companies, Inc. Java Interface public interface RootSolvable { double valueAt(double x); } public double Newton(double a, double b, RootSolvable f);

22 Copyright © 2006 The McGraw-Hill Companies, Inc. 5.7 Type Equivalence Pascal Report: The assignment statement serves to replace the current value of a variable with a new value specified as an expression.... The variable (or the function) and the expression must be of identical type. What is an identical type? For example, struct or array definitions in C:

23 Copyright © 2006 The McGraw-Hill Companies, Inc. struct complex { float re, im; }; struct polar { float x, y; }; struct { /* anonymous type */ float re, im; } a, b; struct complex c, d; struct polar e; int f[5], g[10]; // which are equivalent types?

24 Copyright © 2006 The McGraw-Hill Companies, Inc. Equivalent Types Name Equivalence: two types are equivalent if they have the same name. In the example, c and d are equivalent because both are type complex. a & c aren’t. Java approach for classes, C/C++ for structs Structural Equivalence: types are equivalent if they have the same structure. For record types this means having the same number and order of fields, as well as name and type. a, b, c, and d are equivalent but d and e aren’t. C/C++ approach for arrays. f and g are the same type (1-d array of int s)

25 Copyright © 2006 The McGraw-Hill Companies, Inc. 5.8 Subtypes A subtype is a type that has certain constraints placed on its values or operations. Like type equivalence, subtypes are related to validity of assignments, passing parameters to functions, etc. A variable from a subtype can be passed as a parameter of the primary type, or assigned to a variable of the primary type, but not necessarily vice versa

26 Copyright © 2006 The McGraw-Hill Companies, Inc. Type Equivalence - Examples In Ada & Pascal subtypes can be directly specified. May be used to limit values to a certain range (Ada): subtype Weekend is Day range Sat..Sun ; Java’s class hierarchy is a kind of subtyping : if s is an object of class S and t is an object of class T and S is a subclass of T, then t = s is legal.

27 Copyright © 2006 The McGraw-Hill Companies, Inc. ADA Example subtype one_to_ten is Integer range 1..10; type Day is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); subtype Weekend is Day range Saturday.. Sunday; type Salary is delta 0.01 digits 9 range 0.00.. 9_999_999.99; subtype Author_Salary is Salary digits 5 range 0.0.. 999.99;

28 Copyright © 2006 The McGraw-Hill Companies, Inc. JAVA Integer i = new Integer(3);... Number v = i;... Integer x = (Integer) v; //Integer is a subclass of Number, // and therefore a subtype In the first case i is assigned to v because an Integer is a Number, but v must be cast before assigning it to an Integer variable. Compare to widening and narrowing type conversions.

29 Copyright © 2006 The McGraw-Hill Companies, Inc. 5.8: Polymorphism and Generics From the Greek word meaning “having two or more forms” A function or operation is polymorphic if it can be applied to any one of several related types and achieve the same result. An advantage of polymorphism in a programming language is that it enables code reuse.

30 Copyright © 2006 The McGraw-Hill Companies, Inc. Polymorphism Java: overloaded methods: the number or type of parameters is used to distinguish between them Java: instance variable, method: name, name( ) Beyond overloading, an important feature of polymorphism is the ability to write functions that can be used for the same purpose on different data types.

31 Copyright © 2006 The McGraw-Hill Companies, Inc. Parametric Polymorphism A type of polymorphism in which code is written without any type-specific references. Later, the code can be used with any data type. Functions that are written in this way are called polymorphic functions. Compare to subtype polymorphism, which is based on class hierarchies and inheritance.

32 Copyright © 2006 The McGraw-Hill Companies, Inc. Ada generic functions – templates instantiated at compile time with concrete types and operators –parametric polymorphism –type binding delayed from programming time to compile time Example: In a simple bubble sort (page 128), the critical operations are –Looping through the data –Comparing two elements –Exchanging two elements Which operations are type dependent?

33 Copyright © 2006 The McGraw-Hill Companies, Inc. Parametric Polymorphism - Ada Generics Looping is independent of the data Assignment depends on the data type to the extent that the amount of storage assigned to the parameters is data-type dependent The compare operation is type dependent (compare integers differently than strings, for example) Basic logic does not change

34 Copyright © 2006 The McGraw-Hill Companies, Inc. generic type element is private; type list is array(natural range <>) of element; with function ">"(a,b:element)return boolean; package sort_pck is procedure sort (in out a : list); end sort_pck;

35 Copyright © 2006 The McGraw-Hill Companies, Inc. package sort_pck is procedure sort (in out a : list) is begin for i in a'first.. a'last - 1 loop for j in i+1.. a'last loop if a(i) > a(j) then declare t : element; begin t := a(i); a(i) := a(j); a(j) := t; end; end if;

36 Copyright © 2006 The McGraw-Hill Companies, Inc. Instantiation To specify an integer sort include the following statement: package integer_sort is new generic_sort( Integer, ">" ); Tells the compiler that integer_sort is a specialized version of generic_sort, with element type Integer and operation “ > ”, the usual math compare operator.

37 Copyright © 2006 The McGraw-Hill Companies, Inc. Generics/Templates Ada, C++ and Java all provide parametric polymorphism in the form of generics or templates One way to implement (C++ method) is by simple textual substitution of concrete parameters for generic parameters prior to compilation Object-oriented languages also provide another type of polymorphism through the inheritance mechanism.

38 Copyright © 2006 The McGraw-Hill Companies, Inc. Pure or SubTyping Polymorphism A function that specifies a parameter from type T will also accept an argument from type S, if S is a subtype of T. (based on inheritance) Example: Class – GeometricShape has method Draw. Subclasses Circle, Square, etc. each have their own Draw method. Type is bound at runtime, since the object passed to the method determines which version of Draw is called.

39 Copyright © 2006 The McGraw-Hill Companies, Inc. 5.10 Programmer-defined Types Recall the definition of a type: A set of values and a set of operations on those values. Structures allow a definition of a representation; problems: Representation is not hidden No new type operations can be defined Objects address this problem, but discussion will be deferred until later.


Download ppt "Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;"

Similar presentations


Ads by Google