Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC321: Programming Languages5-1 CSC321: Programming Languages Chapter 5: Types 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic.

Similar presentations


Presentation on theme: "CSC321: Programming Languages5-1 CSC321: Programming Languages Chapter 5: Types 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic."— Presentation transcript:

1 CSC321: Programming Languages5-1 CSC321: Programming Languages Chapter 5: Types 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic Types 5.5Recursive Data Types 5.6Functions as Types 5.7Type Equivalence 5.8Subtypes 5.9Polymorphism and Generics 5.10Programmer-Defined Types

2 CSC321: Programming Languages5-2 A type is a collection of values and operations on those values. Example: Integer type has values..., -2, - 1, 0, 1, 2,... and operations +, -, *, /, <,... The Boolean type has values true and false and operations , , . Computer types have a finite number of values due to fixed size allocation; problematic for numeric types. Types

3 CSC321: Programming Languages5-3 Exceptions: –Smalltalk uses unbounded fractions. –Haskell type Integer represents unbounded integers. Floating point problems? –Even more problematic is fixed sized floating point numbers: 0.2 is not exact in binary. So 0.2 * 5 is not exactly 1.0 Floating point is inconsistent with real numbers in mathematics. Type Problems

4 CSC321: Programming Languages5-4 In the early languages, Fortran, Algol, Cobol, all of the types were built in. If needed a type color, could use integers; but what does it mean to multiply two colors. Purpose of types in programming languages is to provide ways of effectively modeling a problem solution. Purpose of Types

5 CSC321: Programming Languages5-5 Data Interpretation Machine data carries no type information. Basically, just a sequence of bits. Example: 0100 0000 0101 1000 0000 0000 0000 0000 –The floating point number 3.375 –The 32-bit integer 1,079,508,992 –Two 16-bit integers 16472 and 0 –Four ASCII characters: @ X NUL NUL

6 CSC321: Programming Languages5-6 A type error is any error that arises because an operation is attempted on a data type for which it is undefined. Type errors are common in assembly language programming. High level languages reduce the number of type errors. A type system provides a basis for detecting type errors. Type Errors

7 CSC321: Programming Languages5-7 Type Checking A type system imposes constraints such as the values used in an addition must be numeric. Cannot be expressed syntactically in EBNF. Some languages perform type checking at compile time (e.g., C). Other languages (e.g., Perl) perform type checking at run time. Still others (e.g., Java) do both.

8 CSC321: Programming Languages5-8 A language is statically typed if the types of all variables are fixed when they are declared at compile time. A language is dynamically typed if the type of a variable can vary at run time depending on the value assigned. Can you give examples of each? Static and Dynamic Typing

9 CSC321: Programming Languages5-9 A language is strongly typed if its type system allows all type errors in a program to be detected either at compile time or at run time. A strongly typed language can be either statically or dynamically typed. Union types are a hole in the type system of many languages. Most dynamically typed languages associate a type with each value. Strongly Typing

10 CSC321: Programming Languages5-10 Basic Types Terminology in use with current 32-bit computers: –Nibble: 4 bits –Byte: 8 bits –Half-word: 16 bits –Word: 32 bits –Double word: 64 bits –Quad word: 128 bits In most languages, the numeric types are finite in size. So a + b may overflow the finite range. –Unlike mathematics: a+(b+c)  (a+b)+c Also in C-like languages, the equality and relational operators produce an int, not a Boolean

11 CSC321: Programming Languages5-11 An operator or function is overloaded when its meaning varies depending on the types of its operands or arguments or result. Java: a + b (ignoring size) –integer add –floating point add –string concatenation Mixed mode: one operand an int, the other floating point, or one string and the other int Overloading

12 CSC321: Programming Languages5-12 A type conversion is a narrowing conversion if the result type permits fewer bits, thus potentially losing information. Otherwise it is termed a widening conversion. Should languages ban implicit narrowing conversions? Why? C++/Java: –Explicit narrowing conversion –Implicit widening conversion –How? Type Conversion

13 CSC321: Programming Languages5-13 Non-basic Types Enumeration: –enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; –enum day myDay = Wednesday; In C/C++ the above values of this type are 0,..., 6. More powerful in Java: for (day d : day.values()) Sytem.out.println(d);

14 CSC321: Programming Languages5-14 Pointers C, C++, Ada, Pascal Java??? Value is a memory address Indirect referencing Operator in C: * Example: A Simple Linked List in C struct Node { int key; struct Node* next; }; struct Node* head;

15 CSC321: Programming Languages5-15 Pointer Operations If T is a type and ref T is a pointer: & : T → ref T * : ref T → T For an arbitrary variable x: *(&x) = x Increment/decrement String Copy void strcpy(char *p, char *q) { while (*p++ = *q++) ; }

16 CSC321: Programming Languages5-16 Bane of reliable software development Error-prone Buffer overflow, memory leaks Particularly troublesome in C Problems with Pointers float sum(float a[ ],int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += a[i]; return s; } float sum(float *a,int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += *a++; return s; }

17 CSC321: Programming Languages5-17 Dangling References A dangling reference is a pointer to storage that is being used for another purpose; typically, the storage has been deallocated Dangling reference may cause a program either crashing or giving the wrong results Dangling reference may be caused by –Deallocation operations: a storage that is being pointed to by a pointer has been deallocated. –Procedure/function/method returns: the address of a local variable in a procedure is returned as the return value. Consider the following situation (C/C++):

18 CSC321: Programming Languages5-18 Arrays A set of elements, but different languages have different ways to deal with arrays Basically, an array has the following attributes: Data type – the type of all elements Dimension – number of dimensions: 1- dim, 2-dim, 3-dim, etc Range of dimensions – lower and upper bound for each dimension Subscripts – index. Most languages use integer, but others such as Pascal/Ada allow any discrete types

19 CSC321: Programming Languages5-19 Arrays Layout 1-dim array: consecutive locations High-dim array: –Row-major layout: consecutive locations arranged row by row e.g. a[3][3]: a[0,0], a[0,1], a[0,2], a[1,0], a[1,1], a[1,2], a[2,0], a[2,1], a[2,2] –Column-major layout: arranged column by column e.g. a[3][3]: a[0,0], a[1,0], a[2,0], a[0,1], a[1,1], a[2,1], a[0,2], a[1,2], a[2,2]

20 CSC321: Programming Languages5-20 Arrays Element Accessing Element notation: –C/C++, Java, Algol: a[i][j][k] –Pascal: a[i, j, k] –Fortran: a(i, j, k), same as a function. Actually, Fortran treats accessing array elements as performing function evaluation Pointer accessing: –C/C++ can use pointers to access array elements

21 CSC321: Programming Languages5-21 Equivalence between arrays and pointers: a = &a[0] If either e1 or e2 is type: ref T: e1[e2]=*((e1)+(e2)) Example: a is float[ ] and i int: a[i] = *(a + i) –double a[16]; –double *b; –b = &a[0];// assign the base address of a to b –b++;// move b to the next location: a[1] –b + i;// move b to the next i-th location: a[i+1] Arrays and Pointers

22 CSC321: Programming Languages5-22 Index computation: Loc Assume: base – the location of the first element low – the lower bound upp – the upper bound w – the length of one element 1-dim: Loc(a[i]) = base + w*(i-low) = (base – low *w) + i*w = c + i*w, where c = base – low*w is a constant 2-dim: Loc(a[i,j]) = base + w*[ r*(i-low1) + (j-low2) ] = base - w*( r*low1 + low2) + w*(r*i + j) where r = the number of rows above a[i,j], which is (upp2-low2 + 1) 3-dim: Loc(a[i,j,k]) = base + w*[r2*(r1*(i-low1) + (j-low2)) + (k-low3)] where r1 = (upp2-low2+1), r2 = (upp3-low3+1) ?? Arrays Indexing

23 CSC321: Programming Languages5-23 Name-size binding – bound evaluation: how many storage cells should be allocated to an array Static binding – static evaluation: fixed size, determined at compile time, allocated at load time –Advantages: easy; efficient –Disadvantages: inflexible, waste storage Dynamic binding – dynamic evaluation: changeable size, determined at run time, allocated dynamically –E.g. Java:Declare:double a[]; Create:a = new double[10]; a = new double[15]; Extendable arrays: APL, SNOBOL, Perl –Java also has a kind of extendable array: Vector v = new Vector(5); Arrays Binding

24 CSC321: Programming Languages5-24 Semi-dynamic binding – evaluation upon procedure entry: Ada, Pascal, C/C++, Java –The size (range of dimension) is a parameter. –Once the parameter is determined, the array size is fixed E.g. C/C++:double fun(int n) { double a[n]; …} Ada:declare m, n: Integer; begin declare type Matrix is array(Integer range<>, Integer range<>) of Real; a: Matrix(1..m, 1..n); begin … end … end Arrays Binding

25 CSC321: Programming Languages5-25 Some languages treat array as object/package, and maintain its attributes –E.g. Java: length ---a.length  the size of a –Ada: range:a’range Lower bound:a’first Upper bound:a’last With these attributes, when an array is passed as a parameter, do not need to specify the number of elements in the array However, some languages have to specify the length as a parameter –E.g. in C, array is passed by reference (call-by-reference) and does not have the length attribute. Array Attributes

26 CSC321: Programming Languages5-26 Strings A special kind of array Now so fundamental, directly supported. In C, a string is a 1D array with the string value terminated by a NUL character (value = 0). In Java, Perl, Python, a string variable can hold an unbounded number of characters. Libraries of string operations and functions. –Comparison, selection of substring, searching, matching (Most languages) –Replacement, appending, substring deletion (SNOBOL) –Concatenation: produce a new string –Java: String (static) and StringBuffer (dynamic)

27 CSC321: Programming Languages5-27 Structures/Records Composed from simpler data items Each data item (field) has a name Data items may have different types (recall array, all elements must be in the same type) Access to fields mostly uses.(dot) expression C/C++: struct Date { int day; int month; int year; } C: struct Date adate; C++: Date adate; Access: adate.day Pascal: type Date = record day: Integer; month: Integer; year: Integer; end var adate: Date; Access: adate.day

28 CSC321: Programming Languages5-28 Structures/Records The order of fields in the record has no effect on the meaning When a variable of record is declared, it is allocated storage Record assignment is to assign all fields in a record Collection of elements of different types Used first in Cobol, PL/I Absent from Fortran, Algol 60 Common to Pascal-like, C-like languages Omitted from Java as redundant

29 CSC321: Programming Languages5-29 Comparison of Arrays and Records Component: all elements in an array have the same type, while components (fields) of a record may be different Access: array elements are accessed by subscripts and element locations are determine at run time, while record components are accessed by dot expression and component names are determined at compile time Assignment: array assignment is to assign the base location, while record assignment is to assign all components

30 CSC321: Programming Languages5-30 Unions C: union Pascal: case-variant record Logically: multiple views of same storage Useful in some systems applications type union = record case b : boolean of true : (i : integer); false : (r : real); end; var tagged : union; begin tagged := (b => false, r => 3.375); put(tagged.i); -- error end

31 CSC321: Programming Languages5-31 class Value extends Expression { // Value = int intValue | boolean boolValue Type type; int intValue; boolean boolValue; Value(int i) { intValue = i; type = new Type(Type.INTEGER); } Value(boolean b) { boolValue = b; type = new Type(Type.BOOLEAN); } Simulated union type

32 CSC321: Programming Languages5-32 Recursive Data Type Type definition refers to another type that refers to the current type definition E.g. type link =  cell;// refer to cell type cell = record data: integer; next: link;// refer to link end;

33 CSC321: Programming Languages5-33 Dynamic Data Type With dynamic data structures, you can not only operate on the data items in the structure, but also change the structure Usually constructed with static data structures (such as arrays and records) and pointers E.g. Linked lists, Trees The basic components of these structures are records that contain pointers: nodes. Usually, each node has two parts: data items, and links (pointers/references)

34 CSC321: Programming Languages5-34 Functions as Types 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. Java example public interface RootSolvable { double valueAt(double x); } public double Newton(double a, double b, RootSolvable f);

35 CSC321: Programming Languages5-35 Type Names and Equivalence Type names –Each type may or may not have a name, such as int, Integer, Char, etc. are type names; while array [0..9] of integer is an anonymous type.Name equivalence Type equivalence –Name equivalence –Structural equivalence

36 CSC321: Programming Languages5-36 Type Name Equivalence Name equivalence: have the same name type Pure name equivalence: exactly same name Transitive name equivalence: –if S is equivalent to T which is equivalent to U, then S is equivalent to U Type expression equivalence: –A type name is equivalent to itself. –Two type expressions are equivalent if they are formed by applying the same constructor to equivalent expressions.

37 CSC321: Programming Languages5-37 Type Structural Equivalence Two type expressions are structurally equivalent if and only if they are equivalent under the following three rules –A type name is structurally equivalent to itself –Two types are structurally equivalent if they are formed by applying the same type constructor to structurally equivalent types –After a type declaration, type n = T, the type name n is structurally equivalent to T Examples –integer is structurally equivalent to integer –Types S and T are structurally equivalent if they are declared as type S = array [0..9] of integer; type T = array [0..9] of integer; –Types S and integer are structurally equivalent if S is declared as: type S = integer;

38 CSC321: Programming Languages5-38 struct complex { float re, im; }; struct polar { float x, y; }; struct { float re, im; } a, b; struct complex c, d; struct polar e; int f[5], g[10]; which are equivalent types: a, b, c, d, e, f, g? Type Equivalence

39 CSC321: Programming Languages5-39 Pascal uses name equivalence, but ambiguous. Modula-2 (the successor of Pascal) defines two types S and T to be compatible if –S and T are the same name, or –S = T is a type declaration: type S = T; or type T = S; or –One is a subrange of the other; or –Both are subranges of the same basic types C uses structural equivalence for all types except records (struct) Type Equivalence Implementations

40 CSC321: Programming Languages5-40 Subtypes A subtype is a type that has certain constraints placed on its values or operations. In Ada subtypes can be directly specified. 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; Integer i = new Integer(3);... Number v = i;... Integer x = (Integer) v; //Integer is a subclass of Number, // and therefore a subtype

41 CSC321: Programming Languages5-41 Polymorphism and Generics 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 is that it enables code reuse.

42 CSC321: Programming Languages5-42 Polymorphism Comes from Greek, and means having many forms Example: overloaded built-in operators and functions + - * / == !=... –Ada, C++: define + -... for new types –Java overloaded methods: number or type of parameters Example: class PrintStream print, println defined for: boolean, char, int, long, float, double, char[ ] String, Object –Java: + also used for string concatenation –Java: instance variable, method: e.g., name, name( )

43 CSC321: Programming Languages5-43 Generics Ada generics: generic sort (Figs. 5.10 and 5.11) –parametric polymorphism –type binding delayed from code implementation to compile time –procedure sort is new generic_sort(integer); Java generic classes C/C++ templates

44 CSC321: Programming Languages5-44 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 –Type operations cannot be defined Solutions: ADT – Abstract Data Types


Download ppt "CSC321: Programming Languages5-1 CSC321: Programming Languages Chapter 5: Types 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4NonBasic."

Similar presentations


Ads by Google