Presentation is loading. Please wait.

Presentation is loading. Please wait.

Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types

Similar presentations


Presentation on theme: "Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types"— Presentation transcript:

1 Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Recursive Data Types Functions as Types Type Equivalence Subtypes Polymorphism and Generics Programmer-Defined Types

2 Types A type is a collection of values and operations on those values.
Example: Integer type has values ..., -2, -1, 0, 1, 2, ... and operations +, -, *, /, <, ... Boolean type has values true and false and operations , , . A type system is a precise definition of the bindings between the type of a variable, its values and the possible operations on those values. It provides a basis for detecting type errors. A type error is any error that arises because an operation is attempted on a data type for which it is undefined. Computer types have a finite number of values due to fixed size allocation; problematic for numeric types. Exception - Haskell type Integer represents unbounded integers.

3 Types 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. Some languages perform type checking at compile time (eg, C). Other languages (eg, Perl) perform type checking at run time. Still others do both (eg, Java - downcasting - stringRef = (String) objRef; ). 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.

4 Types $ cat Test.java class Animal { static int value = 100; }
class Cat extends Animal { int value = Animal.value + 1; } class Dog extends Animal { int value = Animal.value + 2; } public class Test { public static void main(String[] args) { Cat c = new Cat(); System.out.println(c.value); Dog d = new Dog(); System.out.println(d.value); Animal a = c; System.out.println(a.value); Object o = c; // System.out.println(o.value); a = d; type(c); type(d); type(a); // type(a.value); Dog d2 = (Dog) a; a = c; Dog d3 = (Dog) a; public static void type(Animal a){ System.out.println("I am a " + a); $ java Test 101 102 100 I am a I am a Exception in thread "main" java.lang.ClassCastException: Cat cannot be cast to Dog at Test.main(Test.java:25) Uncomment // System.out.println(o.health); $ javac Test.java Test.java:20: cannot find symbol symbol : variable health location: class java.lang.Object System.out.println(o.health); ^ 1 error Uncomment // System.out.println(a.health); Test.java:23: type(Animal) in Test cannot be applied to (int) type(a.health);

5 Types A type conversion is a narrowing conversion if the resulting type permits fewer bits, thus potentially losing information. Otherwise it is termed a widening conversion. An operator or function is overloaded when its meaning varies depending on the types of its operands or arguments or result.

6 Types Errors $ cat prog.c #include <stdio.h>
#include <stdlib.h> int main () { int x = 1, y = 2; float a = 1.1, b = 2.2; printf("\n%d, %d, %d, %d\n", x, x < y, x > y, y); printf("%f, %f, %f, %f\n", x, x < y, x > y, y); printf("%f, %d, %d, %f\n", a, a < b, a > b, b); printf("%d, %d, %d, %d\n", a, a < b, a > b, b); }

7 Type Errors $ cat test.java class test { static int x = 1, y = 2;
public static void main(String args[]) System.out.println( x + ", " + x < y + ", " + x > y + ", " + y ); } // Doesn’t compile

8 Type Errors class test { static int x = 1, y = 2;
$ cat test.java class test { static int x = 1, y = 2; static float a = 1.1f, b= 2.2f; public static void main(String args[]) System.out.println( x + ", " + (x < y) + ", " + (x > y) + ", " + y ); System.out.println( a + ", " + (a < b) + ", " + (a > b) + ", " + b ); }

9 Type Errors $ cat test.java class test { static int x = 1, y = 2;
static float a = 1.1f, b= 2.2f; public static void main(String args[]) System.out.println( x + ", " + (x < y) + ", " + (x > y) + ", " + y ); System.out.println( a + ", " + (a < b) + ", " + (a > b) + ", " + b ); System.out.println( (x / y) + ", " + (a / b) + ", " + (x / b) + ", " + (x < b) ); }

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

11 Nonbasic Types Pointers Enumerations Arrays Strings Structures Unions

12 Pointers C, C++, Ada, Pascal Java??? Value is a memory address Indirect referencing Operator in C: *

13 Pointer Examples void strcpy(char *p, char *q) { while (*p++ = *q++) ;
} a[i] = *(a + i) struct Node { int key; struct Node* next; }; struct Node* head; 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;

14 Pointer Examples $ cat test.
float x[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; main() { printf("In main, a[1]: %f\n", x[1]); printf("x: %d\n", x); sum(x, 10); } sum( float a[], int n) { printf("a: %d\n", a); int i; float s = 0.0; for (i = 0; i<n; i++) s += a[i]; printf("In loop s, a[i]: %f\n", s, a[i]); printf("s: %f\n", s); $ cat test.c main() { char s1[] = "string1"; char s2[] = "string2"; printf("s1, s2: %s, %s\n", s1, s2); mystrcpy(s1, s2); } mystrcpy(char *p, char *q) char *sp = p; char *sq = q; printf("p, q: %s, %s\n", sp, sq); while (*p++ = *q++) ;

15 Pointer Operations $ cat test.c int main ( ) { int x = 101, y = 202;
int *p; printf("x: %d, y: %d, p: %d\n", x, y, p); p = &y; x = *p; *p = y; printf("x: %d, &x: %d, *(&x): %d\n", x, &x, *(&x) ); }

16 Pointers Bane of reliable software development Error-prone Buffer overflow, memory leaks Particularly troublesome in C

17 Type Systems Type Rule 1. All referenced variables must be declared.
Type Rule 2. All declared variables must have unique names. Type Rule 3. A program is valid if its Declarations are valid and its Block body is valid with respect to the symbol table (dictionary) for those Declarations. Type Rule 4. Validity of a Statement: - A Skip is always valid - An Assignment is valid if: Its target Variable is declared Its source Expression is valid If the target Variable is float, then the type of the source Expression must be either float or int Otherwise if the target Variable is int, then the type of the source Expression must be either int or char Otherwise the target Variable must have the same type as the source Expression.

18 Type Systems Type Rule 4. - A Conditional is valid if:
Its test Expression is valid and has type bool Its thenbranch and elsebranch Statements are valid - A Loop is valid if: Its Statement body is valid - A Block is valid if all its Statements are valid. Type Rule 5. - A Value is always valid. - A Variable is valid if it appears in the symbol table (dictionary). - A Binary is valid if: Its Expressions term1 and term2 are valid If its Operator op is arithmetic, then both Expressions must be either int or float If op is relational, then both Expressions must have the same type If op is && or ||, then both Expressions must be bool

19 Type Systems Type Rule 5. - A Unary is valid if:
Its Expression term is valid, Type Rule 6. The type of an Expression e is: - If e is a Value, then the type of that Value. - If e is a Variable, then the type of that Variable. - If e is a Binary op term1 term2, then: If op is arithmetic, then the (common) type of term1 or term2 If op is relational, && or ||, then bool - If e is a Unary op term, then: If op is ! then bool

20 Implicit Type Conversion
Assignment supports implicit widening conversions We can transform the abstract syntax tree to insert explicit conversions as needed. The types of the target variable and source expression govern what to insert. Example: Suppose we have an assignment f = i - int(c); (f, i, and c are float, int, and char variables). The abstract syntax tree is:

21 Implicit Type Conversion
Example continued: So an implicit widening is inserted to transform the tree to: Here, c2i denotes conversion from char to int, and itof denotes conversion from int to float. Note: c2i is an explicit conversion given by the operator int() in the program.

22 Formalizing the Type System
Symbol Table (TypeMap): Created by: (Type Rule 1) Validity of Declarations: (Type Rule 2) (Type Rule 3)

23 Formalizing the Type System
(Type Rule 4, simplified version for an Assignment

24 Formalizing the Type System
(Type Rule 5, abbreviated versions for Binary and Unary

25 Formalizing the Type System
(Type Rule 6, abbreviated version


Download ppt "Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types"

Similar presentations


Ads by Google