type
play

Type u A type is a collection of values and operations on those - PDF document

10/18/15 CSCI 2325 Principles of Programming Languages Types & Type System Reading: Ch 5 & 6 (Tucker & Noonan) Type u A type is a collection of values and operations on those values. u Example u Integer type has values ...,


  1. 10/18/15 CSCI 2325 Principles of Programming Languages Types & Type System Reading: Ch 5 & 6 (Tucker & Noonan) Type u A type is a collection of values and operations on those values. u Example u Integer type has values ..., -2, -1, 0, 1, 2, ... and operations +, -, *, /, <, … u Boolean type has values true and false and operations ∧ , ∨ , ¬ . u Question 1: Will the values be bounded? u Question 2: Will the types be pre-defined? 1

  2. 10/18/15 Type errors & Type systems u Type error: Incompatibility between data type and operations u Type system: Detects type errors u Can’t we detect type errors just by looking at the values? u Machine level: 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 2

  3. 10/18/15 Static vs. dynamic type systems u A language is statically typed if the types of all variables are fixed when they are declared at compile time. u Example? u A language is dynamically typed if the type of a variable can vary at run time depending on the value assigned. u Example? u Both static and dynamic typing? u Example program next u Which one is “better”—static or dynamic? Java: Why dynamic typing for type casting? 3

  4. 10/18/15 Strongly typed language u 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. u Independent of static or dynamic typing u Very confusing area: u http://ericlippert.com/2012/10/15/is-c-a- strongly-typed-or-a-weakly-typed-language/ u http://c2.com/cgi/wiki?StronglyTyped Is C strongly typed? u No, because errors can go undetected. #include <stdio.h> union {int i; float f;} u; int main() { float x = 0.0; u.i = 987654321; x = x + u.f; printf ("%f\n", x); return 0; } 4

  5. 10/18/15 Basic vs. non-basic types u Basic: int, float, bool, char u Implicit type conversion: narrowing vs. widening u Non-basic: pointer, string, array, list, etc. + programmer-defined types u Implementation issues Implementation of arrays u C: int a[5]; u Static memory allocation: “dope vector” followed by actual array elements u Dope vector: element size, element type, # of elements u Array elements must be stored contiguously u Why? 5

  6. 10/18/15 Arrays: C vs. Java u Java: dynamic allocation, but array size/type cannot be changed thereafter int[] a = new int(5); u Pre-initialized to 0s u Contiguous in memory? u Not guaranteed List vs. array u Size of list may vary at run-time u List elements may not be contiguous in memory u List may contain heterogeneous data (Python) 6

  7. 10/18/15 Other issues u Functions as types – C vs. Java u Sub-types u Polymorphism Type System Ch. 6 7

  8. 10/18/15 Motivation: Detecting Type Errors u The detection of type errors, either at compile time or at run time, is called type checking . u Type errors occur frequently in programs. u Type errors can’t be prevented/detected by EBNF u If undetected, type errors can cause severe run-time errors. u A type system can identify type errors before they occur. Type System for CLite u Static typing and type checking at compile time u Single function: main u Single scope: no nesting, no global variables u Type rules Each referenced variable must be declared 1. Each declared variable must have a unique identifier 2. Identifier must not be a keyword (syntactically enforced) 3. 8

  9. 
 10/18/15 Example Clite Program void main ( ) { int n; int i; int result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; } print result; } Type Rule 1 u All referenced variables must be declared. u Type map is a set of ordered pairs E.g., {<n, int>, <i, int>, <result, int>} u Can implement as a hash table 9

  10. 
 10/18/15 Type Rule 2 u All declared variables must have unique names. void main ( ) { int n; int i; These must all be unique int result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; } 
 print result; } Type checking a program u A program is valid if u Declarations are valid and u Rest is valid wrt Declarations void main ( ) { int n; int i; int result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; } 
 print result; } 10

  11. 10/18/15 Type checking a program (cont…) u Validity of a Statement: u Assignment statement is valid if u Its target Variable is declared u Its source Expression is valid u If the target Variable is float, then the type of the source Expression must be either float or int u Otherwise if the target Variable is int, then the type of the source Expression must be either int or char u Otherwise, the target Variable must have the same type (e.g., bool) as the source Expression Type checking a program (cont…) u A conditional stmt is valid if: u Its test Expression is valid and has type bool u Its body is valid u A while loop is valid if: u Its test Expression is valid and has type bool u Its body is valid 11

  12. 10/18/15 u Validity of an Expression: u A Value is always valid. u A Variable is valid if it appears in the type map. u A Binary is valid if: u Its Expressions term1 and term2 are valid u If its Operator op is arithmetic, then both Expressions must be either int or float u If op is relational, then both Expressions must have the same type u If op is && or ||, then both Expressions must be bool u A Unary is valid if: u Its Expression term is valid, u … u The type of an Expression e is: u If e is a Value, then the type of that Value. u If e is a Variable, then the type of that Variable. u If e is a Binary op term1 term2, then: u If op is arithmetic, then the (common) type of term1 or term2 u If op is relational, && or ||, then bool u If e is a Unary op term, then: u If op is ! then bool u … 12

Recommend


More recommend